Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, I am trying to link two object files compiled with g++ but when i enter the following command Code: $ ld main.o Time.o I get the following errors Code: ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2012
    Posts
    3

    Linker Errors


    Hi, I am trying to link two object files compiled with g++ but when i enter the following command

    Code:
    $ ld main.o Time.o
    I get the following errors

    Code:
    ld: warning: cannot find entry symbol _start; defaulting to 00000000004000e8
    main.o: In function `main':
    main.cpp:(.text+0x41): undefined reference to `std::cout'
    main.cpp:(.text+0x46): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)'
    main.o: In function `__static_initialization_and_destruction_0(int, int)':
    main.cpp:(.text+0x80): undefined reference to `std::ios_base::Init::Init()'
    main.cpp:(.text+0x85): undefined reference to `std::ios_base::Init::~Init()'
    main.cpp:(.text+0x8a): undefined reference to `__dso_handle'
    main.cpp:(.text+0x97): undefined reference to `__cxa_atexit'
    main.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
    Time.o: In function `Time::setTime(int, int, int)':
    Time.cpp:(.text+0xce): undefined reference to `std::allocator<char>::allocator()'
    Time.cpp:(.text+0xe3): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
    Time.cpp:(.text+0xf1): undefined reference to `__cxa_allocate_exception'
    Time.cpp:(.text+0x102): undefined reference to `std::invalid_argument::invalid_argument(std::string const&)'
    Time.cpp:(.text+0x118): undefined reference to `__cxa_free_exception'
    Time.cpp:(.text+0x12f): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
    Time.cpp:(.text+0x142): undefined reference to `std::terminate()'
    Time.cpp:(.text+0x156): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
    Time.cpp:(.text+0x169): undefined reference to `std::terminate()'
    Time.cpp:(.text+0x180): undefined reference to `std::allocator<char>::~allocator()'
    Time.cpp:(.text+0x18e): undefined reference to `_Unwind_Resume'
    Time.cpp:(.text+0x19a): undefined reference to `std::allocator<char>::~allocator()'
    Time.cpp:(.text+0x1ac): undefined reference to `__cxa_throw'
    Time.o: In function `Time::printUniversal()':
    Time.cpp:(.text+0x20f): undefined reference to `std::cout'
    Time.cpp:(.text+0x214): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setfill<char>)'
    Time.cpp:(.text+0x21f): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setw)'
    Time.cpp:(.text+0x22a): undefined reference to `std::ostream::operator<<(int)'
    Time.cpp:(.text+0x237): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)'
    Time.cpp:(.text+0x242): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setw)'
    Time.cpp:(.text+0x24d): undefined reference to `std::ostream::operator<<(int)'
    Time.cpp:(.text+0x25a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)'
    Time.cpp:(.text+0x265): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setw)'
    Time.cpp:(.text+0x26f): undefined reference to `std::ostream::operator<<(int)'
    Time.o: In function `Time::printStandard()':
    Time.cpp:(.text+0x32c): undefined reference to `std::cout'
    Time.cpp:(.text+0x331): undefined reference to `std::ostream::operator<<(int)'
    Time.cpp:(.text+0x33e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)'
    Time.cpp:(.text+0x34a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setfill<char>)'
    Time.cpp:(.text+0x355): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setw)'
    Time.cpp:(.text+0x360): undefined reference to `std::ostream::operator<<(int)'
    Time.cpp:(.text+0x36d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)'
    Time.cpp:(.text+0x378): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setw)'
    Time.cpp:(.text+0x383): undefined reference to `std::ostream::operator<<(int)'
    Time.cpp:(.text+0x38e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    Time.o: In function `__static_initialization_and_destruction_0(int, int)':
    Time.cpp:(.text+0x3c4): undefined reference to `std::ios_base::Init::Init()'
    Time.cpp:(.text+0x3c9): undefined reference to `std::ios_base::Init::~Init()'
    Time.cpp:(.text+0x3ce): undefined reference to `__dso_handle'
    Time.cpp:(.text+0x3db): undefined reference to `__cxa_atexit'
    Time.o: In function `std::invalid_argument::~invalid_argument()':
    Time.cpp:(.text._ZNSt16invalid_argumentD1Ev[_ZNSt16invalid_argumentD1Ev]+0x1f): undefined reference to `std::logic_error::~logic_error()'
    Time.cpp:(.text._ZNSt16invalid_argumentD1Ev[_ZNSt16invalid_argumentD1Ev]+0x34): undefined reference to `operator delete(void*)'
    Time.o: In function `std::invalid_argument::~invalid_argument()':
    Time.cpp:(.text._ZNSt16invalid_argumentD0Ev[_ZNSt16invalid_argumentD0Ev]+0x1f): undefined reference to `std::logic_error::~logic_error()'
    Time.cpp:(.text._ZNSt16invalid_argumentD0Ev[_ZNSt16invalid_argumentD0Ev]+0x34): undefined reference to `operator delete(void*)'
    Time.o:(.rodata._ZTVSt16invalid_argument[_ZTVSt16invalid_argument]+0x20): undefined reference to `std::logic_error::what() const'
    Time.o:(.rodata._ZTISt16invalid_argument[_ZTISt16invalid_argument]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
    Time.o:(.rodata._ZTISt16invalid_argument[_ZTISt16invalid_argument]+0x10): undefined reference to `typeinfo for std::logic_error'
    Time.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
    Time.o:(.eh_frame+0xbb): undefined reference to `__gxx_personality_v0'
    I trying linking to stdc++ and libstdc++ and the results were

    Code:
    ld: cannot find -lstdc++
    ld: cannot find -llibstdc++
    I greatly appreciate any help in solving this problem.

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,211
    The correct syntax is -lstdc++. So if it's not being found when you use that linker flag, it probably isn't there! Have you tried going to /usr/libs and actually looking for it? The file should be called libstdc++.so. If it's not there, you'll have to install it.
    "I'm just a little old lady; don't try to dazzle me with jargon!"
    www.hrussman.entadsl.com

  3. #3
    Just Joined!
    Join Date
    Jun 2012
    Posts
    3
    I did look for the file before posting on this forum and found "libstdc++.a" and "libstdc++.so" in directory "/usr/lib/gcc/x86_64-linux-gnu/4.4". I also found "libstdc++.so.6" and "libstdc++.so.6.0.13" in "/usr/lib".
    but when i typed the command

    Code:
    $ ld Time.o main.o -lstdc++
    i got

    Code:
    ld: cannot find -lstdc++
    I was also curious as to which one of those shared libraries is the correct one and i was also wondering if the linker isn't configured to search in the proper directories.

    Also the object files link successfully with the commands:

    Code:
    $ gcc Time.o main.o -lstdc++  or   $ g++ Time.o main.o
    Maybe if there is a way to determine where gcc or g++ search for libstdc++ or if there is a way to see the command used by either of these commands to link the files.

    Thanks for your help and for responding so quickly!!
    Last edited by gurbos; 08-09-2012 at 04:21 AM.

  4. #4
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,211
    I don't know why you are getting this error, but the answer to your other question is that libstdc++.so.6.0.13 is the actual file containing the library. libstc++.so.6 is a link to it, and libstdc++.so is a link to libstdc++.so.6. All linux libraries are arranged like that. The programs are actually linked directly to libstdc++.so, which links through to the actual library. That way, if you upgrade to libstdc++.so.6.0.14 or even libstdc++.so.6.1, your C++ programs will still all work.
    "I'm just a little old lady; don't try to dazzle me with jargon!"
    www.hrussman.entadsl.com

  5. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,286
    Hi.

    The verbose option on g++ along a few other utilities often help us see what is happening behind the scenes. Here is a demonstration of compile, link, execute of hello, world:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s2	Demonstrate verbose, compile and link, g++.
    
    echo
    set +o nounset
    LC_ALL=C ; LANG=C ; export LC_ALL LANG
    echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
    echo "(Versions displayed with local utility \"version\")"
    version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) g++ ld
    set -o nounset
    
    echo
    echo " Source file:"
    cat hi.cpp
    
    rm -f a.out
    
    echo
    echo " Results, verbose compile, link:"
    g++ -v hi.cpp
    echo
    echo " Results, characteristics of compiled code:"
    file a.out
    echo
    echo " Results, shared library dependencies:"
    ldd a.out
    
    echo
    ./a.out
    
    rm -f a.out 
    
    exit 0
    producing:
    Code:
    % ./s2
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
    bash GNU bash 3.2.39
    g++ (Debian 4.3.2-1.1) 4.3.2
    ld GNU ld (GNU Binutils for Debian) 2.18.0.20080103
    
     Source file:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      cout << " Hello, world from c++\n";
      return ( 0 );
    }
    
     Results, verbose compile, link:
    Using built-in specs.
    Target: x86_64-linux-gnu
    Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.2-1.1' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-cld --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
    Thread model: posix
    gcc version 4.3.2 (Debian 4.3.2-1.1) 
    COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic'
     /usr/lib/gcc/x86_64-linux-gnu/4.3.2/cc1plus -quiet -v -D_GNU_SOURCE hi.cpp -quiet -dumpbase hi.cpp -mtune=generic -auxbase hi -version -o /tmp/ccCPhkkN.s
    ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
    ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../x86_64-linux-gnu/include"
    ignoring nonexistent directory "/usr/include/x86_64-linux-gnu"
    #include "..." search starts here:
    #include <...> search starts here:
     /usr/include/c++/4.3
     /usr/include/c++/4.3/x86_64-linux-gnu
     /usr/include/c++/4.3/backward
     /usr/local/include
     /usr/lib/gcc/x86_64-linux-gnu/4.3.2/include
     /usr/lib/gcc/x86_64-linux-gnu/4.3.2/include-fixed
     /usr/include
    End of search list.
    GNU C++ (Debian 4.3.2-1.1) version 4.3.2 (x86_64-linux-gnu)
    	compiled by GNU C version 4.3.2, GMP version 4.2.2, MPFR version 2.3.2.
    warning: MPFR header version 2.3.2 differs from library version 2.3.1.
    GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
    Compiler executable checksum: 836c9d30f580c422b521aa9b7a9bf559
    COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic'
     as -V -Qy -o /tmp/ccGeVzcI.o /tmp/ccCPhkkN.s
    GNU assembler version 2.18.0 (x86_64-linux-gnu) using BFD version (GNU Binutils for Debian) 2.18.0.20080103
    COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.3.2/:/usr/lib/gcc/x86_64-linux-gnu/4.3.2/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.3.2/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.3.2/:/usr/lib/gcc/x86_64-linux-gnu/
    LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.3.2/:/usr/lib/gcc/x86_64-linux-gnu/4.3.2/:/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../:/lib/:/usr/lib/
    COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic'
     /usr/lib/gcc/x86_64-linux-gnu/4.3.2/collect2 --eh-frame-hdr -m elf_x86_64 --hash-style=both -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.3.2/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.3.2 -L/usr/lib/gcc/x86_64-linux-gnu/4.3.2 -L/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../.. /tmp/ccGeVzcI.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/4.3.2/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/crtn.o
    
     Results, characteristics of compiled code:
    a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.8, not stripped
    
     Results, shared library dependencies:
    	linux-vdso.so.1 =>  (0x00007fff30724000)
    	libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007f228058f000)
    	libm.so.6 => /lib/libm.so.6 (0x00007f228030c000)
    	libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00007f22800f5000)
    	libc.so.6 => /lib/libc.so.6 (0x00007f227fda2000)
    	/lib64/ld-linux-x86-64.so.2 (0x00007f228089b000)
    
     Hello, world from c++
    The built-in link phase consults many libraries.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

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