[CMake] CMake and include only libraries

Michael Wild themiwi at gmail.com
Tue Apr 5 00:54:32 EDT 2011


On 04/05/2011 03:53 AM, Michael Hertling wrote:
> 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


I don't see what the problem with install(FILES ...) is? If you have a
header-only library, you just don't need any of the complex EXPORT
stuff. That is only related to the dependencies between libraries,
executables and transitive linking. In the case of a header-only library
you dump the files, create a XXXConfig.cmake and XXXConfigVersion.cmake
file, and be done with.

Michael


Michael


More information about the CMake mailing list