1

There is a static library which is used by the main program indirectly. Therefore there is no missing reference and the static library is not linked.

The same problem was posted here for the GCC. And the proposed solution worked for me as well. But now I need to build the program with MSVC. So I suppose that I need the equivalent of GCC's --whole-archive parameter?

At the moment the linker call is like that: link.exe /nologo /out:program.exe staticLib.lib main.obj

Can someone help me with that?

Community
  • 1
  • 1
Nikolaus Ammann
  • 507
  • 1
  • 4
  • 12

1 Answers1

0

I recently met with the same problem while trying to link googletest with library containing the tests and found the following note in their primer :

When you define your tests, Google Test creates certain static objects to register them. These objects are not referenced from elsewhere but their constructors are still supposed to run. When Visual C++ linker sees that nothing in the library is referenced from other places it throws the library out. You have to reference your library with tests from your main program to keep the linker from discarding it. Here is how to do it. Somewhere in your library code declare a function:

__declspec(dllexport) int PullInMyLibrary() { return 0; } 

If you put your tests in a static library (not DLL) then __declspec(dllexport) is not required. Now, in your main program, write a code that invokes that function:

      int PullInMyLibrary();
      static int dummy = PullInMyLibrary(); 

This will keep your tests referenced and will make them register themselves at startup.

In addition, if you define your tests in a static library, add /OPT:NOREFto your main program linker options.

While this may be no the most beautiful workaround ever this certainly worked for me and I think if there would be the one better they would have mentioned it in their primer instead.

Predelnik
  • 5,066
  • 2
  • 24
  • 36
  • Also see http://stackoverflow.com/questions/3867254/what-is-the-visual-studio-equivalent-to-gnu-ld-option-whole-archive – Predelnik Apr 02 '15 at 08:35
  • This is not a bug, despite the claim on the page to which you link. – James McNellis Apr 03 '15 at 05:46
  • @JamesMcNellis yeah, I've tested it on linux and it looks like it doesn't work without `--whole-archive` either. Guess better solution is to organize program in different way after all. – Predelnik Apr 03 '15 at 07:36