Reputation: 99498
Are the following to equivalent:
#define xxx
in source and
gcc -D xxx
Does the first one only apply to the source file where it appears and not in other source files where it doesn't appear, so that xxx is not defined in those other files?
Does the second one imply that xxx is defined in all source code files?
thanks!
Upvotes: 1
Views: 618
Reputation: 17278
Yes, you're right on all points. If you do a #define in a header file, it will apply to all files which include that header file, directly or indirectly. That's used to define constants or macros.
Upvotes: 1
Reputation: 47965
I think the compiler option is more helpful of you want to disable or enable features while compiling so you don't need to edit any files.
Upvotes: 1
Reputation: 27230
Does the first one only apply to the source file where it appears and not in other source files where it doesn't appear, so that xxx is not defined in those other files?
Right.
Does the second one imply that xxx is defined in all source code files?
Yes
Both methods do same things but scope of that defination is different in both cases.
-D option is widely used in makefile because here you do not need to modify source files. But if that defination is just specific to any file then define it in appropriate file is good option.
Upvotes: 1
Reputation: 3049
The first one does only apply to the source files where it is defined, but can be placed in a header file that can be included in several files making the definition applicable to more files. The second define is active in all files compiled with -D xxx.
Upvotes: 1