c++ 使用CMake包含来自不同库的同名标头

cnh2zyt3  于 2022-12-24  发布在  其他
关注(0)|答案(1)|浏览(227)

我有两个(或更多)库包含相同名称的头文件(例如,"point.h")。在我的项目中,我需要在不同的上下文中使用这些头文件中定义的类的属性,但当然,不可能以常见的方式包含正确的文件,因为具有相同名称的模糊性。
许多库通过"命名空间"来解决这个问题(例如,"#include〈rapidjson/document.h〉")。我想这样做。我如何配置CMake来实现这一点?有没有一种通用的(可能是简单的)方法来实现这一点?
下面是我的CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)

project(GISManager LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(OSMManager REQUIRED) #First libray
find_package(GeoJSONParser REQUIRED) #Second library

set(GISManager_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(GISManager_H ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(Hdrs ${GISManager_H}/gismanager.h )
set(Srcs ${GISManager_SRC}/gismanager.cpp)

add_library(GISManager ${Hdrs} ${Srcs})
target_include_directories(GISManager PUBLIC
    $<BUILD_INTERFACE:${GISManager_H}/>
    ${OSMManager_INCLUDE_DIRS}/
    ${GeoJSONParser_INCLUDE_DIRS}/
)
target_link_libraries(GISManager ${OSMManager_LIBRARIES} ${GeoJSONParser_LIBRARIES})

add_executable(GISManager_Test ${Hdrs} ${Srcs} ${GISManager_SRC}/main.cpp)
target_include_directories(GISManager_Test PUBLIC
    $<BUILD_INTERFACE:${GISManager_H}/>
    ${OSMManager_INCLUDE_DIRS}/
    ${GeoJSONParser_INCLUDE_DIRS}/
)
target_link_libraries(GISManager_Test ${OSMManager_LIBRARIES} ${GeoJSONParser_LIBRARIES})

install(FILES  ${Hdrs}  DESTINATION include/GISManager-${version})
install(TARGETS GISManager
        DESTINATION lib/GISManager-${version}
        EXPORT GISManager-targets)
install(EXPORT GISManager-targets
        DESTINATION lib/GISManager-${version})

configure_file(
   ${GISManager_SOURCE_DIR}/pkg/gismanager-config.cmake.in
   ${GISManager_BINARY_DIR}/pkg/gismanager-config.cmake @ONLY)

configure_file(
   ${GISManager_SOURCE_DIR}/pkg/gismanager-config-version.cmake.in
   ${GISManager_BINARY_DIR}/pkg/gismanager-config-version.cmake @ONLY)

install(FILES ${GISManager_BINARY_DIR}/pkg/gismanager-config.cmake
         ${GISManager_BINARY_DIR}/pkg/gismanager-config-version.cmake
         DESTINATION lib/GISManager-${version})

这些库是使用以下两个CMakeLists.txt文件生成的:
x一个一个一个一个x一个一个二个x
The files library-config.cmake.in have more or less the same structure in the two libraries, here is the one for GeoJSONParser:

# Compute installation prefix relative to this file.
get_filename_component(_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_prefix "${_dir}/../.." ABSOLUTE)

# Import the targets.
include("${_prefix}/lib/GeoJSONParser-@version@/GeoJSONParser-targets.cmake")

# Report other information.
set(GeoJSONParser_INCLUDE_DIRS "${_prefix}/include/GeoJSONParser-@version@/")

file(GLOB GeoJSONParser_LIBRARIES "${_prefix}/lib/GeoJSONParser-@version@/lib*")

正如您在上面看到的,这两个库都有一个"utils. h",包括一些特定于所讨论的库的函数。

q35jwt9p

q35jwt9p1#

只要使传递给编译器的include目录包含这样一个中间目录,该中间目录执行您希望为#include指令使用的命名空间。
您可能希望对源代码树和安装树都这样做。
如果你想要一个配置代码的答案,请提供一个最小的,有代表性的示例设置。

相关问题