Undefined Python references in C++ using CMake

后端 未结 1 423
我在风中等你
我在风中等你 2021-01-28 00:08

I am trying to compile a c++ project referencing Python using CMake. I am using Cygwin and I have Python2.7 source files in Cygwin.

For example:

PyObjec         


        
相关标签:
1条回答
  • 2021-01-28 00:58

    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:

    • add_definitions to define/undefine macros
    • include_directories to update #include paths
    • add_compile_options to add other compiler options
    • link_directories to update linker search paths
    0 讨论(0)
提交回复
热议问题