如何设置CMake的生成只有头项目? [英] How to setup Cmake to generate header-only projects?
问题描述
标题说明了一切。我想设置页眉只C ++(或C)库项目,但找不到一个干净的方式
The title say it all : I want to setup headers-only C++ (or C) library projects, but can't find a clean way.
某些搜索后,我发现,你不能设置使用 add_library
来做到这一点,因为它需要编译源文件正常库。
要做到这一点的一种方法是使用 add_custom_target
来代替,这种方式:
After some searches I've found that you can't setup 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} )
但是,这似乎并没有因为我看不到在VS2010生成的项目源代码完全在这里工作。我不知道这是否是一个错误或如果我做错了,或者如果有一个prefered办法做到这一点,所以如果你有一个简单的解决方案,请做我的客人。
But that don'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 prefered way to do this, so if you have a simple solution, please be my guest.
推荐答案
更新:的CMake将很快包括图书馆目标调用接口,非常适用于仅标头项目。此功能目前在主分支。 参考。
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.
使用命令 add_custom_target
,你提出对我的作品(VS2010)。这些文件我的项目内整齐地上市,但它有,你不能定义任何附加包含目录使用自定义目标的缺点。相反,我现在用的是以下内容:
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)
此设置您只有头项目作为一个虚拟的归档目标。别担心,不会产生任何实际的二进制文件,如果你应该尝试构建它(至少在VS2010和X code 4)。命令 set_target_properties
是那里,因为CMake的,否则会抱怨它无法推断.HPP文件的目标语言而已。
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.
这篇关于如何设置CMake的生成只有头项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!