[CMake] Faking a library

Magnus Therning magnus at therning.org
Sat Oct 19 02:28:25 EDT 2013


On Wed, Oct 16, 2013 at 11:36:53PM +0200, Alexander Neundorf wrote:
> On Wednesday 16 October 2013, Magnus Therning wrote:
>> When defining a library via add_library() it's possible to link
>> against it by just putting its name into target_link_libraries().
>> Is there some way of wrapping up an external library in a similar
>> way?
>> 
>> As a concrete example I'm playing around with gtest[1] via
>> externalproject_add() like this:
>> 
>>     externalproject_add(gtest
>>         SOURCE_DIR ${PROJECT_SOURCE_DIR}/gtest
>>         BINARY_DIR gtest-build
>>         INSTALL_COMMAND "" # omit installation
>>         )
>> 
>> Then to link to it I need to use
>> 
>>     target_link_libraries(test
>>         -L${CMAKE_BINARY_DIR}/gtest-build -lgtest -lpthread
>>         )
>> 
>> Is there some way to wrap that all up in a target that I can pass
>> straight to target_link_libraries()?
>> 
>> Or is my only option to create a variable that expands to those
>> compiler flags?
> 
> You may create an imported target, and set some properties on it:
> 
> add_library(gtest STATIC IMPORTED)
> set_target_properties(gtest PROPERTIES
>  IMPORTED_LOCATION  ${CMAKE_BINARY_DIR}/libgtest-build.a)
> 
> You may need to set a few more properties like
> IMPORTED_LINK_INTERFACE_LIBRARIES, and you may want to use
> "${CMAKE_STATIC_LIBRARY_PREFIX}libgtest-build.${CMAKE_STATIC_LIBRARY_SUFFIX}"
> instead of hardcoding "lib" and "a".

Thanks, it works to add the following:

    add_library(gtest STATIC IMPORTED)
    set_target_properties(gtest PROPERTIES
        IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/gtest-build/libgtest.a
        IMPORTED_LINK_INTERFACE_LIBRARIES -lpthread
        )

Now the only thing remaining is that all targets using this lib also
needs to find its header files.  At the moment I'm defining a variable
for it:

    set(GTEST_INCDIR ${PROJECT_SOURCE_DIR}/gtest/include)

and then I need to put that into a target_include_directories(), e.g.:

    target_include_directories(test_one PRIVATE
        ${GTEST_INCDIR}
        ...
        )

Is it possible to put the include path in some property on the library
as well, to avoid using a separate variable for that?

If there's no standard property for it, would it be possible to use a
custom one, say INC_DIR, and then use the generator
$<TARGET_PROPERTY:gtest,INC_DIR> to extract it?  (I'm at all sure I've
understood generators though ;-)

/M

-- 
Magnus Therning                      OpenPGP: 0xAB4DFBA4 
email: magnus at therning.org   jabber: magnus at therning.org
twitter: magthe               http://therning.org/magnus

The right to search for truth implies also a duty; one must not
conceal any part of what one has recognized to be true.
     -- Albert Einstein
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://www.cmake.org/pipermail/cmake/attachments/20131019/bbe8accf/attachment.pgp>


More information about the CMake mailing list