Find the answer to your Linux question:
Results 1 to 8 of 8
Hello I have created a derived class, crectangle, but I can not compile a program? I have defined a constructor of this class in a .c file. The following errors ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2012
    Posts
    3

    'Undefined reference to' errors from a Linux c++ compiler?


    Hello

    I have created a derived class, crectangle, but I can not compile a program? I have defined a constructor of this class in a .c file. The following errors are from a SUSE Linux 9.3 c++ compiler:
    '... undefined reference to `vtable for crectangle'
    /tmp/ccQgl8Cs.o(.text+0x147): In function `crectangle::crectangle[in-charge](spoint, spoint)':
    types4.c: undefined reference to `vtable for crectangle'
    /tmp/ccQgl8Cs.o(.gnu.linkonce.t._ZN6sshapeC2Ev+0x1f): In function `sshape::sshape[not-in-charge]()':
    types4.c: undefined reference to `sshape::list'
    /tmp/ccQgl8Cs.o(.gnu.linkonce.t._ZN6sshapeC2Ev+0x2e):ty pes4.c: undefined reference to `sshape::list'
    collect2: ld returned 1 exit status',
    Can you help? Thank you.

    Tina

  2. #2
    Just Joined!
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    38
    It means that your code compiles but cannot be linked into an executable, because the linker doesn't know what it needs to be linked to.
    You must supply all needed .o files (or .c) as arguments to the compiler. It needs at least the file where the base class is defined.

  3. #3
    Just Joined!
    Join Date
    Dec 2012
    Posts
    3

    I have given a command c++ -o main main.cpp types.cpp types3.cpp ...

    Quote Originally Posted by kohog View Post
    It means that your code compiles but cannot be linked into an executable, because the linker doesn't know what it needs to be linked to.
    You must supply all needed .o files (or .c) as arguments to the compiler. It needs at least the file where the base class is defined.
    Hello

    I have given a command c++ -o main main.cpp types.cpp types3.cpp types4.cpp. A base class is in types3.h. The derived class is in types4.h. This file has included types3.h. types4.cpp has included types4.h.

    Tina

  4. #4
    Linux Newbie jkwilborn's Avatar
    Join Date
    Nov 2012
    Posts
    233
    Tina, I will try and give you a quick explanation. Although I'm not really a Linux guru, I did teach C for many years and have worked on many different compilers and systems.

    As I read this the previous person is correct, you code compiled OK, but when it came to linking the final executable it ended with undefined symbols, these are caused by:

    1, the symbols are not properly labeled 'extern' or 'export'ed to the symbol table during compile.

    2, I'm sure you checked spelling, so we'll rule that out.

    You need to find where these are defined, check you source for proper coding of the source to export the symbols in the symbol map. If it's in a header file, and that file is included in ALL references, then you should not have this failure. So, you probably have it defined somewhere that it's used by other code sequences (in other C source) and 'it' assumes it will be resolved during linking.

    Check out the 'export' rules in your source to ensure you are making them 'global' and they will show up in the symbol table to be resolved during the link process.

    Since you compiled multiple sources, even though on the same line, symbols that are not defined as global or extern or whatever you compiler needs, the symbols are lost. The compiler is invoked each time for each module so the symbol table is not added to and carried onward to the linker. This may be an option, but I'd suggest that you define them properly.

    Also, I don't know C++, so the your source code where you reference them may be different than you have coded, although I doubt it.

    You need to ensure that you export or make visible the address of the resolved symbols in the type4.ccp source file. Again I think it's the export keyword, but can't remember exactly.

    Best of luck, let us know if we are of assistance in the resolution.

    Jack

  5. #5
    Linux Newbie nplusplus's Avatar
    Join Date
    Apr 2010
    Location
    Charlotte, NC, USA
    Posts
    106
    Quote Originally Posted by tinah View Post
    Hello

    I have given a command c++ -o main main.cpp types.cpp types3.cpp types4.cpp. A base class is in types3.h. The derived class is in types4.h. This file has included types3.h. types4.cpp has included types4.h.

    Tina
    Still, the linker may not know to look in the current directory for your header files, so you may either need to provide an argument to look in the directory with the header files, or update an environment variable.

    N

  6. #6
    Linux Newbie jkwilborn's Avatar
    Join Date
    Nov 2012
    Posts
    233
    nplusnplus, the linker doesn't look at source files. Object files have symbols in them for linking and contain the 'global' symbols that are needed for the linking process. The source files are not needed in the link phase, so there will not be any 'looking' for source files by the linker. Only object (.o) files are used by the linker and sometimes map files, but usually the linker produces a map file for the debug process so you can look at variables by the name in the source and not by the actual address of the variable.

    Jack

  7. #7
    Just Joined!
    Join Date
    Dec 2012
    Posts
    3
    Quote Originally Posted by jkwilborn View Post
    Tina, I will try and give you a quick explanation. Although I'm not really a Linux guru, I did teach C for many years and have worked on many different compilers and systems.

    As I read this the previous person is correct, you code compiled OK, but when it came to linking the final executable it ended with undefined symbols, these are caused by:

    1, the symbols are not properly labeled 'extern' or 'export'ed to the symbol table during compile.

    2, I'm sure you checked spelling, so we'll rule that out.

    You need to find where these are defined, check you source for proper coding of the source to export the symbols in the symbol map. If it's in a header file, and that file is included in ALL references, then you should not have this failure. So, you probably have it defined somewhere that it's used by other code sequences (in other C source) and 'it' assumes it will be resolved during linking.

    Check out the 'export' rules in your source to ensure you are making them 'global' and they will show up in the symbol table to be resolved during the link process.

    Since you compiled multiple sources, even though on the same line, symbols that are not defined as global or extern or whatever you compiler needs, the symbols are lost. The compiler is invoked each time for each module so the symbol table is not added to and carried onward to the linker. This may be an option, but I'd suggest that you define them properly.

    Also, I don't know C++, so the your source code where you reference them may be different than you have coded, although I doubt it.

    You need to ensure that you export or make visible the address of the resolved symbols in the type4.ccp source file. Again I think it's the export keyword, but can't remember exactly.

    Best of luck, let us know if we are of assistance in the resolution.

    Jack
    Hello

    Thank you for all emails, I have experience of C. I have solved the errors. I have defined other member functions in the derived class. I have defined the static member of the base class.

    Tina
    Last edited by tinah; 12-26-2012 at 03:23 PM.

  8. #8
    Linux Newbie jkwilborn's Avatar
    Join Date
    Nov 2012
    Posts
    233
    Tina, you can go to the 'tools' menu at the top of the page and mark the item 'solved' to close it out...

    Jack

Posting Permissions

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