[CMake] linking to arbitrary libraries in linux.

Christopher Harvey arbuckle911 at gmail.com
Thu Feb 28 00:36:39 EST 2008


Philip Lowman wrote:
> On Wed, Feb 27, 2008 at 8:08 PM, Christopher Harvey
> <arbuckle911 at gmail.com <mailto:arbuckle911 at gmail.com>> wrote:
>
>     Philip Lowman wrote:
>     > On Wed, Feb 27, 2008 at 7:13 PM, Christopher Harvey
>     > <arbuckle911 at gmail.com <mailto:arbuckle911 at gmail.com>
>     <mailto:arbuckle911 at gmail.com <mailto:arbuckle911 at gmail.com>>> wrote:
>     >
>     >     Hi,
>     >     Before I ask my question I want to clarify what I mean by an
>     >     "arbitrary
>     >     library". There are libraries that are registered in the system
>     >     and you
>     >     can link to them like so:
>     >     target_link_library(targetName GL SDL pq)
>     >     and they get sent to the linker int the form:
>     >     -lGL -lSDL -lpq
>     >     that fine, but I have a file in the form libName.a and I
>     compiled it
>     >     from source and it's not in the /usr/lib directory. It's just
>     >     sitting in
>     >     my project directory. how can I tell cmake not to turn
>     >     target_link_libraries(targetName libName.a) into -lName, but
>     keep the
>     >     original so the linker can find it?
>     >
>     >
>     > This works for me in CMake 2.4.8:
>     >
>     > TARGET_LINK_LIBRARIES(target ${PROJECT_SOURCE_DIR}/libFoo.a)
>     >
>     >
>     >
>     > /usr/bin/c++      -fPIC "CMakeFiles/bar.dir/bar.o"   -o bar
>     -rdynamic
>     > -L/home/lowman/tmp/foo -Wl,-Bstatic -lfoo -Wl,-Bdynamic
>     > -Wl,-rpath,/home/lowman/tmp/foo
>     >
>     >
>     > --
>     > Philip Lowman
>     Hi Philip,
>     thanks, that's almost working. Now I've got:
>     target_link_libraries(target ../foolib/libFoo.a)
>
>     this produces:
>     /usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/../../../../x86_64-pc-linux-gnu/bin/ld:
>     cannot find -l../foolib/libFoo.a
>
>     It would work if it looked for "../foolib/libFoo.a" and not
>     "-l../foolib/libFoo.a"
>     Any idea as to how to fix this?
>
>
> If you're trying to link against a library that's outside of your
> source tree, you should always use FIND_LIBRARY then
> TARGET_LINK_LIBRARIES.  FIND_LIBRARY will almost always do exactly
> what you want if you give it the NO_DEFAULT_PATHS option and specify
> the path to the library you want to find.  In the event you have a
> ".so" and ".a" in the same directory you should be made aware that it
> always prefers the ".so".  This behavior has been fixed in CMake CVS
> so you can specify the ".a" file directly if you encounter this situation.
>
> If you're trying to link against a library that's inside your source
> tree you can reference it relative to ${PROJECT_SOURCE_DIR} or one of
> the other variables that defines directories in your source tree, like
> ${CMAKE_CURRENT_SOURCE_DIR}.  Apparently, relative paths will not work
> for this though (as you've discovered).  See this page for details on
> variables you can use:
>
> http://www.cmake.org/Wiki/CMake_Useful_Variables
>
>
>
> -- 
> Philip Lowman
>
>
>
> -- 
> Philip Lowman
> ------------------------------------------------------------------------
>
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake
Ok, I see what's going on now. Works like a charm. Thanks again.

Chris.


More information about the CMake mailing list