CMAKE - How to properly copy static library's header file into /usr/include?

Miroslav Mares picture Miroslav Mares · May 7, 2012 · Viewed 48.6k times · Source

I'm getting into CMAKE usage with C and actually I'm creating two very small static libraries.

My goal is:

  1. The libraries are compiled and linked into *.a files. [THIS WORKS]
  2. Then I wish to copy that *.a files into /usr/local/lib [THIS ALSO WORKS]
  3. As far as I know about libraries (very little), they are linked using -lnameoflib, which is a compiler flag. OK. I have prepared my CMakeLists.txt and it actually copies *.a files into /usr/local/lib. However, to be able to use them in a program, I also need to copy their header files into /usr/local/include, then I can include them the easy way #include <mylibheader.h>. That's how I understand it now.

And my question is - how is the proper way of copying header files into /usr/include folder with CMAKE? I would like it to copy them automatically when make install is executed, like *.a files are.

For both of the libraries I have a smiliar CMakeLists.txt:

project(programming-network)

add_library(programming-network STATIC
    send_string.c
    recv_line.c
    )

INSTALL(TARGETS programming-network
        DESTINATION "lib"
        )

Answer

flm8620 picture flm8620 · Oct 27, 2016

A better way for newest cmake version is to use target's PUBLIC_HEADER properties.

project(myproject)

add_library(mylib some.c another.c)
set_target_properties(mylib PROPERTIES PUBLIC_HEADER "some.h;another.h")
INSTALL(TARGETS mylib 
        LIBRARY DESTINATION some/libpath
        PUBLIC_HEADER DESTINATION some/includepath
)

Some ref:

PUBLIC_HEADER

CMake install command