allexoll
Posts: 43
Joined: Sat Feb 23, 2013 12:27 pm

[SOLVED] getting libstdc++ to work

Fri May 01, 2015 11:39 am

Hi,
I recently finished my C bootloader, so when I started to write the main kernel, it seemed obvious to write it in C++.
I have the standard C library working with the needed system calls, but as I started to move to C++, a few errors came up. Mostly linkage errors.

As of now, I have <vector> working, but i still need to use printf for logging purposes, ostream throwing me about 500 lines of linkage errors, and about the same when i just want to have a std::string.

I have a few questions:
  1. I can see a difference when linking with g++ or ld. g++ throws me errors about vfp arguments, and --verbose tells me it just includes the default lib/ directory, not the lib/fpu/, even when I explicitly pass it as argument, where ld does the job just fine. Any Idea why that is?
  • <vector> works when I link with only libc and libm (C standard lib, not C++), but when I try <string>, if i don't link with libstdc++, I get

    Code: Select all

    obj/kernel.o: In function `main':
    :(.text+0x94): undefined reference to `std::allocator<char>::allocator()'
    :(.text+0xb0): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
    :(.text+0xbc): undefined reference to `std::allocator<char>::~allocator()'
    and if I do link with libstdc++, i get 500+ errors. First there is a few errors about multiple definition of the __throw* function (which I overloaded to resolve previous linkage errors), then lots of errors about stack unwinding (?) like __aeabi_unwind_cpp_pr0, and finally undefined references to standard function like fwrite, memcpy, memcmp, wmemset and so on. Every article/book/guide on baremetal C++ says it is a bad idea to have exceptions, where that would be too much for the small embedded targets. Would that be still such a problem on a target like the Raspi? I assume the libstdc++ is built on libc and its system calls, how right is this assumption? And finally, does the order in which the libs are passed to the linker matter?
Thanks in advance,

Allexoll

ps: if anyone cares to enlighten me on why every format except %d, %u, and %i works for printf, where those three are only right when printing single digit, please do so.

pps: I'm currently working on the Beaglebone black Bare metal, I have a few example at the moment, check it out: https://github.com/allexoll/BBB-BareMetal
Last edited by allexoll on Fri May 01, 2015 5:12 pm, edited 1 time in total.

kriss
Posts: 66
Joined: Thu Apr 02, 2015 8:53 pm
Location: france for now ...

Re: getting libstdc++ to work

Fri May 01, 2015 11:55 am

you ask for libstdc++ working on bare-metal PI but coding for beagleboard with beagle tools ?!?
you should not find same errors coding for PI/2 on it or bug report your distro ;)

allexoll
Posts: 43
Joined: Sat Feb 23, 2013 12:27 pm

Re: getting libstdc++ to work

Fri May 01, 2015 12:11 pm

This is more questions about the use of the toolchain and what not than about running the code. My kernel won't compile when I try to use a bit of the libstdc++...
And what do you mean by distro?


cheers,
Allexoll

kriss
Posts: 66
Joined: Thu Apr 02, 2015 8:53 pm
Location: france for now ...

Re: getting libstdc++ to work

Fri May 01, 2015 12:40 pm

the toolchain you use run on a distro or you make it ?
some people here are making c++ kernel and no such thing reported as a bad toolchain
can't you compile on a toolchain that run on your target ?

allexoll
Posts: 43
Joined: Sat Feb 23, 2013 12:27 pm

Re: getting libstdc++ to work

Fri May 01, 2015 1:11 pm

The toolchain I use is arm-none-eabi 4.9 from https://launchpad.net/gcc-arm-embedded/+download.
I'm not reporting this as a toolchain fault, more as me not understanding how I should use libstdc++ compilation and linking-wise.
I've been using this toolchain for a while now, and I didn't have much trouble until now.

Thanks

Allexoll

kriss
Posts: 66
Joined: Thu Apr 02, 2015 8:53 pm
Location: france for now ...

Re: getting libstdc++ to work

Fri May 01, 2015 1:25 pm

oops sorry i don't use c++ nor this toolchain so i'm not able to know if it's a bad install or a missing flag to g++
rst help

allexoll
Posts: 43
Joined: Sat Feb 23, 2013 12:27 pm

Re: getting libstdc++ to work

Fri May 01, 2015 5:12 pm

I was able to fix errors using

Code: Select all

	$(CHAIN)g++ -T $(MMAP) $(OBJLIST) -Wl,-L$(LIBPATH) $(LIBS) -o $(OBJ)main.elf
for linking. g++ automatically includes the CRT files, which ld did not.

I also compile with -mfloat-abi=soft instead of hard (?)

I can now use ostream cout and string, i think that should prove stdc++ is now working...

anyone know why having ostream added ~450KB of executable size? I knew it would weight something, but that's huge...

Thanks,

Allexoll

kriss
Posts: 66
Joined: Thu Apr 02, 2015 8:53 pm
Location: france for now ...

Re: [SOLVED] getting libstdc++ to work

Fri May 01, 2015 6:48 pm

nice that you found
i know that if you remove math und/overflow checking the size decrease
else "security" check should do same
if your target support hard-float go for it too

allexoll
Posts: 43
Joined: Sat Feb 23, 2013 12:27 pm

Re: [SOLVED] getting libstdc++ to work

Fri May 01, 2015 8:34 pm

It does support hard float (NEON coprocessor), but if I try to compile with hard-fp, g++ throws me the old "X uses VFP arguments, but main.elf does not". I'll be looking into that soon. I don't have extensive float operation in my kernel ATM... how would you remove undeflow/overflow checking? I've never heard of that

Cheers,
Allexoll

kriss
Posts: 66
Joined: Thu Apr 02, 2015 8:53 pm
Location: france for now ...

Re: [SOLVED] getting libstdc++ to work

Sat May 02, 2015 9:47 am

google for "g++ reduce executable size" ;)

Return to “Bare metal, Assembly language”