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

Marcel Loose loose at astron.nl
Mon Nov 29 06:31:17 EST 2010


>>> On 28-11-2010 at 9:10, in message
<alpine.DEB.2.00.1011272348290.11806 at ybpnyubfg.ybpnyqbznva>, "Alan W.
Irwin"
<irwin at beluga.phys.uvic.ca> wrote: 
> On 2010-11-28 06:39+0100 Michael Hertling wrote:
> 
>> 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.
> 
> Thanks, Michael, that was exactly what I needed.  I was completely
> unaware that environment variable worked for CMake despite many
years
> of using CMake on Linux.  Is the LDFLAGS environment variable
> documented for CMake anywhere?  I couldn't find it in the
> documentation you get with "cmake --help-full", and it is also not
> documented at http://www.cmake.org/Wiki/CMake_Useful_Variables. The
> useful environment variables CXXFLAGS, CFLAGS, and FFLAGS that allow
> you to specify general compiler flags in a convenient way are
> undocumented as well, and that is a real shame.
> 
> Alan
> __________________________
> Alan W. Irwin
> 
> Astronomical research affiliation with Department of Physics and
Astronomy,
> University of Victoria (astrowww.phys.uvic.ca).
> 
> Programming affiliations with the FreeEOS equation-of-state
implementation
> for stellar interiors (freeeos.sf.net); PLplot scientific plotting
software
> package (plplot.org); the libLASi project (unifont.org/lasi); the
Loads of
> Linux Links project (loll.sf.net); and the Linux Brochure Project
> (lbproject.sf.net).

Hi Alan,

You may find the following links of interest. They discuss the virtues
and risks of using --as-needed in the light of underlinking and
overlinking; and how to fix broken packages in distributions.

http://wiki.mandriva.com/en/Overlinking
http://wiki.mandriva.com/en/Underlinking
http://www.gentoo.org/proj/en/qa/asneeded.xml

Regards,
Marcel Loose.



More information about the CMake mailing list