huy
huy

Reputation: 4904

How to read predefined (#define) across projects?

I have 2 projects in the same solution: A is static library and B is exe. B depends on A

In B I do a declaration for a DEBUG mode. Something like #define DEBUG

Then I went to check if DEBUG is defined in A, if it does then do some debug printing:

// Code in A
#ifdef DEBUG
cout<<"debug message";
#endif

But this doesn't seem to work. I guess when A is built it doesn't have knowledge about B. How do we go about doing this? Basically because I have different executable project relying on A, and some of them need to print debug messages, and some don't. And yet I don't want to rebuilt A everytime I switch from B to another executable project.

Upvotes: 0

Views: 108

Answers (3)

xappymah
xappymah

Reputation: 1634

Well, you need some common header for B and A where you can define DEBUG option. Or create some static function in B, like isDebug, define it depending on DEBUG and use it in A.

Upvotes: 1

Brian Roach
Brian Roach

Reputation: 76908

#define is a preprocessor macro

It is expanded before compilation. Nothing you do in B is going to have an effect on the already compiled A.

Upvotes: 1

Bo Persson
Bo Persson

Reputation: 92271

The usual solution is to build two A's, one for Debug and one for non-debug. Then the other projects can choose which one to link against.

Upvotes: 1

Related Questions