0

I'm going to abstract my problem to avoid including unneeded details, but if needed I can provide the source code. I'm using visual studio.

I have the following files - all header files have #pragma once:

  • A.cpp //(containing my main function)
  • B.h
  • B.cpp //(Plays no role)
  • C.h
  • C.cpp

And here is how the preprocessor commands are set up:

A.cpp   #defines UseOptionOne
A.cpp   #includes B.h

B.h     #ifdef UseOptionOne   
            #defines Func as f1() //(calling a function that prints a msg)   
        #else                 
            #defines Func as [blank]  

A.cpp   #includes C.h
C.h     #includes B.h     // (B.h have #pragma once, so it doesnt get included again)

Here's how the function calls are set up:

A.cpp   main function uses Func          //- It prints as intended 
A.cpp   calls function in C.cpp // this function does the following:
        {
           #ifndef UseOptionOne
             exit(0)                    //- Doesn't happen, so UseOptionOne is defined
           #endif   
           uses Func                        //- DOES NOTHING?????
        }
A.cpp   uses Func                       //- It prints as intended 

I don't understand how this is possible? UseOptionOne is confirmed to still be defined in C.h but the Func is defined differently???

Can anyone explain this? or would you want me to provide you with my rather complicated solution or some code fragments maybe?

I'm really lost :(

EDIT: I have used breakpoints to confirm that the C.cpp function is called, the 'Func' is simply treated as blank

EDIT2: I can't answer my own question due to lack of reputation, so im putting it here:
I created a new project implementing my abstract description and it did trigger the exit in the #ifndef
So there is no way any of you could solve the problem with this description. I'm just going to have to look through everything again and find the mistake/error.

4
  • 1
    Just a comment. You should probably use include guards instead of #pragma once for code portability. Commented Aug 3, 2012 at 16:30
  • 1
    Well so am I. In your code above you say 'A.cpp #defines UseOptionOne'. In you explanation below you say 'UseOptionOne is defined in C.h'. Which is it? Maybe it's both. The trouble with paraphrasing your code like this is that it's utterly inevitable you will get some crucial detail wrong, and so no-one will be able to answer your question. The best thing to do is not to give us excerpts from your code, and it's not to post your whole program. The best thing to do is to make a mini-version of your code that still has the problem you describe and then post all of that. Commented Aug 3, 2012 at 16:30
  • I agree with RageD it's always better for portability. In my experience the #pragma once statement has never created so much advantage to prefer it to portabiliy. Commented Aug 3, 2012 at 16:38
  • What i meant in the explanation below was the UseOptionOne is confirmed to be defined (still defined) in C.h! my bad Commented Aug 3, 2012 at 16:42

1 Answer 1

2

My two cents:

UseOptionsOne is defined only for A.cpp and for classes that includes A.h: in your case, it's defined only in A.cpp

C.cpp has no reference to A.h, so it does not see the define. In this case, UseOptionOne is not declared in C.cpp, it uses the blank function.

when you go out of the scope of C.cpp, and return in the scope of A.cpp, the function works because in that scope UseOptionOne is defined.

If you want to use the define in all files, you can create a separate definitions.h header and put in it the definitions, and then include that file in all headers for which you want the definition to work.

Sign up to request clarification or add additional context in comments.

6 Comments

Just my thought but you beat me to it, sounds like B.h is included before UseOptionOne is defined in C.cpp
Order does not change things. C includes B -> Func in C is clear - A includes B -> the definition changes Func The result is that Func is different in A and in C, no matter compilation order. Simply C does not know anything about existence of the definition. If it must be seen by all files, the define must be declared or in project configuration, or through a global header that must be included in every header for which you want that definition.
If that was the case, the exit(0) would be triggered in C.cpp, UseOptionOne must be defined in the function, but Func must be blank for it to have the effect mentioned. Technicality either way, what you're saying about defining it in a better way still holds true.
This is incorrect! There is no A.h first of all, nobody includes A.cpp in any way. And C.cpp does see the define of UseOptionOne as i used #ifndef and not #ifdef!
Btw. The problem with putting #define UseOptionOne in a global header, is that it's an option that the user of my library is supposed to set before he includes anything
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.