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

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

## 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 on other sites
You could use the ## token pasting operator to rename some or more of your functions so that the linker won't find them when the define is set incorrectly. Or just use a standard #ifdef to rename one or two of them.

http://msdn.microsof...v=vs.80%29.aspx

##### Share on other sites
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 on other sites
-- aparently we can't delete our posts anymore --- removed doublepost

##### 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 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

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
17
5. 5

• 33
• 13
• 12
• 10
• 12
• ### Forum Statistics

• Total Topics
632573
• Total Posts
3007140

×