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 topic! I just keep running into
problems (Windows platform) because files are included in the wrong order,
one include file messes up the other one etc.. I think I need to brush up on
some basics here.
In this example project, I have several cpp files (~10) that all pretty much
include one common header file. In Visual Studio I turned the option on to
actually generate those pre-processed files - so that I could see what's
actually going on behind the scenes.
Well, I expected one big file but instead got one preprocessor file for each
of the cpp files in the project. OK, I suppose that this makes sense. One of
the things however I'm constantly trying to do is to prevent header files
from being included more than once - do I not need to worry about this when
it's included from different cpp files?
Also, when I #define macros and put them at the beginning of the header file
like
#ifndef HEADERFILE1_H
#define HEADERFILE1_H
....
...
#endif
then those macro definitions don't seem to stick. When the next cpp files is
being processed, the header file is being included again - even though the
macro was previously defined
Is this normal, and where am I getting confused?
Thanks!
Flo. 12 1580
On Thu, 12 Aug 2004 15:17:26 -0700, Florian wrote: I just keep running into problems (Windows platform) because files are included in the wrong order,
Headers should be minimal and complete. Each header should include every
header it needs. Nothing more. This would prevent users needing to know
about inclusion orders.
One cheap trick to ensure the completeness of headers is to include that
header in that headers implementation file *first*. Assume foo.h exists,
then in foo.cpp:
// foo.cpp
#include "foo.h" // before other includes
Some Windows headers were not complete (at least in the VC++ 6.0 days),
so you were forced to know some inclusion order.
one include file messes up the other one etc..
That sounds like a macro problem. One macro defined in one header
changes the text in another header. Macros are evil; try to avoid them.
One of the things however I'm constantly trying to do is to prevent header files from being included more than once
This is needed and important for each translation unit. Each translation
unit should include each header file that it needs only once.
- do I not need to worry about this when it's included from different cpp files?
Perfectly normal. The C++ compilation unit is called a translation
unit. A translation unit is what comes out of the
preprocessor. Translation units are not aware of other translation
units. If a cpp file needs definitions in a header, it must include that
header itself.
Also, when I #define macros and put them at the beginning of the header file like
#ifndef HEADERFILE1_H #define HEADERFILE1_H
... .. #endif
then those macro definitions don't seem to stick. When the next cpp files is being processed, the header file is being included again - even though the macro was previously defined
Is this normal, and where am I getting confused?
This is normal.
Ali
Hi
You can also use the directive, #pragma once, in the header file - this
would ensure that the header is only included once in a build.
Regards
Ashley Visagie
Hi
You can also use the directive, #pragma once, to ensure that the header
file is only included once in a build.
Regards
Ashley Visagie
Ashes wrote: Hi
You can also use the directive, #pragma once, in the header file - this would ensure that the header is only included once in a build.
Strictly speaking, whatever you can do with #pragma is implementation-defined.
I'd say don't use it unless you really have to.
Though I've got to admit, every compiler that I have access to implements
#pragma once the way you would expect it. Then again, Gcc does complain
about it being "obsolete".
Denis
Florian wrote: then those macro definitions don't seem to stick. When the next cpp files is being processed, the header file is being included again - even though the macro was previously defined
Is this normal, and where am I getting confused?
Each source code file (each *.cpp) is compiled independently of all others.
That's the way things work in C++.
And of course it makes sense. Your project consists of 10 files. The project
I am involved in, consists of more then 400 files. Consider I make a change
in one of them and the compiler is going to recompile everything (sometimes
we have to do this, it takes around 2 hours for a complete recompile).
When the compiler compiles a *.cpp file, it starts in a fresh state. Nothing
it has learned by compiling a previous source code file remains. Which is
a good thing, because it means it can compile a collection of *.cpp files
in any order it likes. If you make a change in one of them, only this file
needs to be recompiled and the linker will form a new executable from the
results of all the copilation steps (they are called 'object files' or 'machine
code files')
--
Karl Heinz Buchegger kb******@gascad .at
Danke Heinz,
That makes sense now. I just didn't know that in C++ each cpp file is it's
on unit, like Ali said. Thanks a lot for clarifying this, that will make
troubleshooting a bit easier in the future.
Flo
"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:41******** *******@gascad. at... Florian wrote: then those macro definitions don't seem to stick. When the next cpp files is being processed, the header file is being included again - even though the macro was previously defined
Is this normal, and where am I getting confused?
Each source code file (each *.cpp) is compiled independently of all others. That's the way things work in C++. And of course it makes sense. Your project consists of 10 files. The project I am involved in, consists of more then 400 files. Consider I make a change in one of them and the compiler is going to recompile everything (sometimes we have to do this, it takes around 2 hours for a complete recompile).
When the compiler compiles a *.cpp file, it starts in a fresh state. Nothing it has learned by compiling a previous source code file remains. Which is a good thing, because it means it can compile a collection of *.cpp files in any order it likes. If you make a change in one of them, only this file needs to be recompiled and the linker will form a new executable from the results of all the copilation steps (they are called 'object files' or 'machine code files')
-- Karl Heinz Buchegger kb******@gascad .at
Thanks Ali,
That makes more sense now. I didn't know that in CPP each file was it's own
translation unit. I suppose that's not the case in C then, correct?
Anyway - thanks a bunch,
Flo.
"Ali Cehreli" <ac******@yahoo .com> wrote in message
news:pa******** *************** **********@yaho o.com... On Thu, 12 Aug 2004 15:17:26 -0700, Florian wrote:
I just keep running into problems (Windows platform) because files are included in the wrong order,
Headers should be minimal and complete. Each header should include every header it needs. Nothing more. This would prevent users needing to know about inclusion orders.
One cheap trick to ensure the completeness of headers is to include that header in that headers implementation file *first*. Assume foo.h exists, then in foo.cpp:
// foo.cpp #include "foo.h" // before other includes
Some Windows headers were not complete (at least in the VC++ 6.0 days), so you were forced to know some inclusion order.
one include file messes up the other one etc..
That sounds like a macro problem. One macro defined in one header changes the text in another header. Macros are evil; try to avoid them.
One of the things however I'm constantly trying to do is to prevent header files from being included more than once
This is needed and important for each translation unit. Each translation unit should include each header file that it needs only once.
- do I not need to worry about this when it's included from different cpp files?
Perfectly normal. The C++ compilation unit is called a translation unit. A translation unit is what comes out of the preprocessor. Translation units are not aware of other translation units. If a cpp file needs definitions in a header, it must include that header itself.
Also, when I #define macros and put them at the beginning of the header file like
#ifndef HEADERFILE1_H #define HEADERFILE1_H
... .. #endif
then those macro definitions don't seem to stick. When the next cpp files is being processed, the header file is being included again - even though the macro was previously defined
Is this normal, and where am I getting confused?
This is normal.
Ali
Thanks,
It seems as if my "problem" was the fact that I didn't understand the
concept of the translation units - I should be fine in the future.
Flo.
"Ashes" <av******@hotma il.com> wrote in message
news:92******** *************** *******@localho st.talkaboutpro gramming.com... Hi
You can also use the directive, #pragma once, in the header file - this would ensure that the header is only included once in a build.
Regards Ashley Visagie This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bram Stolk |
last post by:
Hi there,
What could I use to parse CPP macros in Python?
I tried the Parnassus Vaults, and python lib docs, but could not
find a suitable module.
Thanks,
Bram
|
by: Nudge |
last post by:
I have an array, and an unrolled loop which looks like this:
do_something(A);
do_something(A);
....
do_something(A);
I thought: why should I type so much? I should write a macro.
So I was looking to write something along the lines of:
|
by: seemanta dutta |
last post by:
Greetings C gurus,
I have used preprocessor directives since a very long time. But
whenever I see some professional piece of C code, the linux kernel for
example, I get literally confused by the amount of preprocessor
directives used in these code.
Can anyone please tell me how to actually use these preprocessor
directives in a more professional way like selecting particular lines
of code suited for some particular hardware etc...
|
by: Walter Roberson |
last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been
reading carefully over the ANSI X3.159-1989 specification, but I cannot
seem to find a justification for the behaviour. Could someone point
me to the appropriate section, or else confirm the behaviour as a bug?
For a particular project, I am using the C preprocessor phase only.
I am not using the standalone program 'cpp' because proper functioning
of my project...
|
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 where the definitions were:
_UNICODE,_DEBUG,_WIN32_DCOM,WIN32,_WINDOWS,_WINDLL,_AFXDLL,_USRDLL
In VC++7.1, these are the preprocessor definitions of the project (right-click the project in Solution Explorer and choose Properties -> C++ ->...
| |
by: Prashant Mahajan |
last post by:
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...
|
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
the same file ? If not then how much variation is allowed ? Is it
just a bit more or less white space here and there or could could
there be larger differences ?
If the output is not deterministic then is it possible that the output
of the...
|
by: Bogdan |
last post by:
Can anyone recommend a program for indentation of C preprocessor
directives. My file looks like this:
#ifdef a
#define b
#else
#define c
#endif
int main()
{
|
by: josh |
last post by:
Hi, I've read that in the future the C++ will not more support the
preprocessor and now some preprocessor functionality have already been
implemented in the language itself. What are that?
Thanks.
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |