[CMake] How to use 'CMAKE_EXE_LINKER_FLAGS' correctly

Chuck Atkins chuck.atkins at kitware.com
Mon May 23 10:04:51 EDT 2016


If you know the full path to the library then I'd suggest using it as
/path/to/foo/libfoo.a (or .so if it's shared) instead of -L/path/to/foo
-lfoo.  CMake will adjust the flags to -L and -l if needed but the
preference is always to use the full library path.

- Chuck

On Mon, May 23, 2016 at 2:23 AM, Chaos Zhang <zcsd2012 at gmail.com> wrote:

> Thanks a lot! `set(FOO_LIBRARY "-Wl,--whole-archive ${FOO_LIBRARY}
> -Wl,--no-whole-archive")` worked when the content of ${FOO_LIBRARY} look
> like `-Ldir -llib`, if just use lib name as the content of ${FOO_LIBRARY},
> error while occur.
>
> Chuck Atkins wrote
> >> -rpath
> >
> >
> >  RPATHs are automatically added by CMake to executables so they can use
> > libraries from the build tree.
> >
> >
> >
> >> -whole-archive
> >>
> >
> > whole-archive is definitely trickier since you shouldn't be applying it
> to
> > the entire executable but instead wrapping individual libraries with it.
> > Conveniently, you can pass link options directly with
> > target_link_libraries.  So you could have:
> >
> > # Just an example, find_library calls should really be isolated to
> > separate
> > find modules
> > find_library(FOO_LIBRARY foo)
> > set(FOO_LIBRARY "-Wl,--whole-archive ${FOO_LIBRARY}
> > -Wl,--no-whole-archive")
> >
> > add_executable(hello main.c)
> > target_link_libraries(hello ${FOO_LIBRARY})
> >
> >
> >>> set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L . -lhello")
> >>
> >
> > CMAKE_EXE_LINKER_FLAGS should work for other more general cases that
> > aren't
> > tied to specific libraries.  The problem with your example is likely not
> > using an absolute path for -L since the compilation is actually taking
> > place in a nested work directory somewhere.
> >
> > --
> >
> > Powered by www.kitware.com
> >
> > Please keep messages on-topic and check the CMake FAQ at:
> > http://www.cmake.org/Wiki/CMake_FAQ
> >
> > Kitware offers various services to support the CMake community. For more
> > information on each offering, please visit:
> >
> > CMake Support: http://cmake.org/cmake/help/support.html
> > CMake Consulting: http://cmake.org/cmake/help/consulting.html
> > CMake Training Courses: http://cmake.org/cmake/help/training.html
> >
> > Visit other Kitware open-source projects at
> > http://www.kitware.com/opensource/opensource.html
> >
> > Follow this link to subscribe/unsubscribe:
> > http://public.kitware.com/mailman/listinfo/cmake
>
>
>
>
>
> --
> View this message in context:
> http://cmake.3232098.n2.nabble.com/How-to-use-CMAKE-EXE-LINKER-FLAGS-correctly-tp7593495p7593518.html
> Sent from the CMake mailing list archive at Nabble.com.
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160523/9f063fec/attachment.html>


More information about the CMake mailing list