简体   繁体   English

编译静态库时收集所有头文件

[英]Collect all header files when compiling static library

I am using Cmake to compile a static version of the library from source. 我正在使用Cmake从源代码编译该库的静态版本。

The source code has a structure that looks like this: 源代码的结构如下所示:

src/ 
   module1/
       x.cpp
       x.h
       ... 
   module2/
       y.cpp
       y.h
       ...

and so on... 等等...

Compiling a static version of the library is not difficult. 编译库的静态版本并不困难。 However for distribution purposes, I just want to distribute the the headers ( xh, yh, ... ) and static libraries ( module1.a, module2.a, ... ). 但是,出于分发目的,我只想分发标头( xh, yh, ... )和静态库( module1.a, module2.a, ... )。

Is there some command in GCC or CMAKE to automatically collect all of the headers and put them into a separate folder? 在GCC或CMAKE中是否有一些命令可以自动收集所有标头并将它们放在单独的文件夹中?

I am aware that I could manually separate the source and headers, or I could simply distribute all of the code (source and headers), but that is wasteful and undesireable for my particular use case. 我知道我可以手动将源代码和标头分开,或者我可以简单地分发所有代码(源代码和标头),但这对于我的特定用例来说是浪费和不希望的。 Alternatively, I could write a pretty simple Python script to do it, but it seems to me that this is probably a pretty common scenario. 另外,我可以编写一个非常简单的Python脚本来执行此操作,但在我看来,这可能是一个很常见的场景。 So I would guess that there is something in Gcc or Cmake to do it. 因此,我想在Gcc或Cmake中可以做些什么。

Note: I am not in charge of maintaining the codebase, so I have no say in how the project is structured. 注意:我不负责维护代码库,因此我对项目的结构没有发言权。 If I was, I could have separated the code into src and include folders. 如果是的话,我可以将代码分成srcinclude文件夹。

The best thing to do is have cmake glob and install all your artifacts. 最好的办法是使用cmake glob并安装所有工件。

# append to your existing CMakeLists.txt
install(TARGETS module1 module2 #adjust to use your names
        ARCHIVE DESTINATION lib)
file(GLOB_RECURSE header_list "*.h") #adjust if necessary
install(FILES ${header_list}
        DESTINATION include)

Be aware that globbing isn't perfect, and the file list is only updated when cmake is run (ie, it won't detect added or removed files on its own). 请注意,globbing并不完美,并且仅在运行cmake时才更新文件列表(即,它不会自行检测添加或删除的文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM