2015-03-02 62 views
0

很抱歉,如果我複製了一個問題,但我無法找到我在互聯網上任何地方尋找解決方案,但我相信這是一個非常簡單的問題。CMake沒有鏈接Python

我想用一些自定義C++庫擴展python,並用CMake構建我的C++庫。我按照https://docs.python.org/2/extending/extending.html上的說明操作,但它沒有正確編譯。

當我嘗試構建它,我得到的消息:

"C:\Program Files (x86)\JetBrains\CLion 140.2310.6\bin\cmake\bin\cmake.exe" --build C:\Users\pkim2\.clion10\system\cmake\generated\76c451cd\76c451cd\Debug --target parsers -- -j 8 
Linking CXX executable parsers.exe 
CMakeFiles\parsers.dir/objects.a(main.cpp.obj): In function `spam_system': 
C:/code/ground-trac/ground/launch/trunk/Software Support/Data Analysis Scripts/data_review_automation/parsers/main.cpp:9: undefined reference to `_imp__PyArg_ParseTuple' 
C:/code/ground-trac/ground/launch/trunk/Software Support/Data Analysis Scripts/data_review_automation/parsers/main.cpp:12: undefined reference to `_imp__Py_BuildValue' 
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libmingw32.a(main.o):(.text.startup+0xa7): undefined reference to `[email protected]' 
collect2.exe: error: ld returned 1 exit status 
CMakeFiles\parsers.dir\build.make:87: recipe for target 'parsers.exe' failed 
CMakeFiles\Makefile2:59: recipe for target 'CMakeFiles/parsers.dir/all' failed 
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/parsers.dir/rule' failed 
mingw32-make.exe[3]: *** [parsers.exe] Error 1 
mingw32-make.exe[2]: *** [CMakeFiles/parsers.dir/all] Error 2 
mingw32-make.exe[1]: *** [CMakeFiles/parsers.dir/rule] Error 2 
mingw32-make.exe: *** [parsers] Error 2 
Makefile:109: recipe for target 'parsers' failed 

在此基礎上,我懷疑這是用我的東西聯繫起來的的CMakeLists.txt文件的方式有問題,但我不知道如何正確地做到這一點。這就是我的CMakeLists.txt現在看起來像:

cmake_minimum_required(VERSION 2.8.4) 
project(parsers) 

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 
set(SOURCE_FILES main.cpp) 
include_directories(C:\\Python27\\include) 
link_directories(C:\\Python27\\) 
target_link_libraries(python2.7) 
add_executable(parsers ${SOURCE_FILES}) 

在世界上我怎麼得到這個東西編譯正確?我正在運行Windows 7 64位,並使用CLion作爲我的IDE。

+0

我通常只是一聲我的頭撞在牆上了幾個小時,然後放棄 – 2015-03-02 18:55:56

+0

我想這樣做,太:(... – 2015-03-02 19:41:10

回答

0

您正在使用target_link_libraries()錯誤。檢查the docs;你可能想要這樣的東西:

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

請注意,從CMake的輸出應該已經告訴你,什麼是錯的。在我的機器:

CMake Error at CMakeLists.txt:8 (target_link_libraries): 
    Cannot specify link libraries for target "python2.7" which is not built by 
    this project. 
2

你的第一個問題是,你正在使用target_link_libraries錯誤:你應該通過它,你要鏈接的目標要添加一個鏈接,然後庫:

target_link_libraries(parsers python2.7) 

你的第二個問題是你正在構建一個可執行文件,而不是共享庫。如果你想讓你的擴展可以從python訪問,它需要成爲一個庫。

add_library(parsers SHARED ${SOURCE_FILES}) 

但現在來了一個好消息:你的生活變得更簡單(更便攜),如果你使用內置的CMake模塊FindPythonLibs.cmake。要構建一個Python模塊,你只需要做到以下幾點:

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}) 
+0

噢,非常感謝。現在它抱怨找不到PythonLibs(缺少:PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) – 2015-03-02 22:12:18