12

Building with VS2010, I'm building a lib that causes many of these link errors:

error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2'

Resulting in a situation where I have to ship both a release and a debug version of my lib. I have no reason to ship a debug version of the lib, and it just bloats the binary distribution. But client code built in debug refuses to link against my release lib.

I've seen this question appear before, but they don't seem to be asking the right question. I understand what this error is, and why I'm getting it (well, sort of; I'm not sure precisely what emits the dependency. Do you?), but what I want to know is how to eliminate this dependency from occurring in my lib?

Similarly to libs which complain when conflicting CRT's are used, which can be prevented with /Zl (Omit default library name from object files), surely there's a way to prevent this dependency from being emitted into my libs too?

I simply want to produce a single optimised lib, which is capable of linking against either debug or release code. It's not important for client code to debug the lib. Almost no 3rd party libraries ship with distinct debug and release versions. How is it that vendors avoid this problem?

Does anyone know precisely what causes this link dependency, and how to I either disable it entirely, or factor it out of my code?

4
  • did you figure this out? I don't consider the answers below as answers. If you're producing a LIB why is it even linking in the 1st place? if you produce a DLL along w/ an associated import LIB, then people will be able to use it whether they are debug or release. Commented Apr 19, 2018 at 23:31
  • Yes, I misunderstood the problem. This is just a further extension of MS's insane CRT situation. STL has template-ed structures whose layout changes according to the state of the _ITERATOR_DEBUG_LEVEL macro when you compile your code. If you include any STL headers then your code is hard-coded to that particular state of _ITERATOR_DEBUG_LEVEL, and it simply CAN NOT interact with code built differently. It's not about debug/release, it's about different struct layouts embedded in your compiled code. Further, the MS dbg/release C++ libs are built for one state or the other. Commented Apr 21, 2018 at 1:05
  • Basically, if you include any STL header in your code, then you accept a commitment to build a suite of libs with each structure layout. What you CAN do, if you don't want to ship debug/unoptimised code, is to do 2 release builds with _ITERATOR_DEBUG_LEVEL manually defined to each value and then name ship them as debug/release appropriately. Even though they're both really 'release' builds, the distinction is required to match STL data structures for the client linking application. Commented Apr 21, 2018 at 1:07
  • TL;DR: there is no 'workaround' other than "don't include any STL headers anywhere in your code" Commented Apr 21, 2018 at 1:12

4 Answers 4

11

You are linking some code that was compiled with debug build settings with iterator debugging enabled with some other code that was compiled with release build setting.

This link error stops you from blowing your leg off like that. Well, both legs, left arm and index finger of your right hand, you'll have to operate the mouse with your chin. The runtime errors you get from, say, std::string objects having a different layout are very hard to diagnose. Heap corruption is a nasty problem, about the worst to debug. You have to recompile the code with identical settings, no other way.

Sign up to request clarification or add additional context in comments.

3 Comments

I understand the problem, but it doesn't answer the question. Is there a way to stop the compiler from emitting code that is distinct for each configuration? Most libs don't ship with a debug and release variant pair. Are you saying it's impossible to link a release lib to a debug app? That can't be right, I do it all the time.
Yes, didn't answer the question. I'm looking for a solution to this as well. Not sure why it got 2 upvotes.
"No other way" appears to be an oddly mysterious phrase. It isn't often that I use words that explicit. The answer is crystal clear, that it doesn't happen to please you is not my mistake. Bitch at Microsoft about this.
1

Same issue but I was writing regression tests and scripting command line compile and link. I just forgot /D_DEBUG on the compile line when doing a debug build.

Comments

0

One of the things I found out when trying to compile a set of boost code I found elsewhere was that internally it had defined _SECURE_SCL=1, and _HAS_ITERATOR_DEBUGGING=1. My build was debug, and it would have normally set the iterator level to 2, but this included code I got was overriding it to a 0! (and I didn't notice it until digging into it) It was linking against the debug boost libs, which had default built with the iterator level set to 2. Thus, a mismatch occurs. This mismatch can also happen if you try to build with VS 2015+ with boost libs that were produced pre 2015. But that is unlikely.

Comments

-1

Replace 0 with 2 in lib files involved in compilation.

2 Comments

What, as a global macro definition or something?
I did in hex editor. But as others guys says above problem that some lib was compiled with some obsolete settings were that param (you never use ) set to 0 now latest MSVC compiling with 2. To be honest what should be done is recompiling ALL sources with latest compiler settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.