[CMake] Magical transformation of /path/to/libmylib.so to -lmylib

Michael Hertling mhertling at online.de
Thu Jul 14 00:04:06 EDT 2011


On 07/14/2011 12:54 AM, Jens Mueller wrote:
> Michael Hertling wrote:
>> On 07/11/2011 11:34 PM, Jens Mueller wrote:
>>> Hi,
>>>
>>> I'm trying to figure out where the path for a found library is changed.
>>> In my case Curses is found at /usr/lib/libcurses.so. When linking with
>>> gcc "/usr/lib/libcurses.so" is replaced by "-lcurses" as it is specified
>>> at http://www.cmake.org/Wiki/CMake_2.6_Notes#Linking_to_System_Libraries.
>>> Where can I find the code that causes these changes? I looked into
>>> share/Modules/ at various CMake* files but I'm unable find the code.
>>>
>>> Jens
>>
>> The system directories are identified during the initial detection
>> of the compiler ABI ("Detecting <LANG> compiler ABI info"), see
>>
>> Modules/CMakeDetermineCompilerABI.cmake
>> Modules/CMakeParseImplicitLinkInfo.cmake
>>
>> w.r.t. the CMAKE_<LANG>_IMPLICIT_LINK_DIRECTORIES variables.
>>
>> AFAIK, the actual conversion of the full path to a -l option
>> is performed in Source/cmComputeLinkInformation.cxx by:
>>
>> cmComputeLinkInformation::AddFullItem()
>> cmComputeLinkInformation::CheckImplicitDirItem()
>> cmComputeLinkInformation::AddUserItem()
>>
>> Previously, the underlying cmComputeLinkInformation::ImplicitLinkDirs
>> set is populated by cmComputeLinkInformation::LoadImplicitLinkInfo()
>> from the CMAKE_<LANG>_IMPLICIT_LINK_DIRECTORIES variables' contents.
> 
> Thanks very much. This helped a lot. I see much clearer now. The
> compiler I'm using uses gcc for linking. I made sure that the implicit
> link directories are properly extracted. But somehow I need to
> trigger/allow the conversion of full path to -l. It does not work
> automatically. Do I have to configure this for a new language/new
> compiler?
> 
> Jens

Although I don't get your drift, you might use imported targets to
force linking against full paths as well as against -l switches:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(IMPORTED C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
LINK_DIRECTORIES($ENV{HOME}/lib)
ADD_LIBRARY(f SHARED IMPORTED)
ADD_LIBRARY(g SHARED IMPORTED)
SET_TARGET_PROPERTIES(f PROPERTIES
    IMPORTED_LOCATION /usr/lib/libncurses.so)
SET_TARGET_PROPERTIES(g PROPERTIES
    IMPORTED_LOCATION -lncurses)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c)
TARGET_LINK_LIBRARIES(main f -L/usr/lib g)

The -L switches can be placed immediately in TARGET_LINK_LIBRARIES() or
enabled by LINK_DIRECTORIES(), but be aware that -L may be dangerous in
a subtle manner, see [1].

Regards,

Michael

[1] http://www.mail-archive.com/cmake@cmake.org/msg33519.html


More information about the CMake mailing list