Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"         ) 
like image 722
Miroslav Mares Avatar asked May 07 '12 18:05

Miroslav Mares


People also ask

How do I import libraries into a cmakelists project?

In the CMakeLists file, add an IMPORTED library and specify its location on disk: Then use the IMPORTED library inside of our project: On Windows, a .dll and its .lib import library may be imported together: A library with multiple configurations may be imported with a single target:

How do I add a subdirectory to a CMake library?

Inside the if block, put the add_subdirectory () command from above with some additional list commands to store information needed to link to the library and add the subdirectory as an include directory in the Tutorial target. The end of the top-level CMakeLists.txt file will now look like the following:

How do I add an executable file to a CMake list?

In the CMakeLists file, use the add_executable () command to create a new target called myexe. Use the IMPORTED option to tell CMake that this target references an executable file located outside of the project. No rules will be generated to build it and the IMPORTED target property will be set to true.

How do I add a mathfunctions library to CMake?

Add the following one line CMakeLists.txt file to the MathFunctions directory: To make use of the new library we will add an add_subdirectory () call in the top-level CMakeLists.txt file so that the library will get built.


1 Answers

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

like image 148
flm8620 Avatar answered Sep 20 '22 11:09

flm8620