简体   繁体   中英

Collect all header files when compiling static library

I am using Cmake to compile a static version of the library from source.

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, ... ).

Is there some command in GCC or CMAKE to automatically collect all of the headers and put them into a separate folder?

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. So I would guess that there is something in Gcc or Cmake to do it.

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.

The best thing to do is have cmake glob and install all your artifacts.

# 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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