Search code examples
c++cmakeautoconfautomakelibtool

cmake: how to install the include/ and lib/ directories as autotool does


I have an old project and I want to make use of cMake instead of the old autotools.

What the old program does is that, after type make, it will make libtest.a, libtest.la, libtest.so.1.0.0 etc. inside a hidden folder called .libs, then after I type make install, it will intall all libraries to a target folder $TEST_ROOT/lib (environment variable), it will also install all .h files into $TEST_ROOT/include folder.

in the Makefile.am:

source_list=test1.cpp test2.cpp
include_HEADERS=test1.h test2.h
AM_LDFLAGS="-pthread -lm -lrt"
lib_LTLIBRARIES=libtest.la
libtest_la_SOURCES=$(source_list)
libtest_la_LDFLAGS=$(AM_LDFLAGS) -static-libgcc

in configure.ac, I only see one relevant line,

if test -n "${TEST_ROOT}"; then
    ac_default_prefix=${TEST_ROOT}
    includedir=${ac_default_prefix}/include
fi

Frankly I don't really understand why the above codes will make .a, .la, .so etc. libraries all together, and then install them into the curresponding folder. Probably autotools can recognize the "ac_default_prefix" and "includeddir"?

Anyway, I want to do the same thing with cmake, the following is my attempt, but not a complete solution.

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/.libs)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY $(CMAKE_BINARY_DIR}/.libs)

set(CMAKE_CXX_FLAGS "-O3 -Wall")
set(CMAKE_EXE_LINKER_FLAGS "-pthread -lm -lrt")

file(GLOB SOURCES "*.cpp")
add_library(test STATIC ${SOURCES})

The above code will compile libtest.a in the build folder, not in the .libs folder inside build folder (meaning that CMAKE_RUNTIME_OUTPUT_DIRECTORY etc doesn't work).

Secondly, it will only build libtest.a, there is no libtest.la, no libtest.so.1.0.0 etc.

Thirdly, I am still not sure how let make install work like the auto tools. Can I just set the target include directory and target lib directory, then make install will install all .h files and .so, .a, .la files into the target directory?

Please help.

Thanks.


Solution

  • You have to go to the coreesponding CMakeLists.txt and add e.g.

    INSTALL(TARGETS test DESTINATION lib)
    

    In your root CMakeLists.txt you can determine the standard installation path:

    SET(CMAKE_INSTALL_PREFIX ".")
    

    In a similar way you can install your header files:

    FILE(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/*.hxx")
    INSTALL(FILES ${files} DESTINATION include)
    

    You can find even more examples at: https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/Install-Commands

    You can then install the files and libs with make (https://cmake.org/cmake/help/v3.1/variable/CMAKE_INSTALL_PREFIX.html):

    make DESTDIR=/home/mistapink install