如何设置 CMake 来生成仅标头项目?

发布于 2024-11-05 23:56:30 字数 453 浏览 1 评论 0原文

我想设置仅包含头文件的 C++(或 C)库项目,但找不到干净的方法。

经过一番搜索后,我发现您无法使用 add_library 设置普通库来执行此操作,因为它需要可编译的源文件。 一种方法是使用 add_custom_target 来代替,这样:

# Get all headers (using search instead of explicit filenames for the example)
file( GLOB_RECURSE XSD_HEADERS 
    *.hxx
)
add_custom_target( libsxsd SOURCES ${XSD_HEADERS} )

但这似乎在这里并不完全有效,因为我看不到 VS2010 中生成的项目中的源代码。我不知道这是否是一个错误,或者我是否做错了,或者是否有更好的方法来做到这一点。

I want to set up header-only C++ (or C) library projects, but can't find a clean way.

After some searches I've found that you can't set up a normal library using add_library to do this because it requires a compilable source file.
A way to do this would be to use add_custom_target instead, this way:

# Get all headers (using search instead of explicit filenames for the example)
file( GLOB_RECURSE XSD_HEADERS 
    *.hxx
)
add_custom_target( libsxsd SOURCES ${XSD_HEADERS} )

But that doesn't seem to work completely here as I can't see the sources in the project generated in VS2010. I don't know if it's a bug or if I'm doing it wrong or if there is a preferred way to do this.

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

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

发布评论

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

评论(3

阿楠 2024-11-12 23:56:30

更新:CMake 很快将包含一个名为 INTERFACE 的库目标,它非常适合仅包含标头的项目。该功能目前在 master 分支中。 参考

使用您建议的命令 add_custom_target 对我有用(VS2010)。这些文件在我的项目中整齐地列出,但它的缺点是您无法使用自定义目标定义任何“其他包含目录”。相反,我现在使用以下内容:

add_library(HEADER_ONLY_TARGET STATIC test1.hpp test2.hpp)
set_target_properties(HEADER_ONLY_TARGET PROPERTIES LINKER_LANGUAGE CXX)

这将您的仅标头项目设置为虚拟存档目标。别担心,如果您尝试构建它,则不会生成任何实际的二进制文件(至少在 VS2010 和 Xcode 4 中不会)。命令 set_target_properties 之所以存在,是因为 CMake 会抱怨它无法仅从 .hpp 文件推断目标语言。

Update: CMake will soon include a library target called INTERFACE that is ideal for header-only projects. This feature is currently in the master branch. Reference.

Using the command add_custom_target as you propose works for me (VS2010). The files are neatly listed within my project but it has the drawback that you can't define any "Additional Include Directories" with a custom target. Instead, I now use the following:

add_library(HEADER_ONLY_TARGET STATIC test1.hpp test2.hpp)
set_target_properties(HEADER_ONLY_TARGET PROPERTIES LINKER_LANGUAGE CXX)

This sets up your header-only project as a dummy archive target. Don't worry, no actual binaries will be generated if you should try and build it (at least not in VS2010 and Xcode 4). The command set_target_properties is there because CMake will otherwise complain that it cannot infer the target language from .hpp files only.

開玄 2024-11-12 23:56:30

您可以使用最新的接口库来执行此操作功能:

add_library(mylib INTERFACE)
target_include_directories(mylib INTERFACE my_include_dir1 my_include_dir2)

这将创建一个没有任何源文件的库目标,并将包含目录添加到 INTERFACE_INCLUDE_DIRECTORIES 目标的属性。这意味着链接到此库的任何目标在构建时都会将这些目录作为包含路径 (-I) 获取。

例如,要将库与可执行目标一起使用,只需执行以下操作:

add_executable(myexec ${MY_SOURCES})
target_link_libraries(myexec mylib)

You can do this using the recent Interface Library feature:

add_library(mylib INTERFACE)
target_include_directories(mylib INTERFACE my_include_dir1 my_include_dir2)

This creates a library target without any source files, and adds the include directories to the INTERFACE_INCLUDE_DIRECTORIES property of the target. This means that any target that links to this library will get these directories as include paths (-I) when built.

For instance, to use the library with an executable target, just do:

add_executable(myexec ${MY_SOURCES})
target_link_libraries(myexec mylib)
若有似无的小暗淡 2024-11-12 23:56:30

我认为您正在寻找的只是使用添加一个包含目录
cmake 的“include_directories”命令。

执行此操作时,如果它是您无法控制的第三方工具,我还会添加“SYSTEM”标志。

所以你的命令看起来像这样:

include_directories(SYSTEM ${GTEST_INCLUDE_DIRS})

I think what you are looking for is just adding an include directory using the
"include_directories" command for cmake.

When doing this, if it is a third party tool that you don't have control over, I would also add the "SYSTEM" flag.

So you command would look like something like this:

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