Python is hugely popular and has some really useful libraries. Python is particularly strong in the field of AI and machine learning (ML). If you are C++ developer, you might want your users to be able to analyze data with AI modules or frameworks written in the Python language in your applications. Let’s imagine you want the users to carry out a few button clicks to do some heavy AI analysis. That way you get the best of both worlds by having the raw speed and power of C++ which can run Python modules. Do you know, you can do this in a C++ IDE, very easily? In this post, we will show how you can integrate your C++ application with the Python language.
Table of Contents
Why should I use the Python language in my C++ app?
In the programming world today, Python programming language is very popular – perhaps second only to C++. Python is easy to use, and it is more popular because of support from big companies like Google. One of the areas Python is particularly successful are with libraries and frameworks for AI technology and machine learning. It is a great object-oriented, interpreted, and interactive programming language. Python has very clear syntax. It has modules, classes, exceptions, very high-level dynamic data types, and dynamic typing. There are interfaces to many system calls and libraries, as well as to various windowing systems. New built-in Python modules are easily written in C or C++. Python is also usable as a scripting extension for applications written in other languages that benefit from its ease of use. Python is free to use. More information can be found on their official web page https://www.python.org/
We should note that Python can be used inside Delphi projects and this prime number function is tested before in Delphi and in Python here https://www.youtube.com/watch?v=aCz5h96ObUM with Kiriakos Vlahos. Here, we were inspired by this video, and we try to do the same tests on the same function in C++. Finding Prime Numbers in C++ is explained in detailed well in this Easily Learn To Find Prime Numbers In Modern C++ post here.
Why should I use C++ instead of Python in an app?
C and C++ are the fastest and one of the most powerful programming languages and it is easy to do computations as fast as possible. For example, C++ Builder is a great IDE, includes compilers for Win32, Win64, Android, and iOS. C++Builder has both CLANG Enhanced C/C++ Compiler and Embarcadero’s new Bcc C/C++ Compiler. It also features a modern, high-productivity RAD Studio IDE, debugger tools, and enterprise connectivity to accelerate cross-platform UI development. You can develop GUI based applications easily, as it comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs. There is a free C++ Builder Community Edition that can be used by students, beginners, and startups with limitations too.
In our different Prime Numbers Tests (I found that this test is a good test which has integers, floats, floating functions, logical functions, and iterations) on the same PC System, our tests show that C++ is about 9.4 times faster than Python on counting prime numbers in 64 bits and about 17.8 times faster in 32 bits. The calculating of prime numbers is good to use some basic logical operations and mathematical functions. The procedure includes logical if()
clauses, for()
loops, some mathematical procedures (floor
, sqrt
etc.), that show C++ is very fast in mathematical operations. These kinds of tests can be done in different operations to compare languages. Even the best-written Python cannot come anywhere near to the speed of a similar C++ app.
In some other examples, Python maybe 20, 50, 100, 200 times slower. Don’t forget that Python uses C++ modules ‘under the hood’. When you run some commands it uses these modules, which makes it faster in its modular functions, i.e. in AI applications.
Where can I use Python language in my C++ app?
If you want to use Python features in your applications, there is a simple way to do it. First, let’s give an example,
For example, you developed a very complex C++ application that gets instant images from different cameras with different connection modules, let’s assume that users can edit, and do many operations on these images by your application, there are user login and security modules, database connections, etc., etc. And, at some point, you want your users to know what is in that picture or you want your application to warn if there is a human (or car, or a wild animal) in that view. To do this you have 3 different choices;
- You can develop your own Deep Learning library in C++
- You can use 3rd party Deep Learning libraries in C++ (open source or paid)
- You can use Python and its frameworks, and adapt some examples from the internet
Let’s assume that, you found some simple Python codes that can learn and analyze from these images and you want to use this Python code in your beautifully skinned modern high-res C++ app. In this situation, the best to do is to follow the 3rd choice. In this post below, we explain 3rd choice, which is simple and easy to use the latest Python and easy to run the latest Python codes you found.
How to use the Python language in a C++ app?
First, you need an appropriate version of Python with the Python codes you want to use. Python is free to use and more information can be found on their official web page https://www.python.org/, install it from there. Then may need to install more modules or frameworks to your Python. Be sure that the Python compiler is running in any paths in the command console. Now let’s give a very simple and modern C++ Builder VCL example.
- Create a new C++ Builder Application (VCL or FMX)
- Add a Memo (TMemo) to enter some Python codes, this will be Memo1
- Add another Memo (TMemo) for the outputs of Python codes, this will be Memo2
- Add a Button (TButton) to run Python codes, this will be Button1, set its caption or text to “Save and Run”
- Double Click to Button1, this will generate Button1Click()
- Add lines below inside that Button1Click() event,
When the user clicks this button, first, we will save Python codes written in Memo1 to a tmp.py file
1 2 3 |
Memo1->Lines->SaveToFile("tmp.py"); |
Then, we will run this tmp.py Python file by using system() command and output to tmp.out,
1 2 3 |
std::system("python tmp.py > tmp.out"); |
Finally, we will read the output from tmp.out,
1 2 3 4 |
// Load output file Memo2->Lines->LoadFromFile("tmp.out"); |
More on how to use the Python language in a C++ application
Briefly, these are 3 steps together in C++ Builder application. It’s very easy.
1 2 3 4 5 6 7 8 9 10 |
// Save written Python codes Memo1->Lines->SaveToFile("tmp.py"); // Run Python on this file and output to another file std::system("python tmp.py > tmp.out"); // Load output file Memo2->Lines->LoadFromFile("tmp.out"); |
Here is a VCL example that runs python code:
How to use the Python language in a C++ VCL application?
Here is a full VCL example in C++ Builder,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <vcl.h> #pragma hdrstop #include "Python_Runner_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { // Save written Python codes Memo1->Lines->SaveToFile("tmp.py"); // Run Python on this file and output to another file std::system("python tmp.py > tmp.out"); // Load output file Memo2->Lines->LoadFromFile("tmp.out"); } |
How to use the Python language in a cross-platform C++ FMX application?
Here is a FMX example in C++ Builder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <fmx.h> #pragma hdrstop #include "Python_Runner_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { // Save written Python codes Memo1->Lines->SaveToFile("tmp.py"); // Run Python on this file and output to another file std::system("python tmp.py > tmp.out"); // Load output file Memo2->Lines->LoadFromFile("tmp.out"); } |
How to use the Python language in a C++ console application?
Here is a simple console application that can be used in any windows compiler like C++ Builder, Dev-C++, MSVC, VSCode, GNU gcc g++, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> int main() { // Run Python on this file and output to another file std::system("python tmp.py > tmp.out"); // use tmp.out (analzye, edit, display,...) return 0; } |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.