1

我想使用嵌入在 c++ 应用程序中的 python。这是我的代码:

#include <Python.h>

int
main(int argc, char *argv[])
{
  Py_SetProgramName(argv[0]);  
  Py_Initialize();
  PyRun_SimpleString("from time import time,ctime\n"
                     "print 'Today is',ctime(time())\n");
  Py_Finalize();
  return 0;
}

我已包含在 c:\log\python27\include 中以获取 Python.h 以及 c:\log\python27\libs 以具有安装在 c:\log\python27 中的相应 python 链接库它正在编译但不是链接...为什么?我总是有以下错误:

main.o:main.cpp:(.text+0x17): undefined reference to `_imp__Py_SetProgramName'
main.o:main.cpp:(.text+0x1e): undefined reference to `_imp__Py_Initialize'
main.o:main.cpp:(.text+0x34): undefined reference to `_imp__PyRun_SimpleStringFlags'
main.o:main.cpp:(.text+0x3b): undefined reference to `_imp__Py_Finalize'
4

1 回答 1

0

如果您使用的是 python3.5,您可以使用这个简单的命令

g++  test.cpp -I/usr/include/python3.5 -lpython3.5m -o call_function

取决于你的图书馆在哪里!

于 2021-03-06T13:49:49.110 回答