Makefile 命令'make'如何链接到Python库? 一个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; }

Makefile 命令'make'如何链接到Python库? 一个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; },makefile,Makefile,当我运行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:(.t

当我运行
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
[86%]链接CXX可执行文件../../../../../bin/clang smartlog
CMakeFiles/clang smartlog.dir/src/smartlog.cpp.o:在函数“main”中:
SmartLog.cpp:(.text.startup.main+0x250):对“Py_Initialize”的未定义引用
SmartLog.cpp:(.text.startup.main+0x255):对的未定义引用
`Py_为初始值'
SmartLog.cpp:(.text.startup.main+0x121c):对的未定义引用
`皮伦_SimpleStringFlags'
SmartLog.cpp:(.text.startup.main+0x122a):对的未定义引用
`皮伦_SimpleStringFlags'
SmartLog.cpp:(.text.startup.main+0x1236):对的未定义引用
`PyString_FromString'
SmartLog.cpp:(.text.startup.main+0x1248):对的未定义引用
`PyImport_Import'
SmartLog.cpp:(.text.startup.main+0x1263):对的未定义引用
`PyModule_GetDict'
SmartLog.cpp:(.text.startup.main+0x1285):对的未定义引用
`PyDict_GetItemString'
SmartLog.cpp:(.text.startup.main+0x12a0):未定义对的引用
`可调用支票'
SmartLog.cpp:(.text.startup.main+0x35f5):未定义对的引用
`皮尤
CMakeFiles/clang smartlog.dir/src/FindLoggingBehavior.cpp.o:函数中
`FindLoggingVisitor::spiltWord(标准:_cxx11::基本_字符串)':
FindLoggingBehavior.cpp:
(.text._zn18; findlogging visitor9spiltwordenst7_cx1112基本_strings11char_tra
itsIcESaIcEEE+0x8c):对“PyObject\u CallFunction”的未定义引用
FindLoggingBehavior.cpp:
(.text._zn18; findlogging visitor9spiltwordenst7_cx1112基本_strings11char_tra
itsIcESaIcEEE+0xdc):对“PyList_GetItem”的未定义引用
FindLoggingBehavior.cpp:
(.text._zn18; findlogging visitor9spiltwordenst7_cx1112基本_strings11char_tra
itsIcESaIcEEE+0x101):对“PyString\u AsString”的未定义引用
collect2:错误:ld返回了1个退出状态
tools/clang/tools/SmartLog/CMakeFiles/clang SmartLog.dir/build.make:445:recipe
对于目标“bin/clangSmartLog”失败
生成[2]:***[bin/clang smartlog]错误1
CMakeFiles/Makefile2:26568:目标的配方
“tools/clang/tools/SmartLog/CMakeFiles/clang SmartLog.dir/all”失败
make[1]:***[tools/clang/tools/SmartLog/CMakeFiles/clang SmartLog.dir/all]
错误2
Makefile:149:目标“全部”的配方失败
make:**[全部]错误2
如何使
链接到Python库?

已解决。
我将
LINK\u库(“…/libpython2.7.so”)
添加到CmakeList中。

你能给我们看一下代码吗?我可以使用--ignore error选项吗?这不会神奇地解决未解析的引用。
make
并不特别关心或知道它正在处理什么语言或链接哪些库。您必须为链接器传入正确的选项。您是否在某处安装了所需的Python头?您的
Makefile
看起来是什么样子?它是否尝试推断头文件的位置(可能通过
/configure
或类似的自动化方式),还是只需要您手动传递正确的值?