[CMake] Ensuring correct linkage to local target library

Michael Hertling mhertling at online.de
Sat Sep 18 21:46:15 EDT 2010


On 09/18/2010 05:37 PM, Clifford Yapp wrote:
> I have a situation where there exists on some systems a library
> (/lib/librt.so.1) that conflicts with the name we use for BRL-CAD's
> raytracing library (also librt).  For various reasons, I can't rename
> our lib - this means I have to deal with the situation where there
> exists a system library with the correct name but incorrect contents.
> 
> Generally, we install into directories with a prefix (e.g.
> /usr/brlcad) to avoid stomping over system libs.  However, when it
> comes to defining target_link_libraries lines for our executables, I
> end up with something like:
> 
> add_executable(rt ${rt_SRCS})
> target_link_libraries(rt librt)
> 
> This works splendidly, particularly combined with the RPATH settings
> that let me run the executables both in the build and install
> locations without jumping through hoops.  However, the linker
> (justifiably) is confused by the presence of the system librt file
> when it is present.
> 
> Is there a way to specify my local librt to the target_link_libraries
> command in such a way that avoids any chance of picking up the system
> librt, but still preserves the nice properties of running executables
> both in build and install directories cleanly?

Does setting the INSTALL_RPATH target property on rt work? E.g., with

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(RT C)
FILE(WRITE ${CMAKE_BINARY_DIR}/f.c "void f(void){}\n")
ADD_LIBRARY(librt SHARED f.c)
SET_TARGET_PROPERTIES(librt PROPERTIES OUTPUT_NAME rt)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c
    "int main(void){f(); return 0;}\n")
ADD_EXECUTABLE(rt main.c)
TARGET_LINK_LIBRARIES(rt librt)
SET_TARGET_PROPERTIES(rt PROPERTIES INSTALL_RPATH
    ${CMAKE_INSTALL_PREFIX}/lib)
INSTALL(TARGETS rt librt
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib)

I can see rt linking against its librt - in the build directory as well
as in the install directory - but without the INSTALL_RPATH setting, rt
in the install directory links against the system's librt, so it fails,
whereas in the build directory, it still links against its own librt.

Regards,

Michael


More information about the CMake mailing list