473,320 Members | 2,110 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,320 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 1605

"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 (40°39.22'N, 111°50.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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.