repeating intructions in .h and .cpp files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • p_adib@encs.concordia.ca

    #1

    repeating intructions in .h and .cpp files

    Well, I tried this test:

    ////// include_iostrea m.h START //////////
    #include<iostre am>
    ////// include_iostrea m.h END ////////////


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

    #include<iostre am>

    #include "include_iostre am.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?

  • benben

    #2
    Re: repeating intructions in .h and .cpp files

    p_adib@encs.con cordia.ca wrote:
    Well, I tried this test:
    >
    ////// include_iostrea m.h START //////////
    #include<iostre am>
    ////// include_iostrea m.h END ////////////
    >
    >
    ////// including_twice .cpp START //////////
    >
    #include<iostre am>
    >
    #include "include_iostre am.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.hp p"

    // main.cpp///////////////////
    #include "header1.hp p"
    #include "header2.hp p"

    int main(){}

    Regards,
    Ben

    Comment

    • Victor Bazarov

      #3
      Re: repeating intructions in .h and .cpp files

      p_adib@encs.con cordia.ca wrote:
      Well, I tried this test:
      >
      ////// include_iostrea m.h START //////////
      #include<iostre am>
      ////// include_iostrea m.h END ////////////
      >
      >
      ////// including_twice .cpp START //////////
      >
      #include<iostre am>
      >
      #include "include_iostre am.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


      Comment

      • p_adib@encs.concordia.ca

        #4
        Re: repeating intructions in .h and .cpp files

        To illustrate a conflict, try:
        >
        // header1.hpp////////////////
        int a = 0;
        >
        // header2.hpp////////////////
        #include "header1.hp p"
        >
        // main.cpp///////////////////
        #include "header1.hp p"
        #include "header2.hp p"
        >
        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\tes t\include & define
        test\ggtestdoub leinclusion\def inetest\header1 .h(2) : error C2374: 'a' :
        redefinition; multiple initialization
        g:\my documents\f-2006\446\a3\tes t\include & define

        test\ggtestdoub leinclusion\def inetest\header1 .h(2) : see declaration of
        'a'
        g:\my documents\f-2006\446\a3\tes t\include & define

        test\ggtestdoub leinclusion\def inetest\main.cp p(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)

        Comment

        • Pete Becker

          #5
          Re: repeating intructions in .h and .cpp files

          p_adib@encs.con cordia.ca wrote:
          >
          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.

          Comment

          • p_adib@encs.concordia.ca

            #6
            Re: repeating intructions in .h and .cpp files

            Pete Becker wrote:
            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?

            Comment

            • Victor Bazarov

              #7
              Re: repeating intructions in .h and .cpp files

              p_adib@encs.con cordia.ca wrote:
              Pete Becker wrote:
              >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


              Comment

              • BobR

                #8
                Re: repeating intructions in .h and .cpp files


                Victor Bazarov wrote in message ...
                >p_adib@encs.co ncordia.ca wrote:
                >Pete Becker wrote:
                >>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


                Comment

                Working...