命令“ make”如何链接到Python库?

时间:2018-10-05 02:38:52

标签: makefile

C ++项目文件xxx.cpp调用了python函数,如下所示:

  if(FindLoggingBehavior){

    // Initialize python module
    Py_Initialize();
    if(!Py_IsInitialized()){
        return 0;
    }
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append('/Users/abc/source/script/segment/')");
    pName = PyString_FromString("segment");
    pModule = PyImport_Import(pName);
    if(!pModule){
        printf("can't find segment.py");
        return 0;
    }
    pDict = PyModule_GetDict(pModule);
    if(!pDict){
        printf("can't find dict");
        return 0;
    }
    pFunc = PyDict_GetItemString(pDict, "segment");
    if(!pFunc || !PyCallable_Check(pFunc)){
        printf("can't find function [segment]");
        return 0;
    }

当我运行make来编译项目时,发生了以下错误:

[ 86%] Linking CXX executable ../../../../bin/clang-smartlog
CMakeFiles/clang-smartlog.dir/src/SmartLog.cpp.o: In function `main':
SmartLog.cpp:(.text.startup.main+0x250): undefined reference to `Py_Initialize'
SmartLog.cpp:(.text.startup.main+0x255): undefined reference to 
`Py_IsInitialized'
SmartLog.cpp:(.text.startup.main+0x121c): undefined reference to 
`PyRun_SimpleStringFlags'
 SmartLog.cpp:(.text.startup.main+0x122a): undefined reference to 
`PyRun_SimpleStringFlags'
SmartLog.cpp:(.text.startup.main+0x1236): undefined reference to 
`PyString_FromString'
SmartLog.cpp:(.text.startup.main+0x1248): undefined reference to 
`PyImport_Import'
SmartLog.cpp:(.text.startup.main+0x1263): undefined reference to 
`PyModule_GetDict'
SmartLog.cpp:(.text.startup.main+0x1285): undefined reference to 
`PyDict_GetItemString'
SmartLog.cpp:(.text.startup.main+0x12a0): undefined reference to 
`PyCallable_Check'
SmartLog.cpp:(.text.startup.main+0x35f5): undefined reference to 
`Py_Finalize'
CMakeFiles/clang-smartlog.dir/src/FindLoggingBehavior.cpp.o: In function 
`FindLoggingVisitor::spiltWord(std::__cxx11::basic_string<char, 
std::char_traits<char>, std::allocator<char> >)':
FindLoggingBehavior.cpp: 

 (.text._ZN18FindLoggingVisitor9spiltWordENSt7__cxx1112basic_stringIcSt11char_tra 
itsIcESaIcEEE+0x8c): undefined reference to `PyObject_CallFunction'
FindLoggingBehavior.cpp: 
  (.text._ZN18FindLoggingVisitor9spiltWordENSt7__cxx1112basic_stringIcSt11char_tra 
itsIcESaIcEEE+0xdc): undefined reference to `PyList_GetItem'
FindLoggingBehavior.cpp: 
 (.text._ZN18FindLoggingVisitor9spiltWordENSt7__cxx1112basic_stringIcSt11char_tra 
itsIcESaIcEEE+0x101): undefined reference to `PyString_AsString'
collect2: error: ld returned 1 exit status
tools/clang/tools/SmartLog/CMakeFiles/clang-smartlog.dir/build.make:445: recipe 
for target 'bin/clang-smartlog' failed
make[2]: *** [bin/clang-smartlog] Error 1
CMakeFiles/Makefile2:26568: recipe for target 
'tools/clang/tools/SmartLog/CMakeFiles/clang-smartlog.dir/all' failed
make[1]: *** [tools/clang/tools/SmartLog/CMakeFiles/clang-smartlog.dir/all] 
Error 2
Makefile:149: recipe for target 'all' failed
make: *** [all] Error 2

make如何链接到Python库?

1 个答案:

答案 0 :(得分:0)

已解决。 我将LINK_LIBRARIES(".../libpython2.7.so")添加到CMakeLists。