Make sure the same configuration is used for the library and executable

let's say I distribute the library as binary. It comes in two versions, is debugged and released. Debugging and release are incompatible with each other, so if, for example, the user creates the release executable, he / she must link to the release library.

If there is a mismatch between the library and the executable versions, subtle errors that are really hard to understand will now be detected. Instead, I would like to show a really clear error message saying that there is a mismatch, preferably during the connection.

What would be a good way to achieve this?

+4
source share
3 answers

I assume that you are using a static library, and binary you mean .lib, which will be linked at compile time (as opposed to DLLs or the like, which may not be compatible at runtime).

It seems to me that the easiest way is to have this construct in your .h file

#ifdef _RELEASE //or whatever your compiler uses #define InitialiseLibrary InitialiseLibraryRelease #else #define InitialiseLibrary InitialiseLibraryDebug #endif 

Similarly in the cpp library file:

 #ifdef _RELEASE //or whatever your compiler uses void InitialiseLibraryRelease() { CommonInitialise(); } #else void InitialiseLibraryDebug() { CommonInitialise(); } #endif 

Now in the main exe that uses the library:

 InitialiseLibrary(); 

If the liberation of the library and exe don; t match, then the linker will report that it does not match a particular InitialiseLibrary function ...

Another option is to make sure that the release and debug libraries are compiled with named files, and then get a link to work using #pragma in the .h file (as opposed to explicitly including the library in the project). Using #ifdef, as described above, you can choose at compile time which library to link by choosing which #pragma is used. This second method does not exactly solve your question (since it does not stop the connection if the programmer tries to force it), but it is a common way to deal with this kind of difficulties (and has advantages in complex build environments)

+6
source

You can also check the binaries / libraries in your installation script. However, not a C-answer.

0
source

Use the #error directive, which will abort compilation:

 #ifndef RELEASE_VERSION #error Release version required #endif 
-1
source

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


All Articles