2

I'm trying to make my C++ into a .lib file to use with my projects.

I have the files Log.h and Log.cpp.

I went into my project properties in Visual Studio and changed the configuration type from .exe to .lib. I set the build mode to Release and built my class into a file called Log.lib.

In a new C++ project, I'm trying to include that .lib file I made along with the Log.h file. All was successful, it recognised my functions, but when I try to run my exe program with my included Log.h, I get the following errors:

mismatch detected for '_ITERATOR_DEBUG_LEVEL':
    value '2' doesn't match value '0' in main.obj

By referencing this stackoverflow post, I discovered that building and running my new project in Release mode (the same as the .lib mode) removes the errors and I can successfully run my program and use the Log.h.

How can I compile my Log.h lib to be compatible with both Debug and Release?

Ari Seyhun
  • 11,506
  • 16
  • 62
  • 109
  • 1
    You need to build both the Debug and Release versions of the library. And link the appropriate one in your main project. It is automagic if you create a solution with both projects and add the library project to the references of the main project. – Hans Passant Sep 18 '17 at 19:03

2 Answers2

5

You have a mismatch in the version of the C runtime library that your projects are linking to. One of the projects is linking to the debug version of the CRT, while the other one is linking to the release version of the CRT. That mixed configuration is not supported, and it is resulting in the error message. The standard library template classes are actually different in debug and release builds.

You need to check the settings for all of your projects (everything that generates either an EXE or a LIB file as output), and ensure that they are all using the same version of the CRT. This is the /MT or /MD switches passed to the compiler.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • I'm using SDL2 and they supplied a `SDL2.lib` and `SDL2main.lib` which I needed to link to my project. But these lib files have no problem when I switch between `Release` and `Debug` mode. – Ari Seyhun Sep 18 '17 at 18:21
  • I don't understand your comment. If neither of those libraries use the C++ standard library, then you will not have this issue. If they do, then you cannot mix debug and release versions. – Cody Gray - on strike Sep 18 '17 at 18:23
0

It is not possible to build your lib to be compatible to Debug and Release C runtime library (CRT). See also here.

But it is possible to change the version of the CRT in your exe project: If both the debug and release configuration use the same version of the CRT (e.g. Multi-threaded DLL (/MD)) you can build your lib in release configuration and use it in release and debug configuration of your exe program (which will result in poorer debug support).

To change the runtime library in Visual Studio, open the Project Properties and go to "C/C++" - "Code Generation" - "Runtime library" (this depends on the version of Visual Studio you use, but should at least be valid for VS2010-2015).

Thomas Schmid
  • 116
  • 10