55

Some projects in my solution produce this linker warning:

MSIL .netmodule or module compiled with /GL found; restarting link with /LTCG; add /LTCG to the link command line to improve linker performance

I'm using Visual Studio 2013 Update 3. I haven't yet been able to identify anything particular to those projects that could cause this.

What is it about those projects that produces this?


I've looked at this: http://msdn.microsoft.com/en-us/library/k669k83h.aspx but I'm not aware we are using any CLR, managed code, /LN or /NOASSEMBLY.

Scott Langham
  • 58,735
  • 39
  • 131
  • 204
  • 10
    My crystal ball says that you are getting this warning in the Debug build of your project from linking a static .lib that was built with Release configuration settings. Which has /GL turned on by default. That's usually pretty bad news beyond just this warning, YMMV. – Hans Passant Oct 09 '14 at 13:33

4 Answers4

56

I had the same problem, so I did some research.

According to https://msdn.microsoft.com/en-us/library/0zza0de8.aspx :

If you compile your program with /GL and /c, you should use the /LTCG linker option to create the output file.

So the message can be a bit misleading - the problem is not the MSIL .netmodule, but modules compiled with /GL

When using /GL, you tell the compiler to delay the generation of some code namely around function bounderies, in order to optimise them. LTCG instruct the linker to generate (and optimise) the missing code. Otherwise, the program will not run as expected.

Basically, the two switches should be used together (when used). They apply to different parts of the build: one for compilation and the other one for link.

For completeness:

  • /GLis controlled from Configuration Properties > C/C++ > Optimization > Whole Program Optimization

  • /LTCG is controlled from Configuration Properties > Linker > Optimization > Whole Program Optimization

On later versions,

  • /LTCG is controlled from Configuration Properties > Linker > Optimization > Link Time Code Generation / Use Link Time Code Generation (/LTCG)
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
Ionel POP
  • 743
  • 7
  • 12
  • 4
    The setting is not "Whole Program Optimization". It is named "Use Link Time Code Generation". – Elmue Aug 28 '18 at 00:02
14

I have encountered the same error and spent quite a lot of time trying to fix it. Finally, I figured out that it appeared due to the use of "Whole Program Optimization" option in one of my dependency libraries.

By default, this option is set to "Yes" in newly created projects. When I changed it to "No" and recompiled all dependencies, the warning disappeared. I have purely native C++ solution without any managed code.

To fix, open project settings of all dependency projects and check setting:

Configuration Properties > C/C++ > Optimization > Whole Program Optimization

Make sure it is set to "No" everywhere.

Boris Zinchenko
  • 2,142
  • 1
  • 24
  • 34
  • 2
    Not quite. While this makes the warning go away, it also disables several optimizations. It is better to enable the "Link Time Code Generation" setting. – Simon Richter May 25 '15 at 07:37
  • 4
    "Link Time Code Generation" setting is a better alternative. However, it is not always convenient because it is not a default setting. Imagine that you supply a library for a wide audience. You must write then an instruction on adjusting settings in every end project consuming the library. Potential users of the library might grow suspicious and simply select not using it in their projects. Changing setting in C++ options harms the efficiency but allows to reach compatibility to defaults. Which scenario to choose everybody should decide individually depending on concrete circumstances. – Boris Zinchenko May 26 '15 at 08:53
  • 1
    Erm, "Link Time Code Generation" is default in Release builds -- otherwise it would make no sense to have "Whole Program Optimization" as default. – Simon Richter May 26 '15 at 21:23
  • 3
    Just for an experiment, I ve create a new sample C++ console application in VS 2013. It does not contain /LTCG among linker options. – Boris Zinchenko May 28 '15 at 05:49
  • 3
    Additional input: according to this [Visual C++ Team Blog](https://blogs.msdn.microsoft.com/vcblog/2009/02/24/quick-tips-on-using-whole-program-optimization/) you should "Never use /GL for code you intend to put in a library and ship to your customers" – Reinier Torenbeek Feb 14 '18 at 23:00
3

I find the same error goes away by telling the linker about /GL setting you have used:

Set ... Configuration Properties/Linker/Optimization/Link Time Code Generation To ... One of the non-Default settings

Maybe https://msdn.microsoft.com/en-us/library/xbf3tbeh.aspx is of some use?

Neil

Neil Gatenby
  • 419
  • 6
  • 21
1

This message shows a lot, which is really raising suspicion. I use a property sheet which tells both /GL and /LTCG. The project isn't using any external libraries. And I get this message, which doesn't make any sense. It disappears, if I go to the project properties and specify "Use Link Time Code Generation" again from there. It doesn't change the command line or anything, but just makes VC happy...

jj99
  • 300
  • 1
  • 11