[CMake] How to apply the --as-needed linker flag?

Michael Hertling mhertling at online.de
Sun Nov 28 00:39:29 EST 2010


On 11/27/2010 06:45 PM, Alan W. Irwin wrote:
> I just discovered that many Linux distros these days use the
> --as-needed Linux linker option by default.  At first glance that
> option makes a lot of sense since it tends to reduce startup times.
> But I guess there are some caveats as well which is probably why CMake
> does not adopt this linker option by default for Linux builds.
> However, I would at least like to try this option for my own Linux
> builds without forcing it using target_link_libraries. Is it possible
> to specify linker options such as --as-needed using environment
> variables and/or -D options?

On Linux, CMake takes account of the LDFLAGS environment variable
for the initial configuration of the build directory, so saying

LDFLAGS="-Wl,--as-needed" cmake <path/to/srcdir>

enables "--as-needed" for this build directory - forever.

If you need more flexibility you might say, e.g.,

SET_TARGET_PROPERTIES(... PROPERTIES LINK_FLAGS "$ENV{MYLDFLAGS}")

and use the MYLDFLAGS environment variable to reconfigure later:

MYLDFLAGS="-Wl,--no-as-needed" cmake <path/to/bindir>

'hope that helps.

Regards,

Michael


More information about the CMake mailing list