CMake 有条件发布、调试等
CMake conditional for Release,Debug and Etc
我正在开发一个 CMake 项目,该项目需要在 linking 过程中为静态库的每种配置类型(例如,RELEASE、DEBUG、MINSIZEREL 和 RELWITHDEBINFO)设置特定路径。因为我有不同版本的静态库用于 Debug 和 Release 版本。
到目前为止,我已在下面完成此操作,以便为我尝试 link:
的静态库设置文件夹“release”或“debug”
# Here we need to determine our build type and set the each one specific 3rd library for our binary
if( CMAKE_BUILD_TYPE STREQUAL "Debug")
#set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/Install)
#message( STATUS "CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" )
set(THIRDLIBS_BUILD_TYPE "debug")
elseif( CMAKE_BUILD_TYPE STREQUAL "Release")
#SET(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/Install.deb)
#message( STATUS "CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" )
set(THIRDLIBS_BUILD_TYPE "release")
elseif( CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
#SET(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/Install.deb)
#message( STATUS "CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" )
set(THIRDLIBS_BUILD_TYPE "debug")
elseif( CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
#SET(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/Install.deb)
#message( STATUS "CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" )
set(THIRDLIBS_BUILD_TYPE "release")
else()
MESSAGE( STATUS "CMAKE_BUILD_TYPE not set yet ${CMAKE_BUILD_TYPE}" )
endif()
if (WIN32)
set(THIRD_LIBS
../external_libs/mylibalpha/win64/${THIRDLIBS_BUILD_TYPE}/libalpha
../external_libs/mylibbeta/win64/${THIRDLIBS_BUILD_TYPE}/libbeta
)
endif(WIN32)
但是,在我的 Visual Studio 中,我注意到在每个项目构建类型(RELEASE、DEBUG、MINSIZEREL 和 RELWITHDEBINFO)中,${THIRDLIBS_BUILD_TYPE} 变量都是空的并且没有正确设置。我基本上在 linking 上收到一个错误,显示找不到 external_libs/mylibalpha/win64//libalpha.lib 我应该得到这样的东西 external_libs/mylibalpha/win64/发布/libalpha.lib
我的 CMakeLists.txt 脚本有什么问题?
变量 CMAKE_BUILD_TYPE
对多配置生成器没有意义,Visual Studio 就是其中之一。
在 CMake 中定义一个 pre-built 库的规范方法,该库针对不同的配置具有不同的位置是创建具有多个要设置的属性 IMPORTED_LOCATION_<CONFIG> 的 IMPORTED 库目标:
add_library(thirdPartyLib IMPORTED UNKNOWN)
set_target_properties(thirdPartyLib PROPERTIES
IMPORTED_LOCATION_RELEASE
${CMAKE_CURRENT_SOURCE_DIR}/external_libs/mylibalpha/win64/release/libalpha.a
IMPORTED_LOCATION_DEBUG
${CMAKE_CURRENT_SOURCE_DIR}/external_libs/mylibalpha/win64/debug/libalpha.a
)
然后您需要在主项目的构建类型和导入目标的构建类型之间创建一个映射。
这样的映射可以在 per-target 的基础上创建,通过设置属性 MAP_IMPORTED_CONFIG_<CONFIG>:
set_target_properties(thirdPartyLib PROPERTIES
# For Debug version of the project use DEBUG-suffixed library location
MAP_IMPORTED_CONFIG_DEBUG DEBUG
# For Release version of the project use RELEASE-suffixed library location
MAP_IMPORTED_CONFIG_RELEASE RELEASE
# For ReleaseWithDebInfo version of the project use DEBUG-suffixed library location
MAP_IMPORTED_CONFIG_RELWITHDEBINFO DEBUG
# For MinSizeRel version of the project use RELEASE-suffixed library location
MAP_IMPORTED_CONFIG_MINSIZEREL RELEASE
)
或者,可以通过设置变量 CMAKE_MAP_IMPORTED_CONFIG_<CONFIG> 为 所有 IMPORTED 目标创建映射。请注意,这些变量应在 在 创建 IMPORTED 目标之前设置。
# For Debug version of the project use DEBUG-suffixed locations of IMPORTED targets
set(CMAKE_MAP_IMPORTED_CONFIG_DEBUG DEBUG)
# For Release version of the project use RELEASE-suffixed locations of IMPORTED targets
set(CMAKE_MAP_IMPORTED_CONFIG_RELEASE RELEASE)
# For ReleaseWithDebInfo version of the project use DEBUG-suffixed locations of IMPORTED targets
set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO DEBUG)
# For MinSizeRel version of the project use RELEASE-suffixed locations of IMPORTED targets
set(CMAKE_MAP_IMPORTED_CONFIG_MINSIZEREL RELEASE)
有了 IMPORTED 库和配置映射,使用这些库非常简单:
target_link_libraries(my_exe PRIVATE thirdPartyLib)
根据项目的构建类型,CMake 将自动 select 用于链接的适当库。
我正在开发一个 CMake 项目,该项目需要在 linking 过程中为静态库的每种配置类型(例如,RELEASE、DEBUG、MINSIZEREL 和 RELWITHDEBINFO)设置特定路径。因为我有不同版本的静态库用于 Debug 和 Release 版本。
到目前为止,我已在下面完成此操作,以便为我尝试 link:
的静态库设置文件夹“release”或“debug”# Here we need to determine our build type and set the each one specific 3rd library for our binary
if( CMAKE_BUILD_TYPE STREQUAL "Debug")
#set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/Install)
#message( STATUS "CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" )
set(THIRDLIBS_BUILD_TYPE "debug")
elseif( CMAKE_BUILD_TYPE STREQUAL "Release")
#SET(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/Install.deb)
#message( STATUS "CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" )
set(THIRDLIBS_BUILD_TYPE "release")
elseif( CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
#SET(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/Install.deb)
#message( STATUS "CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" )
set(THIRDLIBS_BUILD_TYPE "debug")
elseif( CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
#SET(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/Install.deb)
#message( STATUS "CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" )
set(THIRDLIBS_BUILD_TYPE "release")
else()
MESSAGE( STATUS "CMAKE_BUILD_TYPE not set yet ${CMAKE_BUILD_TYPE}" )
endif()
if (WIN32)
set(THIRD_LIBS
../external_libs/mylibalpha/win64/${THIRDLIBS_BUILD_TYPE}/libalpha
../external_libs/mylibbeta/win64/${THIRDLIBS_BUILD_TYPE}/libbeta
)
endif(WIN32)
但是,在我的 Visual Studio 中,我注意到在每个项目构建类型(RELEASE、DEBUG、MINSIZEREL 和 RELWITHDEBINFO)中,${THIRDLIBS_BUILD_TYPE} 变量都是空的并且没有正确设置。我基本上在 linking 上收到一个错误,显示找不到 external_libs/mylibalpha/win64//libalpha.lib 我应该得到这样的东西 external_libs/mylibalpha/win64/发布/libalpha.lib
我的 CMakeLists.txt 脚本有什么问题?
变量 CMAKE_BUILD_TYPE
对多配置生成器没有意义,Visual Studio 就是其中之一。
在 CMake 中定义一个 pre-built 库的规范方法,该库针对不同的配置具有不同的位置是创建具有多个要设置的属性 IMPORTED_LOCATION_<CONFIG> 的 IMPORTED 库目标:
add_library(thirdPartyLib IMPORTED UNKNOWN)
set_target_properties(thirdPartyLib PROPERTIES
IMPORTED_LOCATION_RELEASE
${CMAKE_CURRENT_SOURCE_DIR}/external_libs/mylibalpha/win64/release/libalpha.a
IMPORTED_LOCATION_DEBUG
${CMAKE_CURRENT_SOURCE_DIR}/external_libs/mylibalpha/win64/debug/libalpha.a
)
然后您需要在主项目的构建类型和导入目标的构建类型之间创建一个映射。
这样的映射可以在 per-target 的基础上创建,通过设置属性 MAP_IMPORTED_CONFIG_<CONFIG>:
set_target_properties(thirdPartyLib PROPERTIES
# For Debug version of the project use DEBUG-suffixed library location
MAP_IMPORTED_CONFIG_DEBUG DEBUG
# For Release version of the project use RELEASE-suffixed library location
MAP_IMPORTED_CONFIG_RELEASE RELEASE
# For ReleaseWithDebInfo version of the project use DEBUG-suffixed library location
MAP_IMPORTED_CONFIG_RELWITHDEBINFO DEBUG
# For MinSizeRel version of the project use RELEASE-suffixed library location
MAP_IMPORTED_CONFIG_MINSIZEREL RELEASE
)
或者,可以通过设置变量 CMAKE_MAP_IMPORTED_CONFIG_<CONFIG> 为 所有 IMPORTED 目标创建映射。请注意,这些变量应在 在 创建 IMPORTED 目标之前设置。
# For Debug version of the project use DEBUG-suffixed locations of IMPORTED targets
set(CMAKE_MAP_IMPORTED_CONFIG_DEBUG DEBUG)
# For Release version of the project use RELEASE-suffixed locations of IMPORTED targets
set(CMAKE_MAP_IMPORTED_CONFIG_RELEASE RELEASE)
# For ReleaseWithDebInfo version of the project use DEBUG-suffixed locations of IMPORTED targets
set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO DEBUG)
# For MinSizeRel version of the project use RELEASE-suffixed locations of IMPORTED targets
set(CMAKE_MAP_IMPORTED_CONFIG_MINSIZEREL RELEASE)
有了 IMPORTED 库和配置映射,使用这些库非常简单:
target_link_libraries(my_exe PRIVATE thirdPartyLib)
根据项目的构建类型,CMake 将自动 select 用于链接的适当库。