CMake 为发布、调试等提供条件

发布于 2025-01-09 06:48:22 字数 1782 浏览 0 评论 0原文

我正在开发一个 CMake 项目,该项目需要在链接过程中为静态库的每种配置类型(例如 RELEASE、DEBUG、MINSIZEREL 和 RELWITHDEBINFO)设置特定路径。因为我有不同版本的静态库用于调试和发布版本。

到目前为止,我已经在下面完成了此操作,以便为我尝试链接的静态库设置文件夹“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} 变量为空且未正确设置。我基本上在链接时遇到错误,显示找不到external_libs/mylibalpha/win64//libalpha.lib,我应该得到类似这样的external_libs/mylibalpha/win64/release/libalpha .lib

我的 CMakeLists.txt 脚本有什么问题?

I am working on a CMake project that need to set specific paths for each configuration type (e.g., RELEASE, DEBUG, MINSIZEREL and RELWITHDEBINFO) for a static library in linking process. Because I have different versions of my static libraries that is used on Debug and Release versions.

So far, I have done this below in order to set the folder "release" or "debug" for my static library that I am try to link:

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

However, in my Visual Studio, I noticed that on each project build type (RELEASE, DEBUG, MINSIZEREL and RELWITHDEBINFO) the ${THIRDLIBS_BUILD_TYPE} variable is empty and not getting properly set. I am basically getting an error on linking showing that cannot find the external_libs/mylibalpha/win64//libalpha.lib and I should be getting something like this external_libs/mylibalpha/win64/release/libalpha.lib

What is wrong with my CMakeLists.txt script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淤浪 2025-01-16 06:48:22

变量 CMAKE_BUILD_TYPE 对多配置生成器没有任何意义,而 Visual Studio 就是此类生成器之一。

在 CMake 中定义具有不同配置不同位置的预构建库的规范方法是创建具有多个属性 IMPORTED_LOCATION_进行设置:

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
)

那么你需要创建一个主项目的构建类型和 IMPORTED 目标的构建类型之间的映射

可以通过设置属性 MAP_IMPORTED_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
)

或者,可以通过设置为所有 IMPORTED 目标创建映射变量 CMAKE_MAP_IMPORTED_CONFIG_。请注意,这些变量应在创建 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)

有了导入的库和配置映射,使用这些库非常简单:

target_link_libraries(my_exe PRIVATE thirdPartyLib)

根据项目的构建类型,CMake 将自动选择正确的库进行链接。

Variable CMAKE_BUILD_TYPE has no sense with multiconfiguration generators, and Visual Studio is one of such generators.

Canonical way for define in CMake a pre-built library which have different locations for different configuration is to create IMPORTED library target with several properties IMPORTED_LOCATION_<CONFIG> to be set:

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
)

Then you need to create a mapping between build types of the main project and build types of the IMPORTED target.

Such mapping could be created either on per-target basis, by setting properties 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
)

Alternatively, the mapping could be created for all IMPORTED targets by setting variables CMAKE_MAP_IMPORTED_CONFIG_<CONFIG>. Note, that these variables should be set before creation of IMPORTED target(s).

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

Having IMPORTED libraries and configurations mapping, using these libraries is quite straightforward:

target_link_libraries(my_exe PRIVATE thirdPartyLib)

Depending on the build type of the project, CMake will automatically select proper library for linking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文