[CMake] MSVC71 WholeProgramOptimization flag (/LTCG) fixed?

Luke Kucalaba lkucalaba at dsci.com
Thu Oct 16 11:59:05 EDT 2008


Simon, I completely agree with your assessment.  The problem is entirely
in that the linker flag /LTCG is not being passed to the static library
linker command-line arguments.  For executable projects and shared
libraries (dlls), the /LTCG flag is passed to the linker correctly (it
appears in the "command-line" page "additional options" section).

The LTCG problem is also present evident with NMake builds:

[ 45%] Building CXX object
Util/CMakeFiles/Util-SL.dir/XMLUtil/XMLWriter.cpp.obj
XMLWriter.cpp
Linking CXX static library ..\build\MT-DLL\Util-SL.lib
XMLWriter.cpp.obj : warning LNK4218: non-native module found; restarting
link with /LTCG
[ 45%] Built target Util-SL

What is happening is if you compile with /GL this embeds LTCG info into
the object file, and then when you go to link, the linker recognizes
this LTCG info and immediately turns on the LTCG option automatically
(and issues the warning).  So this really is a harmless (although quite
annoying) warning.  It may slow down link times due to the extra
overhead involved in restarting the initial link process.

My guess as to why this problem may be occurring in CMake is that this
type of "whole program" optimization is special in that you actually
need to link static libraries in a different manner-- after all the code
generation and initial linking is complete, there is another
compiler/linker pass that does additional inlining and other similar
static library interface/boundary optimizations.  LTCG means "link-time
code generation", which unlike any other linker setting invokes a
post-process compiler/linker optimization phase.  Perhaps CMake just
doesn't pass linker flags to static libraries because it doesn't realize
this type of optimization exists?  Typically the linker phase does not
even occur until the very end of the whole process when finally string
together static libraries to create an executable or dll. 

Luke

-----Original Message-----
From: Simon Sasburg [mailto:simon.sasburg at gmail.com] 
Sent: Saturday, October 11, 2008 7:20 PM
To: Luke Kucalaba
Cc: cmake at cmake.org
Subject: Re: [CMake] MSVC71 WholeProgramOptimization flag (/LTCG) fixed?

My findings, with visual studio 2008 express, are that you don't
really need to have WholeProgramOptimization=TRUE to fix this warning.
It suffices to add /LTCG to the link options of all executables and
modules.

Unfortunately I'm not able to get cmake to add the /LTCG flag to the
module link flags.
See also here:
http://www.cmake.org/pipermail/cmake/2008-October/024446.html

Also, it seems weird that this flag is reported not to work in VS8+,
as both VS8 Express, and VS9 Express show the whole program
optimization options in the IDE.



More information about the CMake mailing list