Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

C++ Standard Libraries

Hi!

I have started working with C++ in my project. Creating classes and using own objects is no problem,

Now I want to use objects of the standard library, e.g. class "string", but I get lots of linker errors.

My linker flags are:

ECOS_GLOBAL_LDFLAGS = -g -nostdlib -Wl,--gc-sections -Wl,-static -mno-hw-mul -mno-hw-div

Example of my source code:

# include <string>

using namespace std;

....

void ThrdMain(cyg_addrword_t data)

{

...

string s1 = "Hello";

string s2 = " ";

s2 = s1;

...

}

This causes many unresolved methods of the class "string":

background.o(.text.ThrdBackground+0x3c):/cygdrive/f/alt_projects/R106/application/src/background.cpp:104: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)&#39;

background.o(.text.ThrdBackground+0x44):/cygdrive/f/alt_projects/R106/application/src/background.cpp:104: undefined reference to `std::allocator<char>::~allocator()&#39;

background.o(.text.ThrdBackground+0x4c):/cygdrive/f/alt_projects/R106/application/src/background.cpp:42: undefined reference to `std::allocator<char>::allocator()&#39;

... and many more

Can anyone help me?

Thanks!

4 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Actually, eCos supports STL just fine; this is a function of GCC and not eCos. It&#39;s just that the "-nostdlib" flag makes it not link the standard C++ library by default.

    You need to add the "-lstdc++" flag to your link command, and it needs to be after all your .o files and any other libraries that use the STL.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    originally posted by mike desimone@Oct 5 2005, 12:22 PM

    actually, ecos supports stl just fine; this is a function of gcc and not ecos. it&#39;s just that the "-nostdlib" flag makes it not link the standard c++ library by default.

    you need to add the "-lstdc++" flag to your link command, and it needs to be after all your .o files and any other libraries that use the stl.

    <div align='right'><{post_snapback}> (index.php?act=findpost&pid=10180)

    --- quote end ---

    --- Quote End ---

    I have the same problem, and the -lstdc++ makes no difference. See below:

    make INSTALL_DIR=c:/Development/DefectAnalyzer/eCos/DspFull_Install all

    nios2-elf-gcc -nostartfiles -Lc:/Development/DefectAnalyzer/eCos/DspFull_Install/lib -Ttarget.ld -g -lstdc++ -Wl,--gc-sections -Wl,-static -mhw-mul -mhw-mulx -mno-hw-div -o hello hello.o

    hello.o(.gnu.linkonce.t._ZN9ExceptionD1Ev+0x68): In function `main&#39;:

    /cygdrive/c/altera/kits/nios2_60/bin/nios2-gnutools/H-i686-pc-cygwin/bin/../lib/gcc/nios2-elf/3.4.1/../../../../../include/c++/3.4.1/bits/basic_string.h:178: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep::_S_empty_rep_storage&#39;

    hello.o(.gnu.linkonce.t._ZN9ExceptionD1Ev+0x6c): In function `main&#39;:

    /cygdrive/c/Development/DefectAnalyzer/eCosProjects/examples/hello.cpp:30: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep::_S_empty_rep_storage&#39;

    [etc.]

    Any ideas about what to try next?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    originally posted by klocken+oct 17 2006, 08:24 pm--><div class='quotetop'>quote (klocken @ oct 17 2006, 08:24 pm)</div>

    --- quote start ---

    <!--quotebegin-mike desimone@Oct 5 2005, 12:22 PM

    actually, ecos supports stl just fine; this is a function of gcc and not ecos. it&#39;s just that the "-nostdlib" flag makes it not link the standard c++ library by default.

    you need to add the "-lstdc++" flag to your link command, and it needs to be after all your .o files and any other libraries that use the stl.

    <div align='right'><{post_snapback}> (index.php?act=findpost&pid=10180)

    --- quote end ---

    --- Quote End ---

    I have the same problem, and the -lstdc++ makes no difference. See below:

    make INSTALL_DIR=c:/Development/DefectAnalyzer/eCos/DspFull_Install all

    nios2-elf-gcc -nostartfiles -Lc:/Development/DefectAnalyzer/eCos/DspFull_Install/lib -Ttarget.ld -g -lstdc++ -Wl,--gc-sections -Wl,-static -mhw-mul -mhw-mulx -mno-hw-div -o hello hello.o

    hello.o(.gnu.linkonce.t._ZN9ExceptionD1Ev+0x68): In function `main&#39;:

    /cygdrive/c/altera/kits/nios2_60/bin/nios2-gnutools/H-i686-pc-cygwin/bin/../lib/gcc/nios2-elf/3.4.1/../../../../../include/c++/3.4.1/bits/basic_string.h:178: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep::_S_empty_rep_storage&#39;

    hello.o(.gnu.linkonce.t._ZN9ExceptionD1Ev+0x6c): In function `main&#39;:

    /cygdrive/c/Development/DefectAnalyzer/eCosProjects/examples/hello.cpp:30: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep::_S_empty_rep_storage&#39;

    [etc.]

    Any ideas about what to try next?

    <div align='right'><{post_snapback}> (index.php?act=findpost&pid=18759)</div>

    [/b]

    --- Quote End ---

    It looks like you didn&#39;t put the -lstdc++ after all the .o files that use the std C++

    library. It needs to be the last thing on the line. So, for example, you might see

    something like this in your makefile:

    @$(XLD) $(LDFLAGS) $(ECOS_GLOBAL_LDFLAGS) -o $@ $(OBJS) -lstdc++