Python 在C++;
我目前正在处理一个使用单独cli和gui的项目。GUI部分用C++编写,以保持可执行的小。我用pythSudio编译了python程序,需要调用C++程序中的一些函数。 Python部分:Python 在C++;,python,c++,pyinstaller,static-linking,Python,C++,Pyinstaller,Static Linking,我目前正在处理一个使用单独cli和gui的项目。GUI部分用C++编写,以保持可执行的小。我用pythSudio编译了python程序,需要调用C++程序中的一些函数。 Python部分: import configparser def getconfig() -> list: config = configparser.ConfigParser() config.read("config.ini") section1 = config[&quo
import configparser
def getconfig() -> list:
config = configparser.ConfigParser()
config.read("config.ini")
section1 = config["general"]
section2 = section["section2"]
return [section1, section2] #should be a list of dict?
通过pyinstaller--onefile--clean myprogram.py编译它
<>我想在C++中做的是:
//please correct me if this is the wrong type,
//i know i probably need to do a bit of parsing between python and C++ types
std::vector<std::unordered_map<std::string, std::string>> > = myprogram.getconfig()
//如果这是错误的类型,请更正我,
//我知道我可能需要在Python和C++类型之间进行一点解析。
std::vector>=myprogram.getconfig()
<>我不想再做C++中的配置解析,或者你推荐它,因为调用编译的Python二进制可能更容易?
此程序只需要在linux上运行,windows是不必要的。如果您只计划在POSIX兼容系统(即GNU/linux)上运行,您可以生成一个新进程来运行Python脚本,例如使用C函数
exec
。例如:
#包括
#包括
#包括
#包括
#包括
int main()
{
char*args[]={“pythonscript.py”,NULL};
int t=execv(“pythonscript.py”,args);
如果(t<0)printf(“%s\n”,strerror(errno));
返回0;
}
然后在pythonscript.py中:
#/usr/bin/python3
打印(“你好,世界!”)
这个技巧也适用于bash脚本。
不过,脚本必须是可执行的,所以请记住运行chmod+x pythonscript.py
编辑:
您可能需要使用管道或其他机制进行进程间通信。(这不是我的专长!)这里可以找到一个关于处理管道的实践方法的简短教程:您解决了这个问题吗?如果是,请将其标记为已关闭,或者如果我的回答对您有任何帮助,请发表评论。