Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 10th, 2006, 11:25 PM
p_adib@encs.concordia.ca
Guest
 
Posts: n/a
Default repeating intructions in .h and .cpp files

Well, I tried this test:

////// include_iostream.h START //////////
#include<iostream>
////// include_iostream.h END ////////////


////// including_twice.cpp START //////////

#include<iostream>

#include "include_iostream.h"

using namespace std;

int main()
{
cout << "Mainly worked" << endl;
return 0;
}

////// including_twice.cpp END //////////

And it worked with no errors or warnings. When is it the case that
lines that are in .h file cause conflicts with code lines in the .cpp
file that includes it?

  #2  
Old November 11th, 2006, 12:05 AM
benben
Guest
 
Posts: n/a
Default Re: repeating intructions in .h and .cpp files

p_adib@encs.concordia.ca wrote:
Quote:
Well, I tried this test:
>
////// include_iostream.h START //////////
#include<iostream>
////// include_iostream.h END ////////////
>
>
////// including_twice.cpp START //////////
>
#include<iostream>
>
#include "include_iostream.h"
>
using namespace std;
>
int main()
{
cout << "Mainly worked" << endl;
return 0;
}
>
////// including_twice.cpp END //////////
>
And it worked with no errors or warnings. When is it the case that
lines that are in .h file cause conflicts with code lines in the .cpp
file that includes it?
>
It's because the standard header <iostreamhas its own include guard.
Your second inclusion of the standard header simply has no effect.

To illustrate a conflict, try:

// header1.hpp////////////////
int a = 0;

// header2.hpp////////////////
#include "header1.hpp"

// main.cpp///////////////////
#include "header1.hpp"
#include "header2.hpp"

int main(){}

Regards,
Ben
  #3  
Old November 11th, 2006, 12:55 AM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: repeating intructions in .h and .cpp files

p_adib@encs.concordia.ca wrote:
Quote:
Well, I tried this test:
>
////// include_iostream.h START //////////
#include<iostream>
////// include_iostream.h END ////////////
>
>
////// including_twice.cpp START //////////
>
#include<iostream>
>
#include "include_iostream.h"
>
using namespace std;
>
int main()
{
cout << "Mainly worked" << endl;
return 0;
}
>
////// including_twice.cpp END //////////
>
And it worked with no errors or warnings. When is it the case that
lines that are in .h file cause conflicts with code lines in the .cpp
file that includes it?
If all is done right, never. Read about "double inclusion guards".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  #4  
Old November 11th, 2006, 01:45 AM
p_adib@encs.concordia.ca
Guest
 
Posts: n/a
Default Re: repeating intructions in .h and .cpp files

To illustrate a conflict, try:
Quote:
>
// header1.hpp////////////////
int a = 0;
>
// header2.hpp////////////////
#include "header1.hpp"
>
// main.cpp///////////////////
#include "header1.hpp"
#include "header2.hpp"
>
int main(){}
Thanks Ben.
So I did it, and I think the error code follows what you're saying:

g:\my documents\f-2006\446\a3\test\include & define
test\ggtestdoubleinclusion\definetest\header1.h(2) : error C2374: 'a' :
redefinition; multiple initialization
g:\my documents\f-2006\446\a3\test\include & define

test\ggtestdoubleinclusion\definetest\header1.h(2) : see declaration of
'a'
g:\my documents\f-2006\446\a3\test\include & define

test\ggtestdoubleinclusion\definetest\main.cpp(5) : warning C4508:
'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.

main.obj - 1 error(s), 1 warning(s)

  #5  
Old November 11th, 2006, 11:55 AM
Pete Becker
Guest
 
Posts: n/a
Default Re: repeating intructions in .h and .cpp files

p_adib@encs.concordia.ca wrote:
Quote:
>
And it worked with no errors or warnings. When is it the case that
lines that are in .h file cause conflicts with code lines in the .cpp
file that includes it?
>
Most compilers have an option to only run the preprocessor. When you're
trying to figure out how the preprocessor works, you can preprocess the
code and look at the result. That can be pretty longwinded, so it's most
useful for simple experiments. Write a one- or two-line header, #include
it in a simple source file, and run it through the preprocessor. Then
#include it a second time and try it again.

--

-- Pete
Roundhouse Consulting, Ltd. -- www.versatilecoding.com
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
  #6  
Old November 22nd, 2006, 02:05 AM
p_adib@encs.concordia.ca
Guest
 
Posts: n/a
Default Re: repeating intructions in .h and .cpp files

Pete Becker wrote:
Quote:
Write a one- or two-line header, #include
it in a simple source file, and run it through the preprocessor. Then
#include it a second time and try it again.
Thanks Pete.
I would like to try it out, but I am confused as to how to run a
preprocessor. Do c++ dev. environments generally allow this option?

  #7  
Old November 22nd, 2006, 01:15 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: repeating intructions in .h and .cpp files

p_adib@encs.concordia.ca wrote:
Quote:
Pete Becker wrote:
Quote:
>Write a one- or two-line header, #include
>it in a simple source file, and run it through the preprocessor. Then
>#include it a second time and try it again.
>
Thanks Pete.
I would like to try it out, but I am confused as to how to run a
preprocessor. Do c++ dev. environments generally allow this option?
Yes. You need to look in your compiler documentation for "generate
preprocessor output" option. Example, g++ and VC++ have -E to output
the result of preprocessing to standard out.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  #8  
Old November 23rd, 2006, 07:05 PM
BobR
Guest
 
Posts: n/a
Default Re: repeating intructions in .h and .cpp files


Victor Bazarov wrote in message ...
Quote:
>p_adib@encs.concordia.ca wrote:
Quote:
>Pete Becker wrote:
Quote:
>>Write a one- or two-line header, #include
>>it in a simple source file, and run it through the preprocessor. Then
>>#include it a second time and try it again.
>>
>Thanks Pete.
>I would like to try it out, but I am confused as to how to run a
>preprocessor. Do c++ dev. environments generally allow this option?
>
>Yes. You need to look in your compiler documentation for "generate
>preprocessor output" option. Example, g++ and VC++ have -E to output
>the result of preprocessing to standard out.
>V
OP:

For g++, there is also the:

-save-temps
Store the usual "temporary" intermediate files permanently; place them
in the current directory and name them based on the source file. Thus,
compiling foo.c with -c -save-temps would produce files foo.i and foo.s,
as well as foo.o. This creates a preprocessed foo.i output file even
though the compiler now normally uses an integrated preprocessor.


watch out, files are big (mostly full of 'air'). Scroll to the bottom of a
myprog.ii (or *.i if C) and work your way back up.

--
Bob R
POVrookie


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles