CMake项目中带有前缀的头文件 [英] Header files with prefix in CMake project

查看:61
本文介绍了CMake项目中带有前缀的头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个CMake项目,其目录结构如下:

I have set up a CMake project whose directory structure looks as follows:

src/
--CMakeLists.txt
--libA/
----CMakeLists.txt
----foo.h
----foo.cpp
--main/
----CMakeLists.txt
----main.cpp

src / CMakeLists.txt 使用 add_subdirectory 引入 libA main libA / CMakeLists.txt 使用 add_library 定义名为 libA ,通过 target_include_directories 导出 foo.h 。如果现在我使用 target_link_library 链接到 main 中的 libA ,我可以通过 main中的 #include< foo.h> 包括 foo.h 。 cpp

src/CMakeLists.txt uses add_subdirectory to pull in libA and main. libA/CMakeLists.txt uses add_library to define a library called libA, which exports foo.h via target_include_directories. If I now link against libA in main using target_link_library, I can include foo.h via #include <foo.h> in main.cpp.

问题:是否可以为 libA 的公共接口提供前缀,这样我就可以(必须)在 main.cpp #include< libA / foo.h>

Question: Is it possible to provide the public interface of libA with a prefix, so that I can (and have to) write #include <libA/foo.h> in main.cpp instead?

推荐答案

您可以使用 root 源目录(或其他一些目录,在 target_include_directories()调用中是 libA 的父级。这将允许相对于另一个目录定义 INTERFACE_INCLUDE_DIRECTORIES 目标属性(在此示例中, CMAKE_SOURCE_DIR )。因此它看起来像这样:

You can use the root source directory (or some other directory which is a parent to libA) in the target_include_directories() call. This will allow the INTERFACE_INCLUDE_DIRECTORIES target property to be defined with respect to another directory (in this example, the CMAKE_SOURCE_DIR). So it would look something like this:

libA / CMakeLists.txt

add_library(libA foo.cpp)
# Define the include files with respect to the directory above this.
target_include_directories(libA PUBLIC ${CMAKE_SOURCE_DIR})

main /main.cpp 文件:

#include <iostream>
#include <libA/foo.h>

int main() {

    FooClass fooclass;
    fooclass.myFunction();

    std::cout << "Hello World!" << std::endl;
    return 0;
}

这篇关于CMake项目中带有前缀的头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆