注意了,我在官方文档中也没找到编译的命令,很多网上的demo也仅仅列出代码不告诉你如何编译,经过实验编译至少应当是这样的:
g++ ccallpython.cpp `python3-config --cflags` `python3-config --ldflags`
如果不加python3-config --cflags
python3-config --ldflags
这两个选项,编译就会出问题,例如在我的实验环境里:
~$ g++ ccallpython.cpp
/tmp/ccOuCjpu.o: In function `main':
ccallpython.cpp:(.text+0x19): undefined reference to `Py_Initialize'
ccallpython.cpp:(.text+0x1e): undefined reference to `Py_IsInitialized'
ccallpython.cpp:(.text+0x5c): undefined reference to `PyRun_SimpleStringFlags'
ccallpython.cpp:(.text+0x6b): undefined reference to `PyRun_SimpleStringFlags'
ccallpython.cpp:(.text+0x7a): undefined reference to `PyRun_SimpleStringFlags'
ccallpython.cpp:(.text+0x89): undefined reference to `PyRun_SimpleStringFlags'
ccallpython.cpp:(.text+0x98): undefined reference to `PyRun_SimpleStringFlags'
ccallpython.cpp:(.text+0xca): undefined reference to `PyImport_ImportModule'
ccallpython.cpp:(.text+0x107): undefined reference to `PyModule_GetDict'
ccallpython.cpp:(.text+0x149): undefined reference to `PyDict_GetItemString'
ccallpython.cpp:(.text+0x160): undefined reference to `PyCallable_Check'
ccallpython.cpp:(.text+0x1a4): undefined reference to `PyTuple_New'
ccallpython.cpp:(.text+0x1bc): undefined reference to `Py_BuildValue'
ccallpython.cpp:(.text+0x1d0): undefined reference to `PyTuple_SetItem'
ccallpython.cpp:(.text+0x1e3): undefined reference to `PyObject_CallObject'
ccallpython.cpp:(.text+0x210): undefined reference to `PyArg_Parse'
ccallpython.cpp:(.text+0x24c): undefined reference to `PyDict_GetItemString'
ccallpython.cpp:(.text+0x263): undefined reference to `PyCallable_Check'
ccallpython.cpp:(.text+0x2b0): undefined reference to `PyObject_CallObject'
ccallpython.cpp:(.text+0x2dc): undefined reference to `PyArg_Parse'
ccallpython.cpp:(.text+0x398): undefined reference to `Py_Finalize'
collect2: error: ld returned 1 exit status
然而,如果python版本和gcc版本不对的话,还会出现别的问题,以python3.7为例,会提示gcc没有一些选项:
~$ g++ ccallpython.cpp `python3-config --cflags` `python3-config --ldflags`
g++: error: unrecognized command line option ‘-fno-plt’
error: unknown argument: '-flto-partition=none'
实际上,有一些bash使用经验的人都知道,xxx
里的xxx会当成命令来执行,因此在终端里输入python3-config --cflags就能看到输出,实际上一些编译选项,不同python版本的编译选项不太相同:
~$ python3.7-config --cflags
-I/home/xxx/anaconda3/include/python3.7m -I/home/xxx/anaconda3/include/python3.7m -Wno-unused-result -Wsign-compare -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O3 -pipe -fdebug-prefix-map==/usr/local/src/conda/- -fdebug-prefix-map==/usr/local/src/conda-prefix -fuse-linker-plugin -ffat-lto-objects -flto-partition=none -flto -flto -fuse-linker-plugin -ffat-lto-objects -flto-partition=none -DNDEBUG -fwrapv -O3 -Wall
~$ python3.5-config --cflags
-I/usr/include/python3.5m -I/usr/include/python3.5m -Wno-unused-result -Wsign-compare -g -fstack-protector-strong -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
当你搜索错误后就会大体知道,解决方法都是升级gcc版本,升级到gcc7,选项-fno-plt等选项是更新的gcc版本才支持的。对于clang编译器也是一样的。
另外网上有些说加上-I(大写字母)和-l(小写字母)选项的,我试了下有些选项的使用是错误的,即编译器认为是使用错误的,提示正确的Usage。
还有一种情况,如果能编译,但是运行的时候出现segment fault (core dump)很有可能是Py_Initialize();没通过,具体可以通过插桩式打log或者调试器发现具体出问题的地方,一般来说,这种问题也是因为版本不对的原因。Python提供的这些C接口没有太多的错误提醒(异常处理),因此出问题了要自己动手调试查找。