如何构建 CMake 库并使用命名空间包含其头文件?

问题描述 投票:0回答:1

我有一个 CMake 项目,其文件夹结构如下:

我的仓库在这里

├── CMakeLists.txt
├── conanfile.py
├── include
│   └── my_algorithm.h
├── src
│   └── my_algorithm.cpp
└── test_package
    ├── CMakeLists.txt
    ├── conanfile.py
    └── src
        └── example.cpp

在我的

test_package/src/example.cpp
中,我有一个像这样的
example.cpp
文件

#include "my_algorithm.h"
#include <vector>
#include <string>

int main() {
    algorithm();
}

如何使用 CMake 来构建我的项目,以便我的

include/algorithm.h
将作为
foundation/algorithm.h
导入,以供使用我的库的其他人作为示例?

我现在的

CMakeLists.txt
是这样的

cmake_minimum_required(VERSION 3.15)
project(my_foundation CXX)

add_library(my_foundation src/algorithm.cpp)
target_include_directories(my_foundation PUBLIC include)

set_target_properties(my_foundation PROPERTIES PUBLIC_HEADER "include/algorithm.h")
install(TARGETS my_foundation)

我遵循两种方法为我的库创建“命名空间”。

方法 1 - 在我的
include
目录中添加文件夹

我更改了存储库的文件夹结构

+ CMakeLists.txt
+ include
  + my_foundation
    + my_algorithm.h

并且

CMakeLists.txt
更改为

add_library(my_foundation src/my_algorithm.cpp)
target_include_directories(my_foundation PUBLIC include)

set_target_properties(my_foundation PROPERTIES PUBLIC_HEADER "include/my_foundation/my_algorithm.h")

但是当我测试在外部使用中导入我的库时,

我仍然需要将标题导入为

my_algorithm.h
而不是
my_foundation/my_algorithm.h

#include "my_foundation/my_algorithm.h" # Not working


//#include "my_algorithm.h" // Working

我的试用版可以在这里找到repo

方法 2 - 仅添加“命名空间”供外部使用

我的首选方式repo

我已经更改了

CMakeLists.txt
文件

add_library(my_foundation src/my_algorithm.cpp)
- #target_include_directories(my_foundation PUBLIC include)
+ target_include_directories(my_foundation PRIVATE include/my_foundation include)
+ target_include_directories(my_foundation INTERFACE include)

尽管如此,我得到的结果与方法 1 相同,我只能将标头导入为

my_algorithm.h
而不是
my_foundation/my_algorithm.h

编辑

我的

test_package/CMakeLists.txt
样子

cmake_minimum_required(VERSION 3.15)
project(PackageTest CXX)
find_package(my_foundation CONFIG REQUIRED)
add_executable(example src/example.cpp)
target_link_libraries(example my_foundation::my_foundation)

我正在使用 conan2 安装 cpp 包并测试我的 cpp 包..

我创建cpp库的命令是

conan install .
cmake --preset conan-release
cmake --build --preset conan-release
cmake conan-2
1个回答
0
投票

这个“命名空间”只不过是一个目录。如果你想要一个

foundation
目录存在,你只需要创建它。

将目录结构更改为以下布局(仅显示如下相关文件):

.
├── CMakeLists.txt
└── include
    └── foundation
        └── algorithm.h

在您的

CMakeLists.txt
中,您无需更改任何内容。两条线

add_library(algorithm ...)
target_include_directories(algorithm PUBLIC include)

... 将把

include
添加到包含目录搜索路径中。因此,每个人都必须
#include "foundation/algorithm.h"
包含相应的文件。

如果您只想更改它以供外部使用,您可以这样做

add_library(algorithm ...)
target_include_directories(algorithm PRIVATE include/foundation)
target_include_directories(algorithm INTERFACE include)

尽管我也会在内部使用

foundation
前缀并完成它。 在您的具体情况下,我肯定这样做只是为了避免您的
foundation/algorithm.h
和标准库的
algorithm
标头之间的混淆。

要在安装后保留头文件及其子目录,您还需要将其传递给

install()
命令,如安装时的 CMake 文档中所述:

install(TARGETS my_foundation
    PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/include/foundation
)
© www.soinside.com 2019 - 2024. All rights reserved.