Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2010 library linking order

How do you specify, in Visual Studio 2010, the order in which library files should be linked?

I have a project that links against libexpat and against another library. This library (not under my control) seems to also include libexpat. The problem is that 'we' use a different version of the library (XML_UNICODE vs not). In Visual Studio 2008 things seemed to work out okay (might have been a coincidence), but in Visual Studio 2010 the wrong instance of libexpat is linked. I was thinking that if I could specify the order in which these two libraries should be linked that then I could circumvent the problem.

like image 295
Randy Voet Avatar asked May 04 '10 12:05

Randy Voet


2 Answers

A few years back I discovered a hack that allows you to force Visual C++ to link libraries with a specific precedence. This is not elegant, but it is functional.

It seems that the linker for Visual C++ generates link order on the fly based on symbol dependencies. By adding a symbol reference up-front, you can force the linker to include the first library specified in the linker input. Please note, I have only tested this with Visual C++ 6 and 8 (2005).

Let's say for example that you have two libraries with the symbol XML_ParserCreate:

  • libexpat.lib - XML_ParserCreate
  • someother.lib - OtherSymbolsYouNeed, XML_ParserCreate

First, order your library dependencies as you would expect, libexpat.lib and then someother.lib. Via command line these would be options to link.exe. In Visual Studio 2005, they would be options under the project's Configuration Properties -> Linker -> Input -> Additional Dependencies. I would imagine Visual C++ 2010 has a similar menu.

Next, add a command line option that defines a known repeated symbol up-front, by using the /INCLUDE linker option. In Visual Studio 2005, this can be added under the project's Configuration Properties -> Linker -> Command Line -> Additional options:

/out some.exe ... libexpat.lib someother.lib
/include:XML_ParserCreate

The definition of this symbol will cause the linker to immediately prefer the first library that terminates (realizes) it. In general Visual C++ will generate an error with repeated symbols; if you haven't already, make sure you are also specifying the /FORCE:MULTIPLE linker option.

My specific need for this was using the DUMA memory debugging library. It defines a variety of memory functions that are also defined in libcmtd.lib. The following would incorrectly link libcmtd's version of _malloc, despite a library order that seems to the contrary:

/out some.exe ... duma.lib libcmtd.lib
/FORCE:MULTIPLE

This was resolved by manually adding the symbol, and has worked reliably for years:

/out some.exe ... duma.lib libcmtd.lib
/INCLUDE:_malloc /FORCE:MULTIPLE
like image 98
Terrance Avatar answered Nov 06 '22 00:11

Terrance


I have found 'a' solution: if you add the libraries through #pragma comment(lib... the order of linking is the same as the order in which you type those pragma's. I'm still keeping the question open for a solution when the libraries are added through the project file instead of through pragma statements.

like image 44
Randy Voet Avatar answered Nov 05 '22 23:11

Randy Voet