[CMake] Rolling up Object libraries into a final product

Cameron Palmer cameron at promon.no
Sat Apr 14 01:51:34 EDT 2018


I’m using object libraries to put together a project composed of numerous modules. When putting together the static library (deliverable) at the end I have used libtool to assemble final product. I’ve been trying to adopt the use of OBJECT type libraries to avoid the libtool call and it mostly works with the exception that OpenSSL and Boost static libraries are not included in the result. This means I’ll still need libtool to add them to the archive.

1) Does this look like I’m using this correctly?
2) Can the .a files be handled by CMake in a better way than using libtool? It is a collection of objects after all.

When this is built, other libraries will add this library in the

target_link_libraries( another_interface INTERFACE common )



### Library - objects
add_library( common_objects OBJECT
        ${HEADER_FILES}
        ${SOURCE_FILES} )
set_target_properties( common_objects PROPERTIES POSITION_INDEPENDENT_CODE on )
target_include_directories( common_objects
        PUBLIC
            include
            iOS/include
            ${BOOST_INCLUDE_DIR} )

### Library - dependencies
add_library( common_interface INTERFACE )
target_link_libraries( common_interface INTERFACE prefs ${BOOST_STATIC_LIBS} )
target_include_directories( common_interface
INTERFACE
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
           $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/iOS/include>
         $<BUILD_INTERFACE:${BOOST_INCLUDE_DIR}>
         $<INSTALL_INTERFACE:include> )

### Library - interface
add_library( common INTERFACE )
target_sources( common INTERFACE $<TARGET_OBJECTS:common_objects> )
target_link_libraries( common INTERFACE common_interface )

### Library is assembled into static and shared as well

if( BUILD_STATIC_LIB )
    add_library( common_static STATIC "" )
    set_target_properties( common_static
            PROPERTIES
                OUTPUT_NAME common )
    target_link_libraries( common_static PRIVATE common )
endif()

if( BUILD_SHARED_LIB )
    add_library( common_shared SHARED "" )
    set_target_properties( common_shared
            PROPERTIES
                OUTPUT_NAME common )
    target_link_libraries( common_shared PRIVATE common )
endif()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180414/4966de68/attachment-0001.html>


More information about the CMake mailing list