CMake不链接Python [英] CMake not linking Python

查看:81
本文介绍了CMake不链接Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果我有一个重复的问题,但我只是无法在Internet上的任何地方找到解决方案,但是我相信这是一个非常简单的问题。

Sorry if I'm duplicating a question, but I just cannot find the solution to what I'm looking for anywhere on the internet, yet I believe that this is a very simple problem.

我正在尝试使用一些自定义C ++库扩展python,并使用CMake构建我的C ++库。我正在按照 https://docs.python.org/2/extending/ extended.html ,但编译不正确。

I'm trying to extend python with some custom C++ libraries, and building my C++ libraries with CMake. I'm following the instructions on https://docs.python.org/2/extending/extending.html, but it's not compiling correctly.

当我尝试构建它时,出现以下消息:

When I try to build it, I get these messages:

"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现在的样子:

Based on this, I suspect that this is a problem with the way I'm linking things in the CMakeLists.txt file, but I have no idea how to do it properly. This is what my CMakeLists.txt looks like right now:

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。

How in the world do I get this thing to compile correctly? I am running Windows 7 64-bit, and using CLion as my IDE.

推荐答案

您的第一个问题是您正在使用 target_link_libraries 错误:您应该向其传递添加链接的目标,然后向其传递要链接的库:

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)

第二个问题是您正在构建可执行文件,而不是共享库。如果要使扩展名可以从python访问,则必须是一个库。

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

但是现在来了一个好消息:如果您使用内置CMake模块 FindPythonLibs.cmake 。要构建python模块,您只需执行以下操作:

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

这篇关于CMake不链接Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆