【问题标题】:Using protobuf with CMake's different build configurations (RelWithDebugInfo and Debug)将 protobuf 与 CMake 的不同构建配置一起使用(RelWithDebugInfo 和 Debug)
【发布时间】:2019-03-20 15:43:44
【问题描述】:

在我的项目中,我使用的是 protobuf 3.5。我至少需要调试和RelWithDebugInfo 配置。为了能够使用 protobuf 调试库构建项目,仅会导致一些问题:

我需要使用发布和调试目标从源代码构建 protobuf 库,因为我的库 (= 2) 的 _ITERATOR_DEBUG_LEVEL 与 protobuf 库 (= 0) 的级别不匹配。在构建调试库和发布库之后,可以在调试配置中进行编译。

现在,改回RelWithDebugInfo 后,我再次遇到同样的错误,但现在正好相反:我的库的_ITERATOR_DEBUG_LEVEL 是0,使用的protobuf 库的级别是2。

检查链接器配置时,我可以看到我的库与 libprotobufd.lib 链接。这是有道理的,因为我在某处读到过 not Release 的所有内容都将使用调试库(如果有)。这导致了我的问题:

在开发过程中,我不会在 Release 中构建我的库。大多数时候RelWithDebugInfo。但是此配置的_ITERATOR_DEBUG_LEVEL 显然设置为0(因为它是带有附加信息的发布配置)。但是 CMake 会链接到与其他不兼容的 protobuf 调试库。

我现在正在寻找一种可能性,告诉 CMake 不要使用库的调试版本,而是使用发布版本而不更改 protobuf 本身的 CMake 脚本。

通常我的方式是link different libraries,具体取决于实际的构建配置。但不幸的是,protobuf CMake 配置会尝试自行处理。

# Load information for each installed configuration.
get_filename_component(_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
file(GLOB CONFIG_FILES "${_DIR}/protobuf-targets-*.cmake")
foreach(f ${CONFIG_FILES})
  include(${f})
endforeach()

而导入的目标被覆盖根据实际配置选择:

protobuf-targets-release.cmake:

# Import target "protobuf::libprotobuf-lite" for configuration "Release"
set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
  IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib/libprotobuf-lite.lib"
  )

protobuf-targets-debug.cmake:

# Import target "protobuf::libprotobuf-lite" for configuration "Debug"
set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX"
  IMPORTED_LOCATION_DEBUG "${_IMPORT_PREFIX}/lib/libprotobuf-lited.lib"
  )

而我的 CMakeLists.txt 中的链接如下所示:

target_link_libraries(${PROJECT_NAME} PRIVATE
    protobuf::libprotobuf
    protobuf::libprotoc
)

我在这里看不到指定所需库的任何可能性。通常我会说我会以某种方式指定它:

target_link_libraries(MyEXE
    debug protobuf::libprotobufd optimized protobuf::libprotobuf
    debug protobuf::libprotocd optimized protobuf::libprotoc)

或者为它周围的不同构建配置包装一些花哨的 if 条件。但是由于 protobuf 有效地覆盖了目标,我不知道如何为每个构建提取正确的库。

有什么想法吗?

【问题讨论】:

  • "但是由于 protobuf 有效地覆盖了目标" - 不,目标没有被覆盖。分配的是它们的属性,但它们都是不同的(不同的后缀)。唯一相同的属性是 IMPORTED_CONFIGURATIONS,但它是 APPEND 的,没有被覆盖。您正在寻找的是 CMAKE_MAP_IMPORTED_CONFIG_<CONFIG> 变量,它将您的项目的配置“映射”到导入的配置。
  • 对不起,是的!你是绝对正确的!这意味着我可以将映射命令放入 if 条件并执行以下操作:if (build == RelWithDebugInfo) then CMAKE_MAP_IMPORTED_CONFIG_RELEASE?

标签: cmake protocol-buffers


【解决方案1】:

我现在正在寻找一种可能性,告诉 CMake 不要使用库的调试版本,而是使用发布版本

变量CMAKE_MAP_IMPORTED_CONFIG_ 旨在解决该问题:

# When build your project in RelWithDebugInfo configuration,
# try to use Release configuration of the *IMPORTED* libraries.
# If some IMPORTED library has no Release configuration, fallback to Debug one.
set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBUGINFO Release Debug)

# Imported targets set with given call will be aware of the variable's set above
find_package(Protobuf REQUIRED)

# Simply link with an IMPORTED library. 
target_link_libraries(MyExe protobuf::libprotobuf)

如果您想仅使用特定 IMPORTED库中的发布配置,但不是全部,您可以为这些特定的属性设置相应的图书馆:

# Firstly, create needed IMPORTED target.
find_package(Protobuf REQUIRED)

# Then adjust its properties.
# While the target is created by others, given property is specifically
# designed to be set in *your project*.
set_property(TARGET protobuf::libprotobuf PROPERTY MAP_IMPORTED_CONFIG_RELWITHDEBUGINFO Release Debug)

# Simply link with an IMPORTED library. 
target_link_libraries(MyExe protobuf::libprotobuf)

【讨论】:

  • 感谢您的回答!这现在工作正常。我只花了一个小时就发现构建配置被命名为 RelWithDebInfo 和 not RelWithDebugInfo,正如我最初所想的那样。伙计,如果 CMake 在未设置变量时能稍微更冗长,那就太好了...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-01
  • 1970-01-01
  • 2020-08-28
  • 1970-01-01
  • 2015-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多