0

I am trying to link a project that has 2 libraries that share functions (one is a branch of the other) as well as having unique functions necessary to the program. Because they share functions, the linker complains about multiple definitions. Is there a way in MSVC to force "use the first one you come across" rather than have to change something architecturally?

user1043761
  • 696
  • 6
  • 22
  • This is how static libraries should normally work. Are you using DLLs, or just including object files in the project? – n. m. could be an AI Aug 07 '18 at 05:47
  • I am statically linking to these libraries (MT versions of the .libs), no DLLs – user1043761 Aug 07 '18 at 05:50
  • Maybe could help: https://stackoverflow.com/questions/2765403/visual-studio-2010-library-linking-order – meguli Aug 07 '18 at 06:11
  • .lib can be an import library as well as a static library. If you have a duplicate function in the same source file with some unique function, you may get this error, but this is rare. Or if you use /WHOLEARCHIVE, stop using it. – n. m. could be an AI Aug 07 '18 at 06:14
  • @meguli - /FORCE:MULTIPLE linker option may have solved it, appears to ignore 2nd definitions of previously defined functions. Somebody describe this in an answer and I'll accept it. – user1043761 Aug 07 '18 at 06:54

1 Answers1

1

In simple terms, you need to use /FORCE:MULTIPLE linker option to make VC++ generate a valid exe or dll file, even if there are multiple definitions. You need to add this to:

Properties->Linker->Command Line->Additional Options

You can check the MSDN documentation to see the caveats. Generated file may not work as expected and there is nothing in the documentation saying that linker will take the first definition it sees, though this is probably the case. If you use /FORCE without any options, it works as if you passed the both options /FORCE:MULTIPLE AND /FORCE:UNRESOLVED.

For a more fine grained solution where you can be sure that linker takes the first definition it sees, take a look at the following answer:

Visual Studio 2010 library linking order

meguli
  • 1,426
  • 1
  • 18
  • 35