Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Multiple includes

Multiple includes

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++visual-studiolinux
27 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shy Agam
    wrote on last edited by
    #1

    Hello experts, I recently solved my duplicate includes problem using the #ifndef-#define-#endif pre-processors. However, this somehow causes a problem if classes from the same .h file should be used by different .cpp files. The first .cpp file including the .h file gets compiled, but the others throw an "undefined reference" compile error. It's as if I never included the .h file on the other files. How can I overcome both duplicate includes and single includes problems? A bit on my workstation: OS: Ubuntu. IDE: Eclipse. Compiler: g++ Thanks in advance, Shy.

    C CPalliniC S 3 Replies Last reply
    0
    • S Shy Agam

      Hello experts, I recently solved my duplicate includes problem using the #ifndef-#define-#endif pre-processors. However, this somehow causes a problem if classes from the same .h file should be used by different .cpp files. The first .cpp file including the .h file gets compiled, but the others throw an "undefined reference" compile error. It's as if I never included the .h file on the other files. How can I overcome both duplicate includes and single includes problems? A bit on my workstation: OS: Ubuntu. IDE: Eclipse. Compiler: g++ Thanks in advance, Shy.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Shy Agam wrote:

      However, this somehow causes a problem if classes from the same .h file should be used by different .cpp files. The first .cpp file including the .h file gets compiled, but the others throw an "undefined reference" compile error. It's as if I never included the .h file on the other files.

      That's not how include guards work. They prevent including the same header file in one compilation unit (thus, one cpp file) but doesn't prevent multiple cpp files to include the same header file.

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      1 Reply Last reply
      0
      • S Shy Agam

        Hello experts, I recently solved my duplicate includes problem using the #ifndef-#define-#endif pre-processors. However, this somehow causes a problem if classes from the same .h file should be used by different .cpp files. The first .cpp file including the .h file gets compiled, but the others throw an "undefined reference" compile error. It's as if I never included the .h file on the other files. How can I overcome both duplicate includes and single includes problems? A bit on my workstation: OS: Ubuntu. IDE: Eclipse. Compiler: g++ Thanks in advance, Shy.

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Did you use the guards properly, e.g.

        // myclasses.h
        #ifndef _MYCLASSES_H_
        #define _MYCLASSES_H_

        // class definitions here...

        #endif // _MYCLASSES_H_

        // mysource1.cpp
        //...
        #include "myclasses.h"

        // use the classes here

        // mysource2.cpp
        //...
        #include "myclasses.h"

        // use the classes here

        ? :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        S 1 Reply Last reply
        0
        • CPalliniC CPallini

          Did you use the guards properly, e.g.

          // myclasses.h
          #ifndef _MYCLASSES_H_
          #define _MYCLASSES_H_

          // class definitions here...

          #endif // _MYCLASSES_H_

          // mysource1.cpp
          //...
          #include "myclasses.h"

          // use the classes here

          // mysource2.cpp
          //...
          #include "myclasses.h"

          // use the classes here

          ? :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          S Offline
          S Offline
          Shy Agam
          wrote on last edited by
          #4

          Indeed. However, I neglected to mention that the headers and their .cpps are in one shared-library project, and there's another project using this library. The other project is the one which fails to compile. It seems that this is some kind of linkage problem of some sort, but I can't figure it out. I copied the main file form my executable project into the library project, just to see if it compiles, and it compiled perfectly. Might I be missing on a specific linkage configuration?

          C 1 Reply Last reply
          0
          • S Shy Agam

            Indeed. However, I neglected to mention that the headers and their .cpps are in one shared-library project, and there's another project using this library. The other project is the one which fails to compile. It seems that this is some kind of linkage problem of some sort, but I can't figure it out. I copied the main file form my executable project into the library project, just to see if it compiles, and it compiled perfectly. Might I be missing on a specific linkage configuration?

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            Shy Agam wrote:

            It seems that this is some kind of linkage problem of some sort, but I can't figure it out.

            Please, post the full error message. This confirms that the problem has nothing to do with include guards, otherwise you would get a compilation error, not a linker error. Are you sure you linked correctly to the library ?

            Cédric Moonen Software developer
            Charting control [v3.0] OpenGL game tutorial in C++

            S 1 Reply Last reply
            0
            • C Cedric Moonen

              Shy Agam wrote:

              It seems that this is some kind of linkage problem of some sort, but I can't figure it out.

              Please, post the full error message. This confirms that the problem has nothing to do with include guards, otherwise you would get a compilation error, not a linker error. Are you sure you linked correctly to the library ?

              Cédric Moonen Software developer
              Charting control [v3.0] OpenGL game tutorial in C++

              S Offline
              S Offline
              Shy Agam
              wrote on last edited by
              #6

              #include <HeaderFileFromLibrary.h>

              using namespace NamespaceFromHeaderFile;

              int main(int argc, char **args)
              {
              ClassFromHeaderFile *pClass = ClassFromHeaderFile::GetInstance();

              .
              .
              .
              
              return 0;
              

              }

              The first row in main throws the following compile error: undefined reference to `NamespaceFromHeaderFile::ClassFromHeaderFile::GetInstance()'

              Cedric Moonen wrote:

              Are you sure you linked correctly to the library ?

              AAMOF I'm not experienced with C++ coding. As I am an experienced developer in other languages, I can write proper code (Or so I would like to think ;) ), but I don't have the experience for configuring my projects properly. As far as I can tell I did link the library correctly. Any way to make sure of it?

              modified on Monday, August 30, 2010 7:40 AM

              C L E 3 Replies Last reply
              0
              • S Shy Agam

                #include <HeaderFileFromLibrary.h>

                using namespace NamespaceFromHeaderFile;

                int main(int argc, char **args)
                {
                ClassFromHeaderFile *pClass = ClassFromHeaderFile::GetInstance();

                .
                .
                .
                
                return 0;
                

                }

                The first row in main throws the following compile error: undefined reference to `NamespaceFromHeaderFile::ClassFromHeaderFile::GetInstance()'

                Cedric Moonen wrote:

                Are you sure you linked correctly to the library ?

                AAMOF I'm not experienced with C++ coding. As I am an experienced developer in other languages, I can write proper code (Or so I would like to think ;) ), but I don't have the experience for configuring my projects properly. As far as I can tell I did link the library correctly. Any way to make sure of it?

                modified on Monday, August 30, 2010 7:40 AM

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #7

                Shy Agam wrote:

                As far as I can tell I did link the library correctly.

                How did you do it ? I am not experienced with g++ so I don't know exactly how to specify library to link. From the error you get, chances are that you didn't link to the library properly.

                Cédric Moonen Software developer
                Charting control [v3.0] OpenGL game tutorial in C++

                1 Reply Last reply
                0
                • S Shy Agam

                  #include <HeaderFileFromLibrary.h>

                  using namespace NamespaceFromHeaderFile;

                  int main(int argc, char **args)
                  {
                  ClassFromHeaderFile *pClass = ClassFromHeaderFile::GetInstance();

                  .
                  .
                  .
                  
                  return 0;
                  

                  }

                  The first row in main throws the following compile error: undefined reference to `NamespaceFromHeaderFile::ClassFromHeaderFile::GetInstance()'

                  Cedric Moonen wrote:

                  Are you sure you linked correctly to the library ?

                  AAMOF I'm not experienced with C++ coding. As I am an experienced developer in other languages, I can write proper code (Or so I would like to think ;) ), but I don't have the experience for configuring my projects properly. As far as I can tell I did link the library correctly. Any way to make sure of it?

                  modified on Monday, August 30, 2010 7:40 AM

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Are you sure you are using the ClassFromHeaderFile definitions correctly. Is GetInstance() a static member with public visibility?

                  It's time for a new signature.

                  P S 2 Replies Last reply
                  0
                  • L Lost User

                    Are you sure you are using the ClassFromHeaderFile definitions correctly. Is GetInstance() a static member with public visibility?

                    It's time for a new signature.

                    P Offline
                    P Offline
                    Paul Michalik
                    wrote on last edited by
                    #9

                    No, what he's getting is an linker error. You either do not link the library which contains the definition, or you're linking in wrong order, or you haven't configured your linker properly. Please, post the makefile (or the command line) you are using to build and link the program.

                    L 1 Reply Last reply
                    0
                    • S Shy Agam

                      #include <HeaderFileFromLibrary.h>

                      using namespace NamespaceFromHeaderFile;

                      int main(int argc, char **args)
                      {
                      ClassFromHeaderFile *pClass = ClassFromHeaderFile::GetInstance();

                      .
                      .
                      .
                      
                      return 0;
                      

                      }

                      The first row in main throws the following compile error: undefined reference to `NamespaceFromHeaderFile::ClassFromHeaderFile::GetInstance()'

                      Cedric Moonen wrote:

                      Are you sure you linked correctly to the library ?

                      AAMOF I'm not experienced with C++ coding. As I am an experienced developer in other languages, I can write proper code (Or so I would like to think ;) ), but I don't have the experience for configuring my projects properly. As far as I can tell I did link the library correctly. Any way to make sure of it?

                      modified on Monday, August 30, 2010 7:40 AM

                      E Offline
                      E Offline
                      Emilio Garavaglia
                      wrote on last edited by
                      #10

                      This is not a COMPILER, but a LINKER ERROR. It has nothing to deal with guards, but to the fact that your main project (not source files, that way it is defined in the IDE environment) doesn't know about the existence of the library project. Since you're using eclipse, you should go to the main project settings and configure it to link also the artifact produced by the library project and configure the projects dependencies so that the library is always built BEFORE the main project. This is one of the most convoluted and confused things that the eclipse CDT has. I've have to admit that -besides a good syntax parser and analyzer- for these relatively trivial things, really sucks!

                      2 bugs found. > recompile ... 65534 bugs found. :doh:

                      1 Reply Last reply
                      0
                      • L Lost User

                        Are you sure you are using the ClassFromHeaderFile definitions correctly. Is GetInstance() a static member with public visibility?

                        It's time for a new signature.

                        S Offline
                        S Offline
                        Shy Agam
                        wrote on last edited by
                        #11

                        Richard MacCutchan wrote:

                        Is GetInstance() a static member with public visibility?

                        Yes.

                        1 Reply Last reply
                        0
                        • P Paul Michalik

                          No, what he's getting is an linker error. You either do not link the library which contains the definition, or you're linking in wrong order, or you haven't configured your linker properly. Please, post the makefile (or the command line) you are using to build and link the program.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          paul_71 wrote:

                          No, what he's getting is an linker error.

                          His messages are not clear as to whether this is a compiler or linker error. BTW this message should have been in reply to the OP not to me.

                          It's time for a new signature.

                          P 1 Reply Last reply
                          0
                          • S Shy Agam

                            Hello experts, I recently solved my duplicate includes problem using the #ifndef-#define-#endif pre-processors. However, this somehow causes a problem if classes from the same .h file should be used by different .cpp files. The first .cpp file including the .h file gets compiled, but the others throw an "undefined reference" compile error. It's as if I never included the .h file on the other files. How can I overcome both duplicate includes and single includes problems? A bit on my workstation: OS: Ubuntu. IDE: Eclipse. Compiler: g++ Thanks in advance, Shy.

                            S Offline
                            S Offline
                            Shy Agam
                            wrote on last edited by
                            #13

                            It appears you guys were right. This is a linker issue. Eclipse lets you specify a reference to your other project. Being a devoted Visual Studio for .NET developer, I guess I assumed adding the reference was suffice. However, I've found the specific settings needed for linking the library. For future references to this post: 1. Open the project's settings. 2. Expand "C/C++ Build" and select "Settings". 3. Under "Tool Settings" expand "GCC C++ Linker" and select "Libraries". 4. Add the name of the library on the top box, and the path to its files on the bottom one. (Note that the path to the library should be the project's Debug directory) So now the project gets compiled, and the linker no longer whines about not finding the library. However I get some new linking errors. The new console output is as follows: make all Building target: Maple Invoking: GCC C++ Linker g++ -L"/LibraryPath/Debug" -o"Maple" ./Source/Maple.o -lCursesPlus /LibraryPath/Debug/libCursesPlus.so: undefined reference to `CursesPlus::CursesPlusEngine::initialized' . . . collect2: ld returned 1 exit status make: *** [Maple] Error 1 The vertical three dots hold place for a series of "undefined reference to..." errors, which are similar to the first error, but specify a different member of CursesPlusEngine. And to make it clear: Maple is my main executable project, and libCursesPlus is my library project. --Edit-- All of the above members are defined as private static in their corresponding header files.

                            modified on Tuesday, August 31, 2010 4:56 AM

                            P S 3 Replies Last reply
                            0
                            • L Lost User

                              paul_71 wrote:

                              No, what he's getting is an linker error.

                              His messages are not clear as to whether this is a compiler or linker error. BTW this message should have been in reply to the OP not to me.

                              It's time for a new signature.

                              P Offline
                              P Offline
                              Paul Michalik
                              wrote on last edited by
                              #14

                              Sorry, these hierarchical"replies" suck!

                              L 1 Reply Last reply
                              0
                              • S Shy Agam

                                It appears you guys were right. This is a linker issue. Eclipse lets you specify a reference to your other project. Being a devoted Visual Studio for .NET developer, I guess I assumed adding the reference was suffice. However, I've found the specific settings needed for linking the library. For future references to this post: 1. Open the project's settings. 2. Expand "C/C++ Build" and select "Settings". 3. Under "Tool Settings" expand "GCC C++ Linker" and select "Libraries". 4. Add the name of the library on the top box, and the path to its files on the bottom one. (Note that the path to the library should be the project's Debug directory) So now the project gets compiled, and the linker no longer whines about not finding the library. However I get some new linking errors. The new console output is as follows: make all Building target: Maple Invoking: GCC C++ Linker g++ -L"/LibraryPath/Debug" -o"Maple" ./Source/Maple.o -lCursesPlus /LibraryPath/Debug/libCursesPlus.so: undefined reference to `CursesPlus::CursesPlusEngine::initialized' . . . collect2: ld returned 1 exit status make: *** [Maple] Error 1 The vertical three dots hold place for a series of "undefined reference to..." errors, which are similar to the first error, but specify a different member of CursesPlusEngine. And to make it clear: Maple is my main executable project, and libCursesPlus is my library project. --Edit-- All of the above members are defined as private static in their corresponding header files.

                                modified on Tuesday, August 31, 2010 4:56 AM

                                P Offline
                                P Offline
                                Paul Michalik
                                wrote on last edited by
                                #15

                                Since you've only got 1 library and 1 runner, and I assume you're not trying to use code from runner in the library, there is only one possibility left: you forgot to add the proper source file(s) to your project(s)...

                                1 Reply Last reply
                                0
                                • S Shy Agam

                                  It appears you guys were right. This is a linker issue. Eclipse lets you specify a reference to your other project. Being a devoted Visual Studio for .NET developer, I guess I assumed adding the reference was suffice. However, I've found the specific settings needed for linking the library. For future references to this post: 1. Open the project's settings. 2. Expand "C/C++ Build" and select "Settings". 3. Under "Tool Settings" expand "GCC C++ Linker" and select "Libraries". 4. Add the name of the library on the top box, and the path to its files on the bottom one. (Note that the path to the library should be the project's Debug directory) So now the project gets compiled, and the linker no longer whines about not finding the library. However I get some new linking errors. The new console output is as follows: make all Building target: Maple Invoking: GCC C++ Linker g++ -L"/LibraryPath/Debug" -o"Maple" ./Source/Maple.o -lCursesPlus /LibraryPath/Debug/libCursesPlus.so: undefined reference to `CursesPlus::CursesPlusEngine::initialized' . . . collect2: ld returned 1 exit status make: *** [Maple] Error 1 The vertical three dots hold place for a series of "undefined reference to..." errors, which are similar to the first error, but specify a different member of CursesPlusEngine. And to make it clear: Maple is my main executable project, and libCursesPlus is my library project. --Edit-- All of the above members are defined as private static in their corresponding header files.

                                  modified on Tuesday, August 31, 2010 4:56 AM

                                  S Offline
                                  S Offline
                                  Shy Agam
                                  wrote on last edited by
                                  #16

                                  It appears I had to declare the static member variables in both the header files, and the source files. The project now compiles successfully. What's the idea behind this repetitive declaration? It's not like a function which is declared in the header, and defined in the source. It's simply a variable... Oo *Confused*

                                  A 1 Reply Last reply
                                  0
                                  • S Shy Agam

                                    It appears I had to declare the static member variables in both the header files, and the source files. The project now compiles successfully. What's the idea behind this repetitive declaration? It's not like a function which is declared in the header, and defined in the source. It's simply a variable... Oo *Confused*

                                    A Offline
                                    A Offline
                                    Aescleal
                                    wrote on last edited by
                                    #17

                                    When you declare a static data member in a class you're telling the compiler: "This class has a static member called ." When you define it outside of a class you're telling the compiler: "You know that variable ? This is where you reserve memory for it and initialise it with ." So it really is identically like a function which is declared in a header and defined in the source. Cheers, Ash PS: It's also identical to the behaviour of global variables - declare in a header, define in source. Which is a big hint why statics are bad, bad, bad...

                                    T 1 Reply Last reply
                                    0
                                    • P Paul Michalik

                                      Sorry, these hierarchical"replies" suck!

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #18

                                      paul_71 wrote:

                                      these hierarchical"replies" suck!

                                      Well, they take a bit of getting used to, but sometimes they work better than the other style.

                                      It's time for a new signature.

                                      1 Reply Last reply
                                      0
                                      • S Shy Agam

                                        It appears you guys were right. This is a linker issue. Eclipse lets you specify a reference to your other project. Being a devoted Visual Studio for .NET developer, I guess I assumed adding the reference was suffice. However, I've found the specific settings needed for linking the library. For future references to this post: 1. Open the project's settings. 2. Expand "C/C++ Build" and select "Settings". 3. Under "Tool Settings" expand "GCC C++ Linker" and select "Libraries". 4. Add the name of the library on the top box, and the path to its files on the bottom one. (Note that the path to the library should be the project's Debug directory) So now the project gets compiled, and the linker no longer whines about not finding the library. However I get some new linking errors. The new console output is as follows: make all Building target: Maple Invoking: GCC C++ Linker g++ -L"/LibraryPath/Debug" -o"Maple" ./Source/Maple.o -lCursesPlus /LibraryPath/Debug/libCursesPlus.so: undefined reference to `CursesPlus::CursesPlusEngine::initialized' . . . collect2: ld returned 1 exit status make: *** [Maple] Error 1 The vertical three dots hold place for a series of "undefined reference to..." errors, which are similar to the first error, but specify a different member of CursesPlusEngine. And to make it clear: Maple is my main executable project, and libCursesPlus is my library project. --Edit-- All of the above members are defined as private static in their corresponding header files.

                                        modified on Tuesday, August 31, 2010 4:56 AM

                                        P Offline
                                        P Offline
                                        Paul Michalik
                                        wrote on last edited by
                                        #19

                                        No! You may declare your static class members only once. What you have missed, is the definition (which also has to be unique), and it needs to be provided outside of the class, for non-const and non-trivial objects.

                                        // in AStuff.h
                                        class AStuff {
                                        public:
                                        void Hello() {
                                        // ...
                                        }
                                        };

                                        // in A.h
                                        class A {
                                        public:
                                        static AStuff sStaticAMember; // <-declaration
                                        };

                                        // in A.cpp
                                        AStuff A::sStaticAMember; //<-definition (required!)

                                        The constructor for A::sStaticAMember will be called by the runtime (dynamic initialization) if the instance is used by the program. You do not (really) have the control over when this is going to happen, neither when the destructor is going to be called. This is one of the reason why the poster above damned "statics" ... and basically I have to agree with him. Are you using C# otherwise? If yes, then forget about the comfort of static class objects and their well defined initialization there - and enter one of the many dark zones of c++. I assume you will probably run into the problem of undefined order of initializations of your non-local objects, but we stand ready to help :)

                                        modified on Tuesday, August 31, 2010 12:56 PM

                                        S 1 Reply Last reply
                                        0
                                        • A Aescleal

                                          When you declare a static data member in a class you're telling the compiler: "This class has a static member called ." When you define it outside of a class you're telling the compiler: "You know that variable ? This is where you reserve memory for it and initialise it with ." So it really is identically like a function which is declared in a header and defined in the source. Cheers, Ash PS: It's also identical to the behaviour of global variables - declare in a header, define in source. Which is a big hint why statics are bad, bad, bad...

                                          T Offline
                                          T Offline
                                          Tim Craig
                                          wrote on last edited by
                                          #20

                                          Aescleal wrote:

                                          PS: It's also identical to the behaviour of global variables - declare in a header, define in source. Which is a big hint why statics are bad, bad, bad...

                                          That's a rather sweeping statement. Parts of the language are there for a reason. There are cases where you absolutely need them. Can you misuse them? Certainly. But just yelling bad, bad, bad, ignores the valid cases.

                                          Once you agree to clans, tribes, governments...you've opted for socialism. The rest is just details.

                                          A 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups