By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

Preprocessor and Compiled Files

P: n/a
I just wanted comments from you all on the following topic:
Let's say we have 2, C code files namely file1.c and file2.c.
file1.c contains few pre-processor definations
say #define TEST1 10
We now compile file1.c , make it's object file.
and use this object file in the other file i.e file2.c.
Then can we use the macro TEST1 in file2.c as well.
As I understand about macros, they are replaced by their
corresponding values , but are the definations removed from the
source files , once the preprocessor scans the file.

Jan 9 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Prashant Mahajan" <th****************@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
I just wanted comments from you all on the following topic:
Let's say we have 2, C code files namely file1.c and file2.c.
file1.c contains few pre-processor definations
say #define TEST1 10
We now compile file1.c , make it's object file.
and use this object file in the other file i.e file2.c.
Then can we use the macro TEST1 in file2.c as well.
As I understand about macros, they are replaced by their
corresponding values , but are the definations removed from the
source files , once the preprocessor scans the file.


The pre-processor is more or less just a sophisticated text processing
thingmy. Any pre-processor directives are indeed removed before the actual
'compiler' looks at your code. So, any macro in test1 is not available once
test1.c has been pre-processed. If you want to 'share' macros, put them in
a common header file and include that in test1/test2

Jan 9 '06 #2

P: n/a
In article <11**********************@g44g2000cwa.googlegroups .com>
Prashant Mahajan <th****************@gmail.com> wrote:
Let's say we have 2, C code files namely file1.c and file2.c.
file1.c contains few pre-processor definations
say #define TEST1 10
We now compile file1.c , make it's object file.
So far so good....
and use this object file in the other file i.e file2.c.
This makes no sense. You cannot use the output of the compiler
as an input to file2.c -- that is:

% cat file1.c
#define TEST1 10
int f(void) { return TEST1; }
% cat file2.c
#include "file1.o"
...

is unlikely to work.
Then can we use the macro TEST1 in file2.c as well.
You can define TEST1 separately in file2.c:

#define TEST1 zorgleblatt

This is generally a bad idea, because now you have two different
things named TEST1, and two different expansions. When you go
to talk to the programmer in the next cubicle and start talking
about the TEST1 macro, he may think you mean the *other* TEST1
macro.

Even if you define it as the same thing:

#define TEST1 10

it remains error-prone, as a change to one of the definitions
will not affect the other one.

This is why Ken, Dennis, and Brian provided "header files". Put
the define in a separate, third file -- named "test1.h" for instance
-- and use "#include" directives in file1.c and file2.c to include
the third, shared file.
As I understand about macros, they are replaced by their
corresponding values, but are the definations removed from the
source files , once the preprocessor scans the file.


The *source* files are not modified by compilation (at least, not
on any sane system). The macros are, however, expanded lexically
at the point they are encountered, in Phase 4 of translation (see
the C standard, "translation phases"). This happens on a
"per-translation-unit" basis.

Compilers are not required to (and many do not) retain any information
about preprocessor macros in the object files produced by Phase 7.
Final executable programs are produced by Phase 8.

Note that phases 1 through 4 are often collectively referred to as
"preprocessing", phases 5 through 7 are often referred to as
"compiling", and phase 8 is typically referred to as "linking".
These have a historical basis: in Dennis's original C compilers,
phases 1 through 4 were implemented by a program called "cpp" (the
"C PreProcessor"), 5 through 7 by the compiler proper[%], and 8 by
the linker/loader "ld". In some compilers, Phase 7 was split into
two or three programs. But as far as the C standard is concerned,
these are all just parts of "translating" C code into something
that is actually run by an "implementation".

[% Technically, Dennis's original C compilers did not even do
some of these -- for instance, there was no adjacent string
literal concatenation.]
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jan 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.