Visual Studio 2010 library linking order

前端 未结 4 1241
北海茫月
北海茫月 2020-12-19 16:54

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 librar

4条回答
  •  我在风中等你
    2020-12-19 17:46

    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
    

提交回复
热议问题