我将尝试在使用CMake编译的C++项目中包含一个Python文件。
首先,我使用这两个文件进行了独立测试:
#include <Python.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
setenv("PYTHONPATH", ".", 0);
char hostname[] = "localhost";
PyObject *pName, *pModule, *pFunc;
PyObject *pArgs, *pValue;
Py_Initialize();
pName = PyString_FromString("GetHostname");
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if(pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule, "GetHostname");
if (pFunc && PyCallable_Check(pFunc)) {
pArgs = PyTuple_New(1);
pValue = PyString_FromString(hostname);
PyTuple_SetItem(pArgs, 0, pValue);
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL) {
printf("The IP address is %s\n", PyString_AsString(pValue));
Py_DECREF(pValue);
}
else {
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr, "Call Failed\n");
return 1;
}
}
else {
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function\n");
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load file\n");
return 1;
}
Py_Finalize();
return 0;
}
而且
import socket
def GetHostname(hostname):
addr = socket.gethostbyname(hostname)
return addr
当我使用编译时
g++ $(python-config --cflags) -o test $(python-config --ldflags) ./test.cpp
来自如何将Python静态库与我的C++程序链接,一切都很顺利。
但是这个项目是使用CMake编译的,我一定做错了什么,因为在编译后我得到了以下错误:
Traceback (most recent call last):
File "/src/GetHostname.py", line 1, in <module>
import socket
File "/usr/lib64/python2.6/socket.py", line 46, in <module>
import _socket
ImportError: /usr/lib64/python2.6/lib-dynload/_socketmodule.so: undefined symbol: PyExc_ValueError
在CMakeLists.txt中,我添加了这些行
find_package( PythonInterp REQUIRED )
find_package( PythonLibs REQUIRED )
include_directories ( ${PYTHON_INCLUDE_DIRS} )
add_library (GetHostname MODULE GetHostname.cc)
target_link_libraries(GetHostname ${PYTHON_LIBRARIES})
CONFIGURE_FILE(${PATH_TO_SOURCE}GetHostname.py ${PATH_TO_BUILD}GetHostname.py COPYONLY)
基于这个主题 Python.h: No such file or directory 一切都编译正常,但是由于错误而无法加载python模块。我在CMake中没有正确链接python库吗?
欢迎任何思路来解释为什么会失败。
使用Python 2.6
我知道这可以用C++来完成,但这不是我想要的答案,因为我需要包含的不止一个python模块。此外,我现在知道本地主机的IP地址,这只是为了测试目的。