我正在转换一个项目以停止使用Visual Studio 项目文件并切换到使用cmake
.
该项目已经有几年的历史了,并且使用了位于代码树之外的自定义预编译外部库:
src/
... 源码树 ...
externalLibs/
Python27/
include/
... 头文件 ...
libs /
python27.lib
python27_d.lib
cmake/
Modules/
FindPython2.cmake
我所做的是添加目录cmake/Modules
,并在其中添加文件 FindPython2.cmake
。
然后,我创建了CMakeLists.txt
项目组件之一:
cmake_minimum_required( VERSION 3.17 )
project( ConnectorTests CXX )
include( ../../../../../global.cmake )
set( CMAKE_MODULE_PATH "${ROOT_PATH}/cmake/Modules" ${CMAKE_MODULE_PATH} )
message( "CMAKE_MODULE_PATH:" "${CMAKE_MODULE_PATH}" )
find_package( Python2 REQUIRED )
add_executable( ConnectorTests pch.h pch.cpp Tests.cpp )
target_include_directories( ConnectorTests PRIVATE
../../Connector
../../MtoUtils
"${Python2_INCLUDE_DIRS}"
)
target_link_libraries( ConnectorTests PRIVATE Python2 )
以上只是相关部分的摘录。
include( ../../../../../global.cmake
我在路径定义中包含了一个全局变量set( CMAKE_MODULE_PATH "${ROOT_PATH}/cmake/Modules"$ {CMAKE_MODULE_PATH} )
(find_package( Python2 REQUIRED )
我放了一个message( )
看看一切是否正常,它向我展示了我的期望:
CMAKE_MODULE_PATH: E:/cmake/Modules
我的FindPython2.cmake
模块是这样的:
add_library( Python2 SHARED IMPORTED )
if( WIN32 )
set_target_properties( Python2 PROPERTIES IMPORTED_LOCATION_RELEASE "${ROOT_PATH}/externalLibs/Python27/libs/python27.lib" )
set_target_properties( Python2 PROPERTIES IMPORTED_LOCATION_DEBUG "${ROOT_PATH}/externalLibs/Python27/libs/python27_d.lib" )
endif( )
set( Python2_FOUND ON )
set( Python2_Development_FOUND ON )
set( Python2_Development.Module_FOUND ON )
set( Python2_Development.Embed_FOUND ON )
set( Python2_INCLUDE_DIRS "${ROOT_PATH}/externalLibs/Python27/include" )
我生成项目没有问题:
cmake -B build -T v141, host = x86 -A win32
但是在编译时:
cmake --build build
LINK : 致命错误 LNK1104: no se puede abrir el archivo 'Python2-NOTFOUND.obj' [E:\Desarrollo\PATH\build\Test\ConnectorTests\ConnectorTests.vcxproj]
我在用着:
cmake --version
cmake 版本 3.17.20032601-MSVC_2
- 我究竟做错了什么 ?
- 我该如何解决?