1

After much struggling with attempting to link a C++ project to a 3rd party static library (.lib), I think I have solved the issue by verifying that I compile my project using the same compiler in which the .lib file was compiled (MSVC11).

Until this point, I assumed all .obj files were equivalent, thus I could take a .lib file (containing various .objs), and use that with any other project I might want to develop in the future. However, this was an incorrect assumption.

So I'm curious as to why (in the context of MSVC) .obj files differ from one version of the compiler to the next. Assuming you're targeting an x86 application, shouldn't the obj files be comprised of the same types of instructions regardless of whether or not you compiled using MSVC11/12/14?

TLDR; Why can't I link a project to an .obj that was created using a different MSVC compiler?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Izzo
  • 4,461
  • 13
  • 45
  • 82
  • 4
    In short: because of ABI. In long - read about ABI :) – SergeyA Aug 24 '16 at 18:28
  • "Why does a `.doc` differ from a `.docx` or even a `.doc` from a different MS-Office version"? – too honest for this site Aug 24 '16 at 18:34
  • Just to stretch the point, would you expect to link an object file made in 1980? – Weather Vane Aug 24 '16 at 18:36
  • See [this](http://stackoverflow.com/questions/15836087/do-visual-studio-2012-updates-break-c-abi). Since VC++ STL can (and will) break binary compatibility between major versions your old library will fail to link against it. If the library doesn't use the standard library and the ABI and mangling scheme don't change, it'll most likely work though. – Andrea Biondo Aug 24 '16 at 18:43
  • 1
    @WeatherVane I do link a 32b `C` static `.lib` made in the early '90s and it works just fine. For plain `C` the issue is not the ABI, but the CRT. – dxiv Aug 24 '16 at 18:49
  • 1
    See: [Do Visual Studio 2012 updates break C++ ABI?](http://stackoverflow.com/q/15836087) and [How can I use Standard Library (STL) classes in my dll interface or ABI?](http://stackoverflow.com/q/5661738), and [Breaking Changes in Visual C++ 2015](https://msdn.microsoft.com/en-us/library/bb531344.aspx) (as well as "Other Versions"). It is honestly quite difficult for me to imagine why you would think that intermediate files compiled by one version of a compiler would be expected to work with a completely different version. That might be a nice feature, but it isn't something you can assume. – Cody Gray - on strike Aug 24 '16 at 20:08

1 Answers1

1

That's because it could be linked to another version of Visual C++ runtime libraries, which is incompatible with the version you are using.

This problem could be even with DLLs if you try to expose C++ objects from it.

Anton Malyshev
  • 8,686
  • 2
  • 27
  • 45