[英]How can I run a Hello world c++ code using python
I want to know how can I create a python script that run c++ code.我想知道如何创建运行 c++ 代码的 python 脚本。
I did find some talks about subprocess module but it's used to run commands I did find some talks about Boost and Swig but I didn't understand as a beginner how to use them我确实找到了一些关于 subprocess 模块的讨论,但它用于运行命令 我确实找到了一些关于 Boost 和 Swig 的讨论,但作为初学者,我不明白如何使用它们
Testing subprocess:测试子流程:
import subprocess
subprocess.call(["g++", "main.cpp"],shell = True)
tmp=subprocess.call("main.cpp",shell = True)
print("printing result")
print(tmp)
Can any one help me please!任何人都可以帮助我吗?
A simple example would be to create a .cpp
file:一个简单的例子是创建一个
.cpp
文件:
// cpy.cpp
#include <iostream>
int main()
{
std::cout << "Hello World! from C++" << std::endl;
return 0;
}
And a Python script:还有一个 Python 脚本:
// cpy.py
import subprocess
cmd = "cpy.cpp"
subprocess.call(["g++", cmd])
subprocess.call("./a.out")
Then in the terminal, run the Python script:然后在终端中,运行 Python 脚本:
~ python cpy.py
~ Hello World! from C++
EDIT:编辑:
If you want control of calling C++ functions from Python, you will need to create bindings to extend Python with C++.如果你想控制从 Python 调用 C++ 函数,你需要创建绑定来扩展 Python 和 ZA7F6F87F87C9FDF183732097.CFCF18 This can be done a number of ways, the Python docs has a thorough raw implementation of how it can be done for simple cases, but also there are libraries such as pybind and boost.Python that can do this for you.
这可以通过多种方式完成,Python 文档对如何在简单情况下完成此操作进行了彻底的原始实现,但也有诸如 pybind 和 boost.Python 之类的库可以为您执行此操作。
An example with boost.Python: boost.Python 的示例:
// boost-example.cpp
#include <iostream>
#include <boost/python.hpp>
using namespace boost::python;
int printHello()
{
std::cout << "Hello, World! from C++" << std::endl;
}
BOOST_PYTHON_MODULE(hello)
{
def("print_hello", printHello);
}
You will need to create a shared object file (.so) and make sure to link the appropriate Python headers and libraries.您需要创建一个共享的 object 文件 (.so) 并确保链接适当的 Python 头文件和库。 An example might look like:
一个示例可能如下所示:
g++ printHello.cpp -fPIC -shared -L/usr/lib/python2.7/config-3.7m-x86_64-linux-gnu/ -I/usr/include/python2.7 -lpython2.7 -lboost_python -o hello.so
And in the same directory that you created the hello.so
file:在您创建
hello.so
文件的同一目录中:
python
>>> import hello
>>> hello.print_hello()
Hello, World! from C++
Boost.Python can be used to do some pretty magic things, including exposing classes, wrapping overloaded functions, exposing global and class variables for reading and writing, hybrid Python/C++ inheritance heirarchies, all with the utility of dramatic performance gains. Boost.Python 可以用来做一些非常神奇的事情,包括公开类、包装重载函数、公开全局变量和 class 变量以进行读写、混合 Python/C++ Z5FED3411FAF832274EF1F040028 I recommend going through these docs and getting to know the API if you are looking to go down this route.
如果您正在寻找 go,我建议您阅读这些文档并了解 API。
As an alternative to compiling the C++ code into a separate program and executing that, you can also use cppyy ( http://cppyy.org ) to run the C++ code directly, through a JIT, within the same program.作为将 C++ 代码编译到单独的程序中并执行该程序的替代方法,您还可以使用 cppyy ( http://cppyy.org ) 直接在相同的程序中运行 ZF6F87C9FDCF8B3C3F07F93F1EEIT8712 代码,
Example:例子:
import cppyy
cppyy.cppdef('''
void hello() {
std::cout << "Hello, World!" << std::endl;
}''')
cppyy.gbl.hello() # calls the C++ function 'hello' defined above
You can use the.os module of python to run os commands.您可以使用 python 的 .os 模块来运行 os 命令。
import os
myCmd1 = 'ls -la'
os.system(myCmd)
Your command can be 'g++ main.cpp second.cpp -o run', then you can use the same mechanism to call the ./run
shell.你的命令可以是'g++ main.cpp second.cpp -o run',然后你可以使用相同的机制来调用
./run
shell。
Make sure you have the right permissions确保您拥有正确的权限
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.