我试图在C++中嵌入Python代码,并在Conda环境中使用这些包。我有:
// main.cpp
#include <Python.h>
int main(int argc, char *argv[]) {
Py_Initialize();
return 0;
}在CMakeLists.txt中,我补充道:
find_package(Python3 COMPONENTS Interpreter Development)我使用Conda (称为venv) active运行cmake。当我试图编译时,我得到:
/home/myself/.conda/envs/venv/bin/python3.7 (found version "3.7.7") found components: Interpreter Development但是当我运行make时,我得到了:fatal error: Python.h: No such file or directory。于是我做了一个locate Python.h,找到了几个Python.h文件:
/home/myself/.conda/envs/venv/include/python3.7m/Python.h
/home/myself/.conda/envs/venv/lib/python3.7/site-packages/tensorflow/include/external/local_config_python/python_include/Python.h
/home/myself/.conda/pkgs/python-3.7.7-hcff3b4d_5/include/python3.7m/Python.h我尝试用#include <Python.h>替换main.cpp中的#include <PATH>,其中PATH被上面列出的路径之一替换。在这三种情况下,我都得到了一个新的错误:
undefined reference to 'Py_Initialize'有人能指出我在这里错过了什么吗?另外,我在这台机器上没有sudo权限。
更新:这个问题和解决方案是针对cmake1.13.5版的。正如Guillame所指出的,对于不同版本的cmake,解决方案可能有所不同。
发布于 2021-05-07 19:49:10
在使用find_package时,还必须将其链接到目标:
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
add_executable(main main.cpp)
# Adds the proper include directories and link to libraries
target_link_libraries(main PUBLIC Python3::Python)关于CMake如何工作、目标和导入的目标如何工作的文档,请参阅cmake-buildsystem(7)。
要知道导入时应该做什么,请参考该模块的文档。例如,下面是FindPython3的文档。有一个可以链接到的所有目标和可以找到的所有组件的列表。
https://stackoverflow.com/questions/67440802
复制相似问题