473,225 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

Preprocessor and Compiled Files

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
2 1595

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Florian | last post by:
Hi, I'm hoping that someone can point me into the right direction here. I'm trying to gain a bit of a better understanding on how the preprocessor works - something that seems like a simple...
1
by: David Lindauer | last post by:
suppose I have this: template <class T> class myclass { T a ; #ifdef PREPROC_SYMBOL T b; #endif } ;
7
by: Erik Leunissen | last post by:
L.S. How can I make certain code parts be compiled conditionally, depending on the definition of a macro such as: #define VERSION "2.3" Is it all right to do things like:
2
by: Paolo | last post by:
I imported a VC++6.0 project into VC++7.1. The conversion operation makes a mess with Preprocessor Definitions, adding a "$(NoInherit)" for each file. For example: I had a DLL project in VC++6.0...
6
by: Urs Thuermann | last post by:
Does a tool exist to apply C preprocessor expansion to a C source only partially, i.e. replace only some directives? I want to replace some #if's by their expansions, e.g. all #ifdef SOME_SYMBOL,...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
8
by: mohdalibaig | last post by:
C preprocessor generates the expanded version of our source code. In order to view this expanded code, I made use of the command cpp filename.c on the dos prompt. This command generates a file of...
14
by: dan | last post by:
I would like to have the preprocessor automatically generate the number of array elements requested. Each element is zero. The elements get pasted into a larger array. The other elements may be...
14
by: lagman | last post by:
All, I'm looking for a tool that is able to take my code base (which is full of preprocessor directives) and spit out source code that is "clean" of any preprocessor directives based on the...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.