Undefined Python references in C++ using CMake

ぃ、小莉子 提交于 2019-12-02 07:46:38

You misunderstand CMake's way: before use something you ought to find it! I.e. make sure that everything you need to build your package is available and usable at build host. Otherwise that would be not good to waste a (compile) time (say 2 hours) and then get an error that some header/library/executable not found. So, at CMake run time you'd better to be sure that everything you need is here. To do so, CMake have a lot of tools.

Consider your particular case: you need to find Python libraries otherwise build is not possible. To do so, you ought to use find_package like this:

find_package(PythonLibs REQUIRED)

Take a look to documentation and provide other options (like version) if you need. You shouldn't use hardcoded paths in your CMakeLists.txt, otherwise your project wouldn't be really portable (and most probably you'll be the only who can build it w/o a lot of problems). Instead Python libs finder module will provide variables you need to use later, or failed w/ error if nothing has found.

If CMake ends w/o errors, you may use found Python libs. First of all you need to update #include paths:

 include_directories(${PYTHON_INCLUDE_DIRS})

Then tell to linker that your executable projectname needs to be linked w/ Python libs:

add_executable(projectname ${SOURCE_FILES})
target_link_libraries(projectname ${PYTHON_LIBRARIES})

And again, try to avoid to modify CMAKE_CXX_FLAGS (and others) directly -- there are bunch of calls to do that globally and/or per target. Some of them are:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!