• Advertisement
Sign in to follow this  

How to verify that library and app were compiled with same switches?

This topic is 2563 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I have a library that can be compiled (C++) with a certain define (either -DBRAND=1 or -DBRAND=2), and I have an application that uses this library.
Both library and application have to be compiled with the same value of BRAND.

Is there a way to verify at compile/link time that libary was compiled with the same BRAND value as application?

Zbyl

Share this post


Link to post
Share on other sites
Advertisement
You could use a trick like boost::staticassert to cause a compile to die due to specific compile time checks. If your library has a header with your library define then you could do


// lib.h
#define LIB_BRAND (2)

// main.cpp in your app
#define BRAND (1)
BOOST_STATIC_ASSERT( BRAND == LIB_BRAND);

and have it properly break because the two brands aren't in fact the same.

Share this post


Link to post
Share on other sites
Thanks guys. Nice solutions.
Unfortunately KulSeran's solution won't work for me, since both defines have the same name. This cannot be changed.

I'll go with Adam's solution. I'd like to put it in one header common to both library and application - but for that I guess I'll need some kind of pragma to force reference for a "marker" symbol in application. Plus compiling lib with additional option -DCREATE_BRAND_MARKER to create the marek in lib.

I'll post here if I'll figure it out.

Thanks.

Share this post


Link to post
Share on other sites
Here's my solution. One should put it in a header included in both lib and app. Library should be compiled with COMPILING_LIBRARY defined.
It generates link-time errors (unresolved external symbol '__marker_symbol_BRAND_0') when linking library and application compiled with different options:


#if defined(COMPILING_LIBRARY)
#if (BRAND == 0)
extern "C" void _marker_symbol_BRAND_0();
#else
extern "C" void _marker_symbol_BRAND_1();
#endif
#else
#if (BRAND == 0)
#pragma comment(linker, "/include:__marker_symbol_BRAND_0")
#else
#pragma comment(linker, "/include:__marker_symbol_BRAND_1")
#endif
#endif


Any comments? Maybe there are some side effects of using "#pragma comment?

Zbyl

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement