[CMake] target_link_libraries: prefer static to dynamic
Michael Wild
themiwi at gmail.com
Mon Dec 28 04:32:16 EST 2009
On 28. Dec, 2009, at 5:38 , Bill Hoffman wrote:
> Pau Garcia i Quiles wrote:
>> On Sun, Dec 27, 2009 at 6:26 PM, Bill Hoffman <bill.hoffman at kitware.com> wrote:
>>> Pau Garcia i Quiles wrote:
>>>> Hello,
>>>>
>>>> I think this has already been discussed and the answer is negative but
>>>> still: when I do target_link_libraries to an external library (for
>>>> instance, my application needs to link to sqlite), is it possible to
>>>> tell CMake to link to the static version of sqlite instead of the
>>>> dynamic version?
>>>>
>>>> For instance, in Debian the libsqlite3-dev package contains both the
>>>> static and the dynamic versions of the library and they have the very
>>>> same name:
>>>>
>>>> /usr/lib/libsqlite3.a
>>>> /usr/lib/libsqlite3.so
>>>>
>>>> ( http://packages.debian.org/sid/i386/libsqlite3-dev/filelist )
>>>>
>>>> Is this feature going to be implemented any time soon? I have not
>>>> looked at the sources for target_link_libraries but at first sight it
>>>> doesn't look difficult to add something like
>>>> "static;optimized;libsqlite3.a;dynamic;optimized;libsqlite3.so".
>>>> There's the problem of find_library on Windows confusing the .lib as
>>>> the import for a .dll with a static .lib library but IIRC it's
>>>> possible to detect that.
>>>>
>>> If you use the full path then it should work fine. find_library will always
>>> return the full path. So if you do target_link_libraries(foo ${SQLITELIB})
>>> where SQLITELIB = /usr/lib/libswlite3.a then it will work just fine.
>> That will not work in the case I was describing, where you have both
>> the shared and the static versions of the library installed. Futher,
>> it would be even worse if both the static and the shared versions of
>> the library are installed but in different paths, because you would
>> depend on what path is searched first.
>> As I said if you install libsqlite3-dev on a Debian-based distro, it
>> will install both /usr/lib/libsqlite3.a and /usr/lib/libsqlite3.so.
>> How can I be sure find_library will find the static version before the
>> dynamic one. What happens if the path to the static library is
>> searched after the path to the shared library?
>
> What version of CMake are you using? This should work... As of 2.6.2 (I think...) CMake uses full paths to libraries, see policy CMP0003: http://www.cmake.org/cmake/help/cmake-2-8-docs.html#policy:CMP0003. It does not matter if there are two libraries in the same directory. There is no way to force find_library to find the static one. However, if you tell CMake to link to a full path to /usr/lib/libsqlite3.a, it should link to /usr/lib/libsqlite3.a even if /usr/lib/libsqlite3.so exists.
>
> -Bill
>
I think there is a misunderstanding... Pau wants it to happen automagically, whereas Bill is referring to overriding the automatic choice manually in the CMakeCache.txt file (be it by using -D flags, editing the file, using ccmake or cmake-gui or a cache-initialization file).
Michael
More information about the CMake
mailing list