I'm getting into CMAKE usage with C and actually I'm creating two very small static libraries.
My goal is:
-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" )
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:
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:
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With