Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

This Is How To Use C++ Functions In A Python App

pexels victoria rain 4576464

Do you want to know how to create and C++ functions in a Python app? In this article we will show you how to use the new C++ Builder 12 to create a C++ function, step by step, and then call that function in any Python app. C++ Builder 12 is released packed full of great features, optimizations, and improvements. One of the great features of C++ Builder 12 was the inclusion of an initial CLANG C++ compiler which is another big step introducing a new 64bit bcc64x CLANG compiler which supports C++11, C++14, C++17, and partially C++20 standards. It is still in active development, but I would like to explain there is a great feature of new clang compiler that supports the latest Python modules. In this post, we explain how to develop Python modules with the new bcc64x C++ Builder 12 CLANG compiler.

What about using Python with Delphi?

If you are using Delphi, you should also check out the Python4Delphi tools.

You can find out more about our free Python tools here.

and read about Python4Delphi in this blog post:

What is Python?

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.

In the programming world today, the Python programming language is very popular – perhaps second only to C++. Python is easy to use, and hugely popular because it is easy to learn and has the support from big companies like Google. One of the areas in which 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/

The latest version of Python (at the time of writing) is Python 3.12, released in October 2023. It has a lot of new changes and improvements; you can download it from here: https://www.python.org/downloads/

What is the new C++ Builder 12 and its new CLANG Compiler?

Do you know, you can develop Python modules with the new bcc64x compiler in a C++ Builder 12? Yes, the new bcc64x is compatible with the latest version of Python. I should note that, this compiler is not integrated into IDE yet (it is planned, soon) but nervertheless we can use RAD Studio Command Prompt for the C++ Builder 12.

Here are some great features of C++ Builder 12,

How to write a Python module in C++?

The official Python page explains how you can extend Python by adding new built-in modules https://docs.python.org/3/extending/extending.html . Let’s sum up all here below.

First, let’s create our simple Hello World module in C++, this file will be “hello.cpp“. Create a new cpp file with RAD Studio or with any IDE. Now, let’s develop our first Python module in C++.

1. Include Python.h header
Include your headers and then include Python header,

[crayon-6741b0585e7d3435810466/]

Be sure that path folder of python header files is defined in your compiler options.

2. Define your function

Let’s define a world() function that prints “Hello C++ World!”,

[crayon-6741b0585e7de115540899/]

3. Create a Python object for your function and return to Python

[crayon-6741b0585e7e0578551600/]

4. Store all your functions above by creating array of static type PyMethodDef

[crayon-6741b0585e7e1762350034/]

5. Create the structure with the name of the module to be used in Python,

[crayon-6741b0585e7e3695353806/]

6. Create a gateway to Python. The return type must be: PyMODINIT_FUNC,
The name must be PyInit_ the name of your lib (void arguments), i.e. for the hello library it must be PyInit_hello(void).

[crayon-6741b0585e7e5130628912/]

As a result, we will have hello.cpp file. We will compile and use this file in steps below.

How to develop Python modules with the new C++ Builder 12 CLANG compiler?

Step 1. Generate Python import library

Goto Python installation directory and generate the import library from Python dll

[crayon-6741b0585e7e6544547799/]

Step 2. Copy Python import library

Copy the python312.a into the Python libs folder (i.e. “PythonPython312libs”)

Step 3. Create your C++ code for the Python module

Create hello.cpp file to develop a “Hello World” example as described above. Here is the full C++ code example:

hello.cpp

[crayon-6741b0585e7e8002533432/]

Step 4. Create some Python code that uses the module

In Python, we use modules by using import command. Then we can run our world() method of the hello library. Create a new test.py python code as shown below.

test.py

[crayon-6741b0585e7ea044076307/]

Step 5. Create a bat file that creates a shared library using bcc64x

If you want to compile a Python module in C++ with bcc64x the best way is creating a .bat file that has compiler options. Of course, you can do this in a single line command. In this example we will use .bat file.

To compile hello.cpp with the new bcc64x compiler, in addition to bcc64x command,

Create a new compile.bat file with a notepad or with any IDE . Here is an example compile.bat file that shows how to compile C++ file (.cpp) as a shared library module (.pyd). Note that you should modify this code below with your own user folder name and settings (here mine was atavm)

compile.bat

Note that due to a limitation of the blogging platform you should replace the forward slashes in the paths with backslashes.

[crayon-6741b0585e7ec156454818/]

This will create a shared library module using bcc64x, be sure that the LIBS and INCS folder are correct, and your file names are correct. When you run this bat file in PowerShell or DOS, this will create hello.pyd Python module by using python312 import library and by compiling hello.cpp file as in the options above.

Step 6. Execute compile.bat and run Python example

On the PowerShell or DOS prompt, execute compile.bat and run test.py example as below.

[crayon-6741b0585e7ed231852734/]

and the output will be as follows:

[crayon-6741b0585e7f0853310517/]

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. 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 UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Exit mobile version