如何设置 CMake 来生成仅标头项目?
我想设置仅包含头文件的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新:CMake 很快将包含一个名为 INTERFACE 的库目标,它非常适合仅包含标头的项目。该功能目前在 master 分支中。 参考。
使用您建议的命令
add_custom_target
对我有用(VS2010)。这些文件在我的项目中整齐地列出,但它的缺点是您无法使用自定义目标定义任何“其他包含目录”。相反,我现在使用以下内容:这将您的仅标头项目设置为虚拟存档目标。别担心,如果您尝试构建它,则不会生成任何实际的二进制文件(至少在 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: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.您可以使用最新的接口库来执行此操作功能:
这将创建一个没有任何源文件的库目标,并将包含目录添加到 INTERFACE_INCLUDE_DIRECTORIES 目标的属性。这意味着链接到此库的任何目标在构建时都会将这些目录作为包含路径 (
-I
) 获取。例如,要将库与可执行目标一起使用,只需执行以下操作:
You can do this using the recent Interface Library feature:
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:
我认为您正在寻找的只是使用添加一个包含目录
cmake 的“include_directories”命令。
执行此操作时,如果它是您无法控制的第三方工具,我还会添加“SYSTEM”标志。
所以你的命令看起来像这样:
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: