Johan Kotlinski
Johan Kotlinski

Reputation: 25731

Making sure same configuration is used for library and executable

let's say I am distributing a library as binary. It is shipped in two versions, debug and release. Debug and release are incompatible with each other, so if e.g. the user builds a release executable, he/she must link with release library.

If there is a mismatch between library and executable versions, currently there will be subtle errors that are really hard to figure out. I would instead like to show a really clear error message that informs there is a mismatch, preferably at link time.

What would be a good way to achieve this?

Upvotes: 4

Views: 280

Answers (3)

Jim Ward
Jim Ward

Reputation: 1

Could also checksum the binaries/libraries in your install script. Not a C answer, though.

Upvotes: 0

Elemental
Elemental

Reputation: 7466

I'm going to assume that you are using a static library and by binary you mean a .lib that will be linked at compile time (as opposed to a dll or the like that might be mismatched at run-time).

Seems to me the easiest way is to have this sort of a construct in your .h file

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

Similarly in the cpp file of the library:

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

Now in the main exe which is using the library:

InitialiseLibrary();

If the release-ness of the library and exe don;t match then the linker will report not matching one or other InitialiseLibrary... function.

A different option is to ensure that the release and debug libraries compile to differently named files and then get the link to work using #pragma in the .h file (as opposed to explicitly including the library in a project). Using the #ifdef as above you can select at compile time which library to link by choosing which #pragma is used. This second technique doesn't exactly address your question (as it doesn't STOP the link happening if the programmer tries to force it) but it is a normal way of dealing with difficulties of this sort (and has advantages in complex build environments)

Upvotes: 6

Kos
Kos

Reputation: 364

Use #error directive, that will abort the compilation:

#ifndef RELEASE_VERSION
#error Release version required
#endif 

Upvotes: -1

Related Questions