[CMake] How to install headers in a library

Michael Wild themiwi at gmail.com
Sun Feb 28 05:50:20 EST 2010


On 28. Feb, 2010, at 11:09 , Hendrik Sattler wrote:

> Am Sonntag 28 Februar 2010 10:31:57 schrieb Giampiero Gabbiani:
>> Hi all,
>> I like very much the simplicity of cmake in comparison with the classic
>> autotools framework, but still I have two questions concerning the porting
>> of a Linux based project under cmake:
>> 
>> 1) when defining a library (shared or static) as a project target , I
>> expected that cmake would understand the architecture of the host machine
>> and install conseguently the libraries (.so or .a) in the correct /lib or
>> /lib64 path. Instead I noticed that I have to explicity set the
>> destination path
>> 
>> install(TARGETS <static library name> ARCHIVE DESTINATION lib64)
>> install(TARGETS <shared object name> LIBRARY DESTINATION lib64)
>> 
>> This works well on my machine (a 64 bit system) but of course will not work
>> if anyone want to install on a 32 bit system.
>> 
>> So ... is there a way in order to let cmake automatically install on the
>> correct path according with the hosting system architecture?
>> 
>> 2) together with the resulting binaries libs (both archive and library) I
>> want to install also the corrisponding header(s) in /usr/local/include
>> path...how is it possible to do that?
> 
> You could use a variable to represent the directory. This would depend on the 
> existence of lib64/ (not all have this as directory, on Debian it's just a 
> compatibility link to lib/) and on SIZEOF_VOID_P being 8:
> set (LIBRARY_INSTALL_DIR lib)
> if (EXISTS "${CMAKE_INSTALL_PREFIX}/lib32/" AND CMAKE_SIZEOF_VOID_P EQUAL 4)
>  set (LIBRARY_INSTALL_DIR lib32)
> elif (EXISTS "${CMAKE_INSTALL_PREFIX}/lib64/" AND CMAKE_SIZEOF_VOID_P EQUAL 8)
>  set (LIBRARY_INSTALL_DIR lib64)
> endif ()
> 
> Or let the user set it.
> 
> HS

Such decisions I often handle like this:

- compute a sensible default, as Hendrik showed above.
- use that default to set a cache variable
- let the user override the default using the cache variables

In above example this would be:

# compute default library install dir
set (_DEFAULT_LIBRARY_INSTALL_DIR lib)
if (EXISTS "${CMAKE_INSTALL_PREFIX}/lib32/" AND CMAKE_SIZEOF_VOID_P EQUAL 4)
 set (_DEFAULT_LIBRARY_INSTALL_DIR lib32)
elif (EXISTS "${CMAKE_INSTALL_PREFIX}/lib64/" AND CMAKE_SIZEOF_VOID_P EQUAL 8)
 set (_DEFAULT_LIBRARY_INSTALL_DIR lib64)
endif ()
# the library install dir
set(LIBRARY_INSTALL_DIR "${_DEFAULT_LIBRARY_INSTALL_DIR}" CACHE PATH "Installation directory for libraries")
# make the library install dir an absolute path (can be important e.g. when using CONFIGURE_FILE to embed
# the library installation directory into a file)
if(NOT IS_ABSOLUTE "${LIBRARY_INSTALL_DIR}")
  set(LIBRARY_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${LIBRARY_INSTALL_DIR}")
endif()


Of course, you can extend the logic for the default selection to handle particularities of other platforms, such as Windows.

HTH

Michael


More information about the CMake mailing list