CMake not linking Python

荒凉一梦 提交于 2019-12-04 16:19:36

Your first problem is that you are using target_link_libraries wrong: you should pass it the target to which to add a link and then the library you want to link in:

target_link_libraries(parsers python2.7)

Your second problem is that you are building an executable, instead of a shared library. If you want to make your extension accessible from python it needs to be a library.

add_library(parsers SHARED ${SOURCE_FILES})

But now comes the good news: your life becomes much simpler (and more portable) if you use the built in CMake module FindPythonLibs.cmake. To build a python module you would only need to do the following:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(PythonLibs REQUIRED)

add_library(parsers SHARED ${SOURCE_FILES})
include_directories(${PYTHON_INCLUDE_DIRS})
target_link_libraries(parsers ${PYTHON_LIBRARIES})

You are using target_link_libraries() wrong. Check the docs; you probably want something like:

add_executable(parsers ${SOURCE_FILES})
target_link_libraries(parsers python2.7)

Note that the output from CMake should already tell you that something is wrong. On my machine:

CMake Error at CMakeLists.txt:8 (target_link_libraries):
  Cannot specify link libraries for target "python2.7" which is not built by
  this project.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!