0

i have the following files

(its pseudo code, and i know the define, undef is ugly, but i would need it for some project)

if i compile those files and link them together - it seems to work - that in file3, file1 - MYVAL == 1

is it safe to assume this, that the preprocessor stuff is done file-by-file?

conf.h:

#define MYVAL 1 

src1.c

#include "conf.h" int maint(int argc, char ** argv) { printf("%d", MYVAL); } 

src2.c

#include "conf.h" void demo() { #undef MYVAL #define MYVAL 2 printf("%d", MYVAL); } 

src3.c

 #include "conf.h" void demo2() { printf("%d", MYVAL); } 

regards

3
  • 1
    The compiler doesn't actually see any "source" files, all it sees are translation units. A translation unit is, roughly speaking, a source file and all included header files. The preprocessor reads a source file and all included header files, and generates a single file which is then fed to the compiler. Neither the preprocessor nor the compiler knows anything about other files. Commented Feb 3, 2015 at 13:34
  • 1
    As for the #include preprocessor directive, the preprocessor simply copy-paste the contents of the included file (after preprocessing) into the place where the #include directive was. Commented Feb 3, 2015 at 13:35
  • possible duplicate of Working of the C Preprocessor Commented Feb 3, 2015 at 14:53

3 Answers 3

2

Preprocessing is done per translation unit before the compilation phase (so way earlier than the linkage phase). In your case the preprocessor will expand that macro in each of your .c files individually based on your inclusion of the conf.h header file.

is it safe to assume this, that the preprocessor stuff is done file-by-file?

Basically in your case yes. Each of your .c files is a distinct translation unit. (Unless they start including each other or something) They are preprocessed separately, compiled and then their objects get linked together.

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

1 Comment

It's an implementation detail, but notionally the preprocessor performs the '#includes #define macro substitutions and other preprocessor directives then starts compiling. However many implementations do something a little more incremental. You won't go far wrong if you imagine a big long temporary file is rolled out and then compiled.
1

When you #include "conf.h" its code is placed instead of this line. This is the preprocessor's work. So, this define sentence is placed in each file.

But if you do #define work(n) funcCall((n)) and work(5); then, it'll fail if funcCall is not defined in any of your files.

Comments

1

Yes, the preprocessor is used for each source file. It generates a preprocessed file from the source file and all include files that is actually passed to the C compiler.

When you #define something in a include file it gets in the preprocessed file. #defines in other source file doesn't care.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.