CMake不链接Python

2024-05-17 05:05:07 发布

您现在位置: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 `WinMain@16'
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})

我怎么才能把这个东西正确编译呢?我正在运行Windows764位,并使用CLion作为我的IDE。


Tags: cmaketargetformakemaindirrecipeerror