[CMake] CMake and include only libraries

Michael Hertling mhertling at online.de
Mon Apr 4 21:53:54 EDT 2011


On 04/04/2011 07:30 PM, Theodore Papadopoulo wrote:

> 	Hi,
> 
> I'm trying to use cmake to install a include only library.
> I'd like to use the export feature as described in
> 
> http://www.itk.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file
> 
> and I found the idea of using
> 
> set_target_properties(foo PROPERTIES PUBLIC_HEADER
> "foo.h;${CMAKE_CURRENT_BINARY_DIR}/config.h")
> 
> nice. But in my case, I do not have any library to build...
> All my attempts to create custom targets or library targets with only
> headers fail.... So I'm left with using install(FILES ...) and forget
> this export idea unless someone knows a trick...
> 
> Here is a sketch of my CMakelists.txt
> 
> SET(Utils_HEADERS ...)
> 
> INCLUDE(TestBigEndian)
> TEST_BIG_ENDIAN(WORDS_BIGENDIAN)
> CONFIGURE_FILE (${CMAKE_CURRENT_SOURCE_DIR}/Utils/config.h.in config.h
> @ONLY)
> 
> SET(HEADERS)
> FOREACH(file ${Utils_HEADERS})
>     SET(HEADERS "${HEADERS};${CMAKE_CURRENT_SOURCE_DIR}/Utils/${file}")
> ENDFOREACH()
> 
> # Those do not work
> #ADD_LIBRARY(UtilsIncludes SHARED ${HEADERS}
> ${CMAKE_CURRENT_SOURCE_DIR}/Utils/config.h)
> #ADD_CUSTOM_TARGET(UtilsIncludes All DEPENDS ${HEADERS}
> #${CMAKE_CURRENT_SOURCE_DIR}/Utils/config.h.in)
> SET_TARGET_PROPERTIES(UtilsIncludes PROPERTIES
>                       PUBLIC_HEADER
> "${HEADERS};${CMAKE_CURRENT_BINARY_DIR}/config.h")
> 
> INSTALL(TARGETS UtilsIncludes EXPORT  UtilsLibraryDepends
>         LIBRARY DESTINATION lib
>         PUBLIC_HEADER DESTINATION include/Odyssee++ COMPONENT dev)
> 
> Thank's in advance for any hint.
> 
> 	All the best.
> 
> 		Theo.

You might

- build an empty static or shared library from the header files only,
- remove it right after is has been built by a custom command, and
- provide an OPTIONAL clause to the INSTALL() command to prevent
  it from failing when it tries to install the library file:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(HEADERLIBRARY C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
FILE(WRITE ${CMAKE_BINARY_DIR}/f.h "void f(void)\n")
ADD_LIBRARY(f STATIC f.h)
SET_TARGET_PROPERTIES(f PROPERTIES
    LINKER_LANGUAGE C PUBLIC_HEADER ${CMAKE_BINARY_DIR}/f.h)
ADD_CUSTOM_COMMAND(TARGET f POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E remove $<TARGET_FILE:f>)
INSTALL(TARGETS f EXPORT f
    PUBLIC_HEADER DESTINATION include
    ARCHIVE DESTINATION include OPTIONAL)
INSTALL(EXPORT f DESTINATION share)

Note the ARCHIVE DESTINATION *include* to prevent the INSTALL() command
from creating a directory for the non-existing library. The downside of
that approach is a little extra cost to build the empty library each
time make is run.

Nevertheless, the export file created by INSTALL(EXPORT ...) will still
contain a reference to the non-existing libf.a, so I wonder what's the
point of using the INSTALL() command's EXPORT feature with a header-
only library, i.e. what's so bad with INSTALL(FILES ...) or - after
setting up the library's headers - with INSTALL(DIRECTORY ...)?

Regards,

Michael


More information about the CMake mailing list