NOOB question about #define and added .cpp files.

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,

 

I have a simple c++ project that I have added some .cpp and .h files to. 

They are being compiled correctly but I want to use some #defines in main.c to set options etc.

If I put the #defines in the files where they are used everything is good, but if I put them in main.c then I get errors stating that they are not defined. 

I suspect that this is because the other files are compiled first? Is there a way to force main.c to compile first?

 

 

I am using AtmelStudio 6

 

Thanks.

 

(EDIT) - Just to clarify, main.c is an ordinary c file and the "library" files are c++

Last Edited: Mon. Jan 19, 2015 - 01:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Remember that #define (in fact # anything including #include) are nothing to do with the C or C++ compiler. the C/C++ compiler never sees any line or text you type that is in a #define. So if you type:

#define FOO 37

int main(void) {
    PORTB = FOO;
}

it is only the C preprocessor that "sees" the #define line or the name "FOO". The C preprocessor is basically just a string substitution system so it read the text above and remembers that the user has asked for "FOO" to be replaced by "37" and in anything that follows it does exactly that. So the C compiler sees:



int main(void) {
    PORTB = 37;
}

The whole idea of "FOO" has gone at this stage (which you can see by getting the C compiler to generate .i files using the -save-temps option).

 

So don't assume that the preprocessor does anything more than this. Of course another command it does support is "#include" and that says "just go and get all the text of that file over there and insert it here. So if you had:

// header.h
#define FOO 37
//main.c
#include "header.h"

int main(void) {
    PORTB = FOO;
}

then first the #include would do exactly what it says. So the file would first become:

//main.c
// header.h
#define FOO 37

int main(void) {
    PORTB = FOO;
}

then, as before the pre-pro will use the #define and replace anything it asks to. So that then becomes:

//main.c
// header.h


int main(void) {
    PORTB = 37;
}

which is exactly what the C (or C++) compiler sees.

 

So don't assume that if you have file1.c (or even a file1.h/file.c pair) that contains a "#define FOO 37" that a file2.c will know anything about that. file2.c is not compiled at the same time as file1.c. It might actually have been compiled last Wednesday for all you know. So nothing about it is "shared". The only way to "share" stuff between source files in C is to put the thing to be shared into a .h file then have both file1.c and file2.c #include that shared.h file.

 

So you might want a config.h that is #include'd (and hence shared) between two or more .c files then anything that is #define'd within it will be substituted into both file1.c and file2.c when each is compiled in turn.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You shouldn't use #define in a C++ program except in rare cases. Use const instead and make it part of a class.

 

Beyond that Claswson nailed it

 

Keith Vasilakes

Firmware engineer

Minnesota

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your thorough explanation.

 

How big of a problem is using #define in c++? the .h files that I am adapting are full of them.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is not really a problem, it is just that the facilities of C++ have solutions for most things that you would use #define in C. Using those solutions makes your code safer, easier to read, and removes a significant source of hard to find bugs.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just wait until you try a bit if Java ;-)

 

There is no preprocessor whatsoever there so no #defines.

 

BTW I was going to suggest a google for something like "replace #define with const in c++" but I just tried that and read some of the answers and there's loads of mis-information and a case of the "blind leading the blind". Principally a lot of people who should know better appear to know very little about compiler optimization and the fact that "static const int n = 12345;" will usually be compiled away and will NOT create/consume any memory as it only "exists" at compile time.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:

It is not really a problem, it is just that the facilities of C++ have solutions for most things that you would use #define in C. Using those solutions makes your code safer, easier to read, and removes a significant source of hard to find bugs.

 

I agree, no 'problem' just better not to use #defines

Keith Vasilakes

Firmware engineer

Minnesota