Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Zbychs   Members   -  Reputation: 193

Like
0Likes
Like

Posted 16 January 2011 - 01:23 AM

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

Sponsor:

#2 Adam_42   Crossbones+   -  Reputation: 2450

Like
1Likes
Like

Posted 16 January 2011 - 07:54 AM

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

#3 KulSeran   Members   -  Reputation: 2479

Like
1Likes
Like

Posted 16 January 2011 - 10:51 AM

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.

#4 KulSeran   Members   -  Reputation: 2479

Like
0Likes
Like

Posted 16 January 2011 - 10:53 AM

-- aparently we can't delete our posts anymore --- removed doublepost

#5 Zbychs   Members   -  Reputation: 193

Like
0Likes
Like

Posted 16 January 2011 - 01:06 PM

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.

#6 Zbychs   Members   -  Reputation: 193

Like
0Likes
Like

Posted 16 January 2011 - 05:13 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS