Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help: Weird Preprocessor behavior

Status
Not open for further replies.

cdgios

Technical User
Jul 17, 2002
40
0
0
US
Environment: I am writing C code in the Visual C++ 6.0 IDE

I have a Visual C++ console application with a bunch of files that has the main file (DriverMain.cpp) as follows :

//#include "DCR_Register.h"
#include "DCR_Utilities.h"
#include "DCR_TranslationalLoopSetting.h"
#include "DCR_FreqSynthesizer.h"
#include "DCR_AGCTable.h"
#include "DCR_TuneTx.h"
#include "DCR_TuneRx.h"
int main()
{
DCR_DisplayInitValRegs () ;
//DCR_Initialize() ;
DCR_DisplayAllRegisters () ;
return 0 ;
}

The DCR_Register.h file is as shown below

#ifndef DCR_REGISTER_H
#define DCR_REGISTER_H


#if defined(DCR_CX74017) && defined(DCR_CX74063)
#error "Should not define both DCR_CX74017 and DCR_CX74063!"
#endif

#if !defined(DCR_CX74017) && !defined(DCR_CX74063)
#error "Either DCR_CX74017 or DCR_CX74063 should be defined!"
#endif

.....

#endif /*DCR_REGISTER_H*/

As you can see either DCR_CX74017 or DCR_CX74063 needs to be defined for the application. And the DCR_Register.h files is included in all other .cpp files in the project.

In the DriverMain.cpp file (as shown above) if
1) DCR_register.h file isn't included and none of its functions called:
The software compiles fine for either compiler switches.

2)DCR_register.h file is included and one or more of its functions called:
a. For DCR_CX74017 defined, THe compile switch fails while compiling DriverMain.cpp with the following error message #error : "Either DCR_CX74017 or DCR_CX74063 should be defined!"
It seems that somehow DCR_CX74017 is not being recognized???

b. For DCR_CX74063 defined, the code compiles and executes fine

c. If both DCR_CX74063 and DCR_CX74017 are defined (for test purposes), The DriverMain.cpp files compiles fine but the rest of the .cpp files (which include DCR_Register.h) fail with the following error:
#error : "Should not define both DCR_CX74017 and DCR_CX74063!"

d. If neither DCR_CX74063 nor DCR_CX74017 are defined, all the .cpp files including DriverMain.cpp fail to compile with the following error:
#error : "Either DCR_CX74017 or DCR_CX74063 should be defined!"


It seems somehow that DCR_CX74017 is not being recognized in DriverMain.cpp.

What could possibly be going on?.

Your help is greatly appreciated.

Thanks
Best Regards
Chandra
 
Where/how are DCR_CX74017 and DCR_CX74063 defined?

If it's on the command line, then make sure it's consistent across all the files.
 
Hi,
If you are using Visual C++ 6.0 IDE to write C code the first thing you should remember is to name your files .c not .cpp.

This tells Visual C++ to compile the program as a C file and not a C++ file and it will complain if you try to do anything that is C++'ish.


----
 
Hello, Thanks for both the replies.
As for the first reply, There is just one file (DCR_Register.h) that is included in all the other .cpp files that has this compile check. So, It has to be consistent across all the files.

As for the second reply, The pre-processor construct that i had used for compile time check, is as per the ISO C standard and not a specific C++ feature and so it sghouldn't matter that i am using the Visual C++ compiler to compile C code. Because you can freely write C code in C++.

Hope this answer helps.

Best Regards
Chandra
 
> So, It has to be consistent across all the files.
Which isn't the question I asked - you've just shown where they are used - not defined.

I asked about this
In some file...
#define DCR_CX74063 1
On the command line...
/DDCR_CX74063


Add to the DCR_Register.h header file, before you check them.

#if defined(DCR_CX74017)
#message DCR_CX74017 defined
#endif
#if defined(DCR_CX74063)
#message DCR_CX74063 defined
#endif

If #message doesn't work, try #warning
 
Hello Salem, I have defined the macro on the command line as /DDCR_CX74063 or /DDCR_CX74017 depending on which macro i want defined. And i have added the check for these macros in the DCR_Register.h as shown in my first post.

As a separate question, to gaurd against multiple includes we use the following construct,
ifndef SOMETHING
#define DCRSOMETHING
....
....
#endif /*SOMETHING*/

My understanding is that this construct should be included for each .h file that has the possibility of being included in more than 1 .h and/or .c files. Am i right? or are there any more criteria

Thanks
Best Regards
Chandra
 
As a separate question, to gaurd against multiple includes we use the following construct,
ifndef SOMETHING
#define DCRSOMETHING

Except you must use the same name in both of them
#ifndef DCRSOMETHING
#define DCRSOMETHING
Other than that, yes - that's how you construct include guards
 
Salem, Thanks for the answer to the 2nd question. But going back to my first question, given that i am following all the rules that we had discussed for the compile check, do you know whay this weird behavior could be happenning?.

Thanks
Best Regards
Chandra
 
Do you get the expected

#message DCR_CX74017 defined
#message DCR_CX74063 defined

Lines printed?
 
Salem, Guess what, I exited visual C++ and re-started and opened the workspace and compiled the code and it works fine!!!!!!!!. But to answer your question, I do get the lines printed. As for the Visual C++ service pack, how do you verify wether it is installed on your computer. I went thtough the Add/remove Programs list and couldn't find it there.

Thanks
Best Regards
Chandra
 
Hello Salem, I think it is more a fault with the Visual C++ IDE than anything else. I think this because, when i export the makefile and examine it, the makefile is not reflecting the pre-processor defines that i included through the Visual C++ IDE. Eventhough, the visual C++ IDE through "Project Options:" window in Project Settings shows the selections correctly, the exported makefile shows something else. I guess it is best to write your own makefile import it into visual C++ to be sure.

Best Regards
Chandra
 
> how do you verify wether it is installed on your computer
No idea - I just checked here, expecting to see something useful in Help About, and nothing.

> I exited visual C++ and re-started and opened the workspace...
:)
Sometimes, nothing beats a good old fashioned restart / clean / rebuild all.
 
Hello Salem, the "build: clean" option didn't work. I problem is intermittent. The IDE does sometimes generate the correct makefile and in some cases it doesn't. Just have to be aware of that. The other wierd thing being that, even with the wrong makefile you would expect it to generate the "error" for all the .cpp files that include the DCR_Register.h file. But that is not the case. It compiles all the files except one?????. Is that crazy or what?. Anyway thanks for your help.

best regards
chandra
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top