我正在将 Python 嵌入到 C++ 项目中,示例代码来自 https://docs.python.org/3/extending/embedding.html#very-high-level-embedding:
CMakeLists.txt
:
cmake_minimum_required(VERSION 3.20)
project(python_in_cpp)
set(CMAKE_CXX_STANDARD 11)
# sudo apt install libpython3.11-dev
find_package(PythonLibs 3.11 REQUIRED)
find_package(Python COMPONENTS Interpreter Development)
message("Python_FOUND:${Python_FOUND}")
message("Python_VERSION:${Python_VERSION}")
message("Python_Development_FOUND:${Python_Development_FOUND}")
message("Python_LIBRARIES:${Python_LIBRARIES}")
include_directories(${Python_INCLUDE_DIRS})
link_directories(
${Python3_LIBRARY_DIRS}
${Python3_RUNTIME_LIBRARY_DIRS}
)
link_libraries(${Python3_LIBRARIES})
add_executable(python_in_cpp main.cpp)
main.cpp
:
#define PY_SSIZE_T_CLEAN
#include <Python.h>
int
main(int argc, char *argv[])
{
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
Py_SetProgramName(program); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
if (Py_FinalizeEx() < 0) {
exit(120);
}
PyMem_RawFree(program);
return 0;
}
CMake 日志显示
Python
和 PythonLibs
都找到了,但是在编译时,我收到了关于几个未定义引用的错误:
main.cpp:(.text+0x1f): undefined reference to `Py_DecodeLocale'
/usr/bin/ld: main.cpp:(.text+0x63): undefined reference to `Py_SetProgramName'
/usr/bin/ld: main.cpp:(.text+0x68): undefined reference to `Py_Initialize'
/usr/bin/ld: main.cpp:(.text+0x7c): undefined reference to `PyRun_SimpleStringFlags'
/usr/bin/ld: main.cpp:(.text+0x81): undefined reference to `Py_FinalizeEx'
/usr/bin/ld: main.cpp:(.text+0x9e): undefined reference to `PyMem_RawFree'
我该如何修复它来构建这个项目?
我安装了几个python:
$ apt list --installed | grep "lib*python"
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
libpython3-dev/stable,now 3.11.2-1+b1 amd64 [installed]
libpython3-stdlib/stable,now 3.11.2-1+b1 amd64 [installed,automatic]
libpython3.11-dev/stable-security,now 3.11.2-6+deb12u3 amd64 [installed]
libpython3.11-minimal/stable-security,now 3.11.2-6+deb12u3 amd64 [installed,automatic]
libpython3.11-stdlib/stable-security,now 3.11.2-6+deb12u3 amd64 [installed,automatic]
libpython3.11/stable-security,now 3.11.2-6+deb12u3 amd64 [installed,automatic]
这是一个愚蠢的错字。您打印了
Python_LIBRARIES
,但在 Python3_LIBRARIES
语句中使用了 link_libraries
。
为了避免将来出现此类问题,您应该更喜欢使用模块提供的导入目标。
您可以用这一行替换您的三个语句(
include_directories
、link_directories
和link_libraries
):
target_link_libraries(python_in_cpp Python::Python)
确保将其放在 after
add_executable
声明中。