Prevent mixing debug and release libraries

As a library developer, I want my library users (Windows, MSVC) not to communicate with the wrong configuration (do not associate the debug library with their release programs and vice versa).

Is it possible to warn the user during compilation, which he must associate with the correct library configuration?

EDIT

Both versions of debugging and release should be available to allow Windows developers to debug their applications. Therefore, both versions must be available for debugging and releasing my library.

I ask this question because most of the support for novice Windows developers is due to the fact that they mix debug and release code and also get hard to debug runtime errors.

+6
source share
4 answers

Good question, I always assumed that developers using my libraries would reference the correct version. Now that I think about it, why do you want to publish your debug library? Why shouldn't both debug and release versions link to your release library?

Regardless, I see a way to do this by exporting some characters for each configuration:

//header: class DLLIMPEXP Dummy { static int x; virtual void dummy(); } //cpp #ifdef DEBUG int Dummy::x = 0; void Dummy::dummy() { } #endif 

As you can see, your character will only be exported if your module is compiled into DEBUG. Attempting to communicate with lib in release mode from the third module will result in a linker error. You may have something similar for the reverse.

I do not suggest that you do this, I would rather document it or distribute only the release version of my module.

+4
source

There are two different aspects to this:

  • incompatibility problem
  • performance issue

If this is a performance issue, then the choice will still be theirs, they may want to debug.

If this is a matter of incompatibility, one simple thing is to change the namespace for the debug version so that the characters will be distorted in different ways.

 #ifdef NDEBUG namespace project { #else namespace project { namespace debug { #endif // content #ifdef NDEBUG } #else } using namespace debug; } #endif 

Nested in the debug namespace, you change the character binding (although, compiled, it doesn't change anything). This actually prevents linking the library compiled against the debug version with the release version (and thus solves incompatibility at an early stage, rather than crashing mysteriously).

However, I would strongly recommend that you reserve this for a very specific set of classes (this is hard).

As a general rule, it should be possible to provide compatible interfaces in both debug and release modes so that clients can be hot-swappable at boot time.

+1
source

You can add the #warning directive, but I am totally against you. You better deliver two different names to a different version of your library.

Here is another hint of your problem:

 myLib.h // Release Version myLibd.h // Debug Version 

Doing this this way will make the user take care of how to configure the application in your library (since the setting must be manual).

You can also add a note to README or INSTALL, most users read it when they want to set up a link to MSVC.

You can also check the macro value of DEBUG and NDEBUG in your program. (When approved during initialization of the library.

0
source

Add this code to your lib header

Different names for different types

 #ifndef _DLL // C runtime as dll # ifdef _DEBUG # pragma comment(lib, "MyLibD.lib") # else # pragma comment(lib, "MyLib.lib") # endif #else // C runtime statically # ifdef _DEBUG # pragma comment(lib, "MyLibSD.lib") # else # pragma comment(lib, "MyLibS.lib") # endif #endif 

Different paths for different types

 #ifndef _DLL // C runtime as dll # ifdef _DEBUG # pragma comment(lib, "debug/dynamic/MyLib.lib") # else # pragma comment(lib, "release/dynamic/MyLib.lib") # endif #else // C runtime statically # ifdef _DEBUG # pragma comment(lib, "debug/static/MyLib.lib") # else # pragma comment(lib, "debug/static/MyLib.lib") # endif #endif 

after that you will need to add the lib path to your linker and you will no longer be able to mix it.

0
source

Source: https://habr.com/ru/post/899112/


All Articles