[CMake] transitive link flags

Michael Hertling mhertling at online.de
Sun Nov 6 18:05:50 EST 2011


On 11/06/2011 07:04 PM, Luke Dalessandro wrote:
> Hi everyone,
> 
> I have a static library target that has some sources that rely on link-time symbol interposition.
> 
> Is there an existing way to set transitive link flags of the "-Wl,--wrap,symbol" variety on the library target so that executables that depend on the library are automatically linked correctly?
> 
> Thanks,
> Luke 

AFAIK, there's currently no immediate possibility to achieve this goal
- a target property or the like - but you can use the trick with a
reimported empty static library; see the following example:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(WRAP C)
SET(CMAKE_VERBOSE_MAKEFILE ON)

FILE(WRITE ${CMAKE_BINARY_DIR}/empty.c "")
ADD_LIBRARY(empty STATIC empty.c)
EXPORT(TARGETS empty NAMESPACE imported FILE importedempty.cmake)
INCLUDE(${CMAKE_BINARY_DIR}/importedempty.cmake)
SET_TARGET_PROPERTIES(importedempty PROPERTIES
    IMPORTED_LINK_INTERFACE_LIBRARIES "-Wl,--wrap,malloc")

FILE(WRITE ${CMAKE_BINARY_DIR}/f.c
"#include <stdio.h>
#include <stdlib.h>
void *__wrap_malloc(size_t n)
{
    printf(\"malloc()\\n\"); return __real_malloc(n);
}\n")
ADD_LIBRARY(f STATIC f.c)
TARGET_LINK_LIBRARIES(f importedempty)

FILE(WRITE ${CMAKE_BINARY_DIR}/main.c
"int main(void){void *p=malloc(1); return 0;}\n")
ADD_EXECUTABLE(main main.c)
TARGET_LINK_LIBRARIES(main f)

The library f with the symbol(s) to be wrapped is linked against a
reimported empty static library which pulls in the required --wrap
flag(s) via its IMPORTED_LINK_INTERFACE_LIBRARIES target property.
The latter does not exists for ordinary static libraries, but
linking against an empty one doesn't do any harm.

Perhaps, this is worth a feature request, e.g. a new target property,
say, TRANSITIVE_LINK_FLAGS[_<CONFIG>] which are added to the linker
command if a binary is linked against the respective library.

'hope that helps.

Regards,

Michael

@Łukasz: Please don't drop the ML.


More information about the CMake mailing list