CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Dec 2003
    Posts
    30

    Linking problem - "undefined reference"

    Hi,

    I try to Link my compiled object file with gcc, but
    it gives me 50 lines of "undefined reference" errors, despite
    the compile phase goes fine?!

    Code:
    ...
    Utils.o(.text+0xf):Utils.cpp: undefined reference to `operator new(unsigned long)'
    Utils.o(.text+0x2e):Utils.cpp: undefined reference to `operator delete(void*)'
    Utils.o(.text+0x5e):Utils.cpp: undefined reference to `std::ios_base::Init::Init()'
    Utils.o(.text+0xbc):Utils.cpp: undefined reference to `std::ios_base::Init::~Init()'
    Utils.o(.gnu.linkonce.t._ZN5Utils7printDescriptionEv+0xf):Utils.cpp: undefined reference to `std::cout'
    ...
    well, I have installed all the required libraries for gcc, I think,

    These are what I have included in the source file:

    Code:
    using namespace std;
    
    #include <iostream>
    
    
    #include <string>
    #include <vector>
    #include <fstream>
    
    #include "FileUtils.cpp"

  2. #2
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    You have installed them, but do you reference them? You seem to be lacking a reference to the standard C++ library.
    All the buzzt
    CornedBee

  3. #3
    Join Date
    Dec 2003
    Posts
    30
    >You seem to be lacking a reference to the standard C++ library.

    well, I´m migrating from Java to C++ and I`m a bit puzzled.
    Could you advice me a bit more.

    Do I have to tweak gcc options or do I have something
    wrong with my code?

  4. #4
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    It's the compiler options. Think of it as the class path.

    gcc source.cpp -lstdc++
    or something like that. Order matters!
    All the buzzt
    CornedBee

  5. #5
    Join Date
    Dec 2003
    Posts
    30
    gcc source.cpp -lstdc++
    ummmm... I`m lost. I`v tried to search to manual
    for an answer...but no,

    as far I understand, I have to inform the linker about
    some libraries. But I don`t know what are the names of
    those and where they reside?

    If I try to do the whole thin as follows:

    gcc -Wall -o myprog.exe mymain.c

    -> "undefined reference to..."

    Do I have to use the "-Xlinker" switch? If so, how to use it.


    ..ah, it`s allways pain in the beginning

  6. #6
    Join Date
    Dec 2003
    Posts
    30
    I mean, with -l or -L flag. But what to include?

  7. #7
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    -lc for the standard C library.
    I think -lstdc++ for the standard C++ library.

    Then there are other libraries: to get libpng.a you'd write
    -lpng




    I just noticed you #include a .cpp. Don't ever do that!
    All the buzzt
    CornedBee

  8. #8
    Join Date
    Dec 2003
    Posts
    30
    I just tried the following

    Code:
    C:\projects\test>gcc -lstdc++ -o Test.exe Test.o
    and with this
    Code:
    -Lc:\djgpp\lib\
    and it produced:

    Code:
    c:/djgpp/bin/ld.exe: cannot find -lstdc++
    collect2: ld returned 1 exit status
    does this mean I don`t have that library??



    I just noticed you #include a .cpp. Don't ever do that!
    hmm, I have seen two "ways" of coding with C++.

    1. "Like" in java, you wite the .cpp file. No header file.
    2. make a header file, and then the .cpp file, like:
    MyClass::MyMethod()
    { ... }
    whitout enclosing the functions in a class scope ( like in Java)

  9. #9
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    Been a long time since I used gcc this way. Never MingW. I really don't know.
    All the buzzt
    CornedBee

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449
    Since you are using gcc, I suggest getting one of the IDE's that complement gcc. Dev C++ (aka Bloodshed C++) is an IDE for gcc, and it's free. You don't worry about all of these problems of linking, since they have built into the IDE the proper linker flags and paths.
    Originally posted by halmark6Z
    hmm, I have seen two "ways" of coding with C++.
    1. "Like" in java, you wite the .cpp file. No header file.
    2. make a header file, and then the .cpp file, like:
    MyClass::MyMethod()
    { ... }
    whitout enclosing the functions in a class scope ( like in Java)
    Please do not use Java as a model to programming C++. In C++, header files usually contain function prototypes and struct and class definitions, as well as #defined constants (aka macros). C++ header files are simply files that contain definitions that you would want to include in more than one CPP file. They should not be compared to Java's #import, which (at least to me) is more than just an "insert file here" command.

    Just as an addendum, there was another poster a few weeks ago who was insistent on writing C++ like Java, since they came from a Java background. He/she was creating some sort of Object class similar to Java, so that they can create a Vector class. All of this was unnecessary, since C++ already has a vector class. I'm saying this because there are Java programmers who want to turn C++ into Java so as to be more "comfortable", but that isn't the way to go about learning proper C++.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    instead of typing "gcc" try "g++"


    g++ source.cpp

  12. #12
    Join Date
    Dec 2003
    Posts
    30
    Finally, I got it working. Thank for helping, really!

    I used gxx instead of g++

    Well, I tried a bit further.

    I made a simple class to do set/get stuff.
    Then I created a file with a main function and I tried
    to use my DerivedSimpleClass there ( compiled to DerivedSimpleClass.o ) but
    the linker prompted:

    Code:
    C:\projects\test>gxx -o -lstdcxx Test.o DerivedSimpleClass.o SimpleClass.o
    Test.o(.gnu.linkonce.t._ZN8SimpleClassC2Ev+0x8):Test.cpp: undefined reference to `vtable for SimpleClass
    Test.o(.gnu.linkonce.t._ZN14DerivedSimpleClass15getContentEv+0xf):Test.cpp: undefined reference to `SimpleClass::getContent()'
    Test.o(.gnu.linkonce.t._ZN8SimpleClassD2Ev+0x7):Test.cpp: undefined reference to `vtable for SimpleClass
    collect2: ld returned 1 exit status
    So I guess I`m using the linker wrong again?

  13. #13
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    Try switching changing the order of the object files. ld can be picky.
    All the buzzt
    CornedBee

  14. #14
    Join Date
    Dec 2003
    Posts
    30
    I tried, it complains about the same thing:

    Code:
    gxx -o Test.exe Test.o DerivedSimpleClass.o SimpleClass.o
    gxx -o Test.exe SimpleClass.o DerivedSimpleClass.o Test.o
    gxx -o Test.exe DerivedSimpleClass.o Test.o SimpleClass.o
    they all produce:

    Code:
    Test.o(.gnu.linkonce.t._ZN14DerivedSimpleClass15getContentEv+0xf):Test.cpp: undefined reference to `SimpleClass::getContent()'
    collect2: ld returned 1 exit status
    and the code

    Code:
    //SimpleClass.cpp
    class SimpleClass
    {
    public:
      virtual string getContent() = 0;
    };
    
    //DerivedSimpleClass.cpp
    class DerivedSimpleClass : public SimpleClass
    {
    public:
      string getContent()
      {
        return "let me out";
      }
    };
    
    // Test.cpp
    
    int main(int argc, char* argv[])
    {
      // complains about this ?!
      DerivedSimpleClass *notworking = new DerivedSimpleClass();
      cout<<notworking->getContent();
    }
    Well, how does everybody in general deal with multiple .o files

  15. #15
    Join Date
    Dec 2003
    Posts
    30

    Wink

    fixed...I see light in the horizon....

    inheritance bug.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured