[CMake] Library search path ordering

William A. Hoffman billlist at nycap.rr.com
Thu Aug 25 09:23:32 EDT 2005


The reason it is needed is that often times libraries are specified with full
paths to cmake.   And not as -L and -l, but rather /full/path/libA.a /full/path/libB.a.
However, to work with linkers cmake splits those into -L/full/path and -lA -lB, the
order of the -l's must be preserved.  However, the -L may need to be reordered so
that the correct library is found.   Say you have something like this:

/A/liba.a
/A/libb.a
/B/libb.a

Then you tell cmake to link /A/liba.a and /B/libb.a, in the old cmake
you got:  -L/A -L/B -la -lb, and the /A/libb.a was used instead of the
specified /B/libb.a.  With the new cmake, you will get -L/B -L/A -la -lb,
and you will get the libraries specified.   In most cmake projects libraries
are specified as full paths because FIND_LIBRARY is used.    If it is a library
that is being built by cmake, then you just have to use the library name.

-Bill


At 07:34 AM 8/25/2005, David Flitney wrote:
>Bill,
>
>Thanks for that. If I understand correctly, what you're implying here is that I'll need to use:
>
>TARGET_LINK_LIBRARIES(Project ${Project_BINARY_DATA}/lib/libmiscmaths,a)
>
>to force my preferred ordering? I hadn't realised this was possible/necessary. Previously with CMake 2.0.6 - and also with 2.2 on non MacOSX platforms - I got the correct ordering without this (just lucky?).
>
>Okay, that appears to work. It does leave me a little confused as to why there is a need for a configuration time reordering. Surely the developer could do this work through the order in the CMakeLists.txt file? As appears to be the case with the INCLUDE_DIRECTORIES directives.
>
>Thanks again,
>
>Dave
>
>On 24 Aug 2005, at 22:17, William A. Hoffman wrote:
>
>>To try and use -L and to make sure that full paths to libraries specified are
>>used, cmake reorders paths using the following algorithm:
>>
>>
>>/** \class cmOrderLinkDirectories
>> * \brief Compute the best -L path order
>> *
>> * This class computes the best order for -L paths.
>> * It tries to make sure full path specified libraries are
>> * used.  For example if you have /usr/mylib/libfoo.a on as
>> * a link library for a target, and you also have /usr/lib/libbar.a
>> * and you also have /usr/lib/libfoo.a, then you would
>> * want -L/usr/mylib -L/usr/lib to make sure the correct libfoo.a is
>> * found by the linker.  The algorithm is as follows:
>> * - foreach library create a vector of directories it exists in.
>> * - foreach directory create a vector of directories that must come
>> *   after it, put this in a map<dir, vector<dir>> mapping from a directory
>> *   to the vector of directories that it must be before.
>> * - put all directories into a vector
>> * - sort the vector with a compare function CanBeBefore
>> *   CanBeBefore returns true if a directory is OK to be before
>> *   another directory.  This is determined by looking at the
>> *   map<dir vector<dir>> and seeing if d1 is in the vector for d2.
>>
>>
>>Can you be more specific about all the full path libraries you are using, and
>>are there any warnings from cmake about not being able to come up with a correct order?
>>
>>-Bill
>>
>>
>>At 01:07 PM 8/24/2005, David Flitney wrote:
>>>OS: MacOSX 10.3.9 (Panther)
>>>CMake Version: 2.2-beta
>>>
>>>I'm having a problem with -L directives. They are being reordered (optimised :-) ?) such that I can't control which gets searched first, e.g., say I have a locally built library:
>>>
>>>${Project_BINARY_DIR}/lib/libmiscmaths.a
>>>
>>>but also there is an older version in /usr/local/maths/lib then I can't ensure that my locally built one is linked against.
>>>
>>>I've tried:
>>>
>>>LINK_DIRECTORIES(${Project_BINARY_DIR}/lib /usr/local/maths/lib)
>>>TARGET_LINK_LIBRARY(Project miscmaths)
>>>
>>>but this ordering isn't being obeyed.
>>>
>>>-- 
>>>Cheers, Dave
>>>
>>>Dave Flitney, IT Manager
>>>Oxford Centre for Functional MRI of the Brain
>>>E:flitney at fmrib.ox.ac.uk W:+44-1865-222713 F:+44-1865-222717
>>>URL: http://www.fmrib.ox.ac.uk/~flitney
>>>
>>>_______________________________________________
>>>CMake mailing list
>>>CMake at cmake.org
>>>http://www.cmake.org/mailman/listinfo/cmake
>>
>-- 
>Cheers, Dave
>
>Dave Flitney, IT Manager
>Oxford Centre for Functional MRI of the Brain
>E:flitney at fmrib.ox.ac.uk W:+44-1865-222713 F:+44-1865-222717
>URL: http://www.fmrib.ox.ac.uk/~flitney



More information about the CMake mailing list