[英]How to Compile C++ with embedded Python
I have written a Hello world program in C++ with embedded python.我在 C++ 中编写了一个带有嵌入式 python 的 Hello world 程序。 I am learning how to embed python code in a C++ Program.
我正在学习如何在 C++ 程序中嵌入 python 代码。 But when I try to compile my program with the normal method it throws errors.
但是当我尝试用普通方法编译我的程序时,它会抛出错误。 I think there is a different way to do the compilation.
我认为有一种不同的方式来进行编译。
I am using Windows 10 so please don't suggest me any linux command.我正在使用 Windows 10 所以请不要向我建议任何 linux 命令。
Here is my code这是我的代码
#define PY_SSIZE_T_CLEAN
#include <C:\Users\super\AppData\Local\Programs\Python\Python310\include\Python.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
wchar_t *program = Py_DecodeLocale(argv[0], nullptr);
if (program == nullptr)
{
cerr << "Failed to decode argv[0]" << endl;
exit(1);
}
Py_SetProgramName(program);
Py_Initialize();
PyRun_SimpleString("print('Hellow World')");
if (Py_FinalizeEx() < 0)
{
cerr << "Failed to finalize" << endl;
exit(120);
}
PyMem_RawFree(program);
return 0;
}
Here are my errors这是我的错误
Executing task: C/C++: g++.exe build active file
Starting build...
C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g E:\cpp\main.cpp -o E:\cpp\main.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\super\AppData\Local\Temp\cc0M2Uyj.o:E:\cpp/main.cpp:9: undefined reference to `__imp_Py_DecodeLocale'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\super\AppData\Local\Temp\cc0M2Uyj.o: in function `main':
E:\cpp/main.cpp:15: undefined reference to `__imp_Py_SetProgramName'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:\cpp/main.cpp:17: undefined reference to `__imp_Py_Initialize'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:\cpp/main.cpp:19: undefined reference to `__imp_PyRun_SimpleStringFlags'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:\cpp/main.cpp:21: undefined reference to `__imp_Py_FinalizeEx'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:\cpp/main.cpp:27: undefined reference to `__imp_PyMem_RawFree'
collect2.exe: error: ld returned 1 exit status
Build finished with error(s).
* The terminal process terminated with exit code: -1.
* Terminal will be reused by tasks, press any key to close it.
To compile the program, I just clicked the run button in vs code and it showed those errors.要编译程序,我只需单击 vs 代码中的运行按钮,它就会显示这些错误。
I tried the same using cygwin and python at localhost.我在本地主机上使用 cygwin 和 python 尝试了相同的操作。 Executed following commands:
执行了以下命令:
$ # /cygdrive/c/cygwin64/bin/libpython3.8.dll
$ ln /cygdrive/c/cygwin64/bin/libpython3.8.dll /usr/lib/libpython3.8.dll
$ g++.exe 73258055.cpp -L/usr/lib -lpython3.8 -o ./a.out
$ ./a.out
C:/cygwin/home/murugesanopenssl/stackoverflow/73258055/a.out: error while loading shared libraries: libpython3.8.dll: cannot open shared object file: No such file or directory
$ cp /usr/lib/libpython3.8.dll .
$ ./a.out
Hellow World
$ sed -i "s/Hellow World/Hello World\\\\\\\\nRegards,\\\\\\\nmurugesan openssl/;" 73258055.cpp
$ g++.exe 73258055.cpp -L/usr/lib -lpython3.8 -o ./a.out
$ ./a.out
Hello World
Regards,
murugesan openssl
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.