Interfacing C++ with Python: Implementation of Extensions and Embedding
In modern software development, both C++ and Python are very popular programming languages. Although they have their own advantages and disadvantages, many projects require leveraging the strengths of both languages. C++ is typically used for high-performance computing, while Python is favored for its simplicity and rich libraries. Therefore, achieving interaction between C++ and Python helps to fully utilize the capabilities of both in the same project.
In this article, we will detail how to implement interaction between C++ and Python through extensions and embedding, while providing relevant code examples to help basic users understand easily.
1. Extensions
1.1 Overview
Extensions refer to compiling C++ code into a dynamic link library (DLL or .so), which can then be called from Python. Here, we will create a simple C++ class and call its method from Python.
1.2 Creating C++ Code
First, create a file named <span>example.cpp</span>:
#include <iostream>
#include <pybind11/pybind11.h>
namespace py = pybind11;
class Example {
public:
void greet() {
std::cout << "Hello from C++!" << std::endl;
}
};
PYBIND11_MODULE(example, m) {
m.doc() = "example module"; // optional documentation
py::class_<Example>(m, "Example")
.def(py::init<>())
.def("greet", &Example::greet);
}
The above code defines a simple class <span>Example</span>, which has a method that outputs a greeting. Then, it is converted into a module that can be called from Python using the <span>PYBIND11_MODULE</span> macro.
1.3 Compiling C++
To compile the above code, you need to install the pybind11 library and use the appropriate command. For example, on a Linux system, you can use the following command:
c++ -std=c++14 -O3 -Wall -shared -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix`
Make sure you have installed pybind11 suitable for your system version and Python environment.
1.4 Using Python to Call C++
Once the module is successfully compiled, you can use it in Python. Run the following Python code in the terminal:
import example
obj = example.Example()
obj.greet()
After running this script, the terminal should display:
Hello from C++!
This indicates that we have successfully called a C++ method from Python, which is one way of extending functionality!
2. Embedding
2.1 Overview
Embedding refers to integrating the Python interpreter into a C++ application, allowing the application to execute Python scripts or functions. Below, we demonstrate how to perform this integration.
2.2 Creating Embedded C++
First, create a new file <span>embed.cpp</span>:
#include <iostream>
#include <python3.8/Python.h> // Ensure the path to python.h is correct
int main() {
Py_Initialize(); // Initialize Python
PyRun_SimpleString("print('Hello from embedded Python!')"); // Execute a simple script
Py_Finalize(); // Finalize and clean up
return 0;
}
The above code initializes and runs a simple Python script within a C++ program, then prints the corresponding information.
2.3 Compiling and Running
Use a command similar to the one below to compile this embedded application (remember to modify according to your paths):
g++ -std=c++14 embed.cpp -o embed \
-Python.h \
-L/usr/lib/python3.x/config-3.x-x86_64-linux-gnu \
-lpython3.x \
-I/usr/include/python3.x/
Adjust the version information in the parameters to match your actual situation. Some systems may require additional parameters; if errors occur, please refer to the relevant manuals.
Then directly run the generated executable file:
./embed
The console should output the following:
Hello from embedded Python!
Conclusion
This article introduced how to achieve data transfer and function calls between C++ and Python through both extensions and embedding. On one hand, by creating dynamic link libraries, existing C/C++ open-source functionalities become more accessible; on the other hand, integrating a complete and powerful Python interpreter into the application also facilitates complex operations such as data analysis and reusing existing logic. Both techniques have wide applications, and I believe readers can smoothly apply them to their respective scenarios to enhance development efficiency.