Undefined references to C++ functions (stdc++)

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
kazade
Insane DCEmu
Insane DCEmu
Posts: 145
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Tue May 02, 2017 3:11 pm
Has thanked: 3 times
Been thanked: 34 times

Undefined references to C++ functions (stdc++)

Post by kazade »

I've finally managed to compile my library into a static library for the Dreamcast, however when I try to use it I get undefined references from all the C++ functions. I've tried to manually link stdc++ but it doesn't seem to make any difference, here's the link line (generated by CMake):

Code: Select all

/opt/toolchains/dc/kos/utils/gnu_wrappers/kos-c++    -std=c++11 -g   CMakeFiles/simulant_tests.dir/global.cpp.obj CMakeFiles/simulant_tests.dir/main.cpp.obj  -o simulant_tests.elf  -L/opt/toolchains/dc/kos/lib/dreamcast  -L/opt/toolchains/dc/kos/addons/lib/dreamcast  -L/opt/toolchains/dc/kos-ports/lib  -L/opt/toolchains/dc/sh-elf/lib/gcc/sh-elf/4.7.3  -L/opt/toolchains/dc/sh-elf/sh-elf/lib -lm -lstdc++ -lc -lm -lstdc++ -lc ../simulant/libsimulant.a -lm -lstdc++ -lc -lm -lstdc++ -lc -lm -lstdc++ -lc -lkallisti -lc
Can anyone see what I'm doing wrong?

Thanks!
kazade
Insane DCEmu
Insane DCEmu
Posts: 145
Joined: Tue May 02, 2017 3:11 pm
Has thanked: 3 times
Been thanked: 34 times

Re: Undefined references to C++ functions (stdc++)

Post by kazade »

So I've tried to force CMake to use kos-ld as the linker, and now I get the following error:

Code: Select all

[ 86%] Linking CXX executable simulant_tests.elf
cd /simulant/dbuild/tests && /usr/bin/cmake -E cmake_link_script CMakeFiles/simulant_tests.dir/link.txt --verbose=1
kos-ld
/opt/toolchains/dc/sh-elf/bin/sh-elf-ld: unrecognised emulation mode: 4-single-only
Supported emulations: shelf shlelf sh shl
Any ideas?
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5652
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: Undefined references to C++ functions (stdc++)

Post by BlueCrab »

You should definitely be linking with sh-elf-g++ or kos-c++. KOS' built-in "linker" flags, assume that. That, and it's rather annoying to even try to get sh-elf-ld to do the right thing with libstdc++ anyway.

As for your problem, you're sure whatever functions that are coming up as undefined are actually available in the version of libstdc++ in use?
kazade
Insane DCEmu
Insane DCEmu
Posts: 145
Joined: Tue May 02, 2017 3:11 pm
Has thanked: 3 times
Been thanked: 34 times

Re: Undefined references to C++ functions (stdc++)

Post by kazade »

Um, well the stuff seems to be fundamental things (like string, new + delete), here's a snippet:

Code: Select all

/opt/toolchains/dc/sh-elf/lib/gcc/sh-elf/4.7.3/../../../../sh-elf/include/c++/4.7.3/ext/new_allocator.h:100: undefined reference to `operator delete(void*)'
CMakeFiles/simulant_tests.dir/global.cpp.obj: In function `_ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE1EE10_M_releaseEv':
/opt/toolchains/dc/sh-elf/lib/gcc/sh-elf/4.7.3/../../../../sh-elf/include/c++/4.7.3/bits/shared_ptr_base.h:164: undefined reference to `__gnu_cxx::__exchange_and_add(int volatile*, int)'
CMakeFiles/simulant_tests.dir/global.cpp.obj: In function `_ZN9__gnu_cxx13new_allocatorIlE10deallocateEPlj':
/opt/toolchains/dc/sh-elf/lib/gcc/sh-elf/4.7.3/../../../../sh-elf/include/c++/4.7.3/ext/new_allocator.h:100: undefined reference to `std::ios_base::Init::Init()'
/opt/toolchains/dc/sh-elf/lib/gcc/sh-elf/4.7.3/../../../../sh-elf/include/c++/4.7.3/ext/new_allocator.h:100: undefined reference to `std::ios_base::Init::~Init()'
/opt/toolchains/dc/sh-elf/lib/gcc/sh-elf/4.7.3/../../../../sh-elf/include/c++/4.7.3/ext/new_allocator.h:100: undefined reference to `operator new(unsigned int)'
/opt/toolchains/dc/sh-elf/lib/gcc/sh-elf/4.7.3/../../../../sh-elf/include/c++/4.7.3/ext/new_allocator.h:100: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
/opt/toolchains/dc/sh-elf/lib/gcc/sh-elf/4.7.3/../../../../sh-elf/include/c++/4.7.3/ext/new_allocator.h:100: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/opt/toolchains/dc/sh-elf/lib/gcc/sh-elf/4.7.3/../../../../sh-elf/include/c++/4.7.3/ext/new_allocator.h:100: undefined reference to `operator delete(void*)'
It goes on for 10s of 1000s of lines - although looking at it, the undefined references seem to center around:

- new + delete
- exceptions and exception handling things (___cxa_begin_throw etc.)
- basic_string
- exchange_and_add

Surely it wouldn't even compile if this stuff was missing from the library... would it?
kazade
Insane DCEmu
Insane DCEmu
Posts: 145
Joined: Tue May 02, 2017 3:11 pm
Has thanked: 3 times
Been thanked: 34 times

Re: Undefined references to C++ functions (stdc++)

Post by kazade »

OK, I think I've found the problem - this is the output of the `nm` command on libstdc++.a

Code: Select all

compatibility.o:
00000000 n _ZNSt11char_traitsIcE11eq_int_typeERKiS2_
00000000 n _ZNSt11char_traitsIcE3eofEv
00000000 n _ZNSt11char_traitsIwE11eq_int_typeERKjS2_
00000000 n _ZNSt11char_traitsIwE3eofEv
00000000 n _ZSt3minIiERKT_S2_S2_
00000000 n _ZStoRRSt12_Ios_IostateS_
00000000 n _ZStorSt12_Ios_IostateS_
         U __Unwind_Resume
00000000 t __ZL20__gthread_key_deletei
         U __ZNKSi6sentrycvbEv
         U __ZNKSt13basic_istreamIwSt11char_traitsIwEE6sentrycvbEv
         U __ZNKSt15basic_streambufIcSt11char_traitsIcEE4gptrEv
         U __ZNKSt15basic_streambufIcSt11char_traitsIcEE5egptrEv
         U __ZNKSt15basic_streambufIwSt11char_traitsIwEE4gptrEv
         U __ZNKSt15basic_streambufIwSt11char_traitsIwEE5egptrEv
         U __ZNKSt9basic_iosIcSt11char_traitsIcEE5rdbufEv
         U __ZNKSt9basic_iosIwSt11char_traitsIwEE5rdbufEv
00000000 T __ZNSi6ignoreEi
         U __ZNSi6ignoreEv
         U __ZNSi6sentryC1ERSib
00000000 W __ZNSt11char_traitsIcE11eq_int_typeERKiS2_
00000000 W __ZNSt11char_traitsIcE3eofEv
00000000 W __ZNSt11char_traitsIwE11eq_int_typeERKjS2_
00000000 W __ZNSt11char_traitsIwE3eofEv
00000000 T __ZNSt13basic_istreamIwSt11char_traitsIwEE6ignoreEi
         U __ZNSt13basic_istreamIwSt11char_traitsIwEE6ignoreEv
         U __ZNSt13basic_istreamIwSt11char_traitsIwEE6sentryC1ERS2_b
         U __ZNSt15basic_streambufIcSt11char_traitsIcEE12__safe_gbumpEi
         U __ZNSt15basic_streambufIcSt11char_traitsIcEE5sgetcEv
         U __ZNSt15basic_streambufIcSt11char_traitsIcEE6snextcEv
         U __ZNSt15basic_streambufIwSt11char_traitsIwEE12__safe_gbumpEi
         U __ZNSt15basic_streambufIwSt11char_traitsIwEE5sgetcEv
         U __ZNSt15basic_streambufIwSt11char_traitsIwEE6snextcEv
         U __ZNSt9basic_iosIcSt11char_traitsIcEE11_M_setstateESt12_Ios_Iostate
         U __ZNSt9basic_iosIcSt11char_traitsIcEE8setstateESt12_Ios_Iostate
         U __ZNSt9basic_iosIwSt11char_traitsIwEE11_M_setstateESt12_Ios_Iostate
         U __ZNSt9basic_iosIwSt11char_traitsIwEE8setstateESt12_Ios_Iostate
00000000 W __ZSt3minIiERKT_S2_S2_
00000000 W __ZStoRRSt12_Ios_IostateS_
00000000 W __ZStorSt12_Ios_IostateS_
         U __ZTIN10__cxxabiv115__forced_unwindE
         U ___cxa_begin_catch
         U ___cxa_end_catch
         U ___cxa_rethrow
         U ___gxx_personality_v0
         U _kthread_key_delete

compatibility-debug_list.o:
00000000 n _ZSt4swapIPNSt6__norm15_List_node_baseEEvRT_S4_
00000000 T __ZNSt6__norm15_List_node_base4hookEPS0_
00000000 T __ZNSt6__norm15_List_node_base4swapERS0_S1_
00000000 T __ZNSt6__norm15_List_node_base6unhookEv
00000000 T __ZNSt6__norm15_List_node_base7reverseEv
00000000 T __ZNSt6__norm15_List_node_base8transferEPS0_S1_
00000000 W __ZSt4swapIPNSt6__norm15_List_node_baseEEvRT_S4_

compatibility-debug_list-2.o:

compatibility-list.o:
00000000 n _ZSt4swapIPSt15_List_node_baseEvRT_S3_
00000000 T __ZNSt15_List_node_base4hookEPS_
00000000 T __ZNSt15_List_node_base4swapERS_S0_
00000000 T __ZNSt15_List_node_base6unhookEv
00000000 T __ZNSt15_List_node_base7reverseEv
00000000 T __ZNSt15_List_node_base8transferEPS_S0_
00000000 W __ZSt4swapIPSt15_List_node_baseEvRT_S3_

compatibility-list-2.o:

compatibility-c++0x.o:
00000000 t __ZL20__gthread_key_deletei
00000000 r __ZStL19piecewise_construct
         U _kthread_key_delete

compatibility-atomic-c++0x.o:
00000000 t __ZL20__gthread_key_deletei
00000000 r __ZStL10adopt_lock
00000000 r __ZStL10defer_lock
00000000 r __ZStL11try_to_lock
00000000 r __ZStL13allocator_arg
00000000 r __ZStL19piecewise_construct
00000000 r __ZStL6ignore
         U _kthread_key_delete

compatibility-thread-c++0x.o:
00000000 t __ZL20__gthread_key_deletei
00000000 r __ZN9__gnu_cxxL21__default_lock_policyE
00000000 r __ZStL10adopt_lock
00000000 r __ZStL10defer_lock
00000000 r __ZStL11try_to_lock
00000000 r __ZStL13allocator_arg
00000000 r __ZStL19piecewise_construct
00000000 r __ZStL6ignore
         U _kthread_key_delete
That's all it outputs... surely there should be more than that? Did I do something wrong when compiling the toolchain?
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5652
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: Undefined references to C++ functions (stdc++)

Post by BlueCrab »

That is... Most certainly very wrong. There should be a LOT more in the output from nm.

There's definitely something wrong with your toolchain if that's all it spits out.

Even for what you do have, it looks quite a bit different than what I have for those files:

Code: Select all

compatibility.o:
         U __Unwind_Resume
00000000 T __ZNSi6ignoreEi
         U __ZNSi6ignoreEv
         U __ZNSi6sentryC1ERSib
00000000 T __ZNSt13basic_istreamIwSt11char_traitsIwEE6ignoreEi
         U __ZNSt13basic_istreamIwSt11char_traitsIwEE6ignoreEv
         U __ZNSt13basic_istreamIwSt11char_traitsIwEE6sentryC1ERS2_b
00000000 t __ZNSt15basic_streambufIcSt11char_traitsIcEE5sgetcEv.part.1
00000000 t __ZNSt15basic_streambufIwSt11char_traitsIwEE5sgetcEv.part.4
         U __ZNSt9basic_iosIcSt11char_traitsIcEE11_M_setstateESt12_Ios_Iostate
         U __ZNSt9basic_iosIcSt11char_traitsIcEE5clearESt12_Ios_Iostate
         U __ZNSt9basic_iosIwSt11char_traitsIwEE11_M_setstateESt12_Ios_Iostate
         U __ZNSt9basic_iosIwSt11char_traitsIwEE5clearESt12_Ios_Iostate
         U __ZTIN10__cxxabiv115__forced_unwindE
         U ___cxa_begin_catch
         U ___cxa_end_catch
         U ___cxa_rethrow
         U ___gxx_personality_v0

compatibility-debug_list.o:
00000000 T __ZNSt6__norm15_List_node_base4hookEPS0_
00000000 T __ZNSt6__norm15_List_node_base4swapERS0_S1_
00000000 T __ZNSt6__norm15_List_node_base6unhookEv
00000000 T __ZNSt6__norm15_List_node_base7reverseEv
00000000 T __ZNSt6__norm15_List_node_base8transferEPS0_S1_

compatibility-debug_list-2.o:

compatibility-list.o:
00000000 T __ZNSt15_List_node_base4hookEPS_
00000000 T __ZNSt15_List_node_base4swapERS_S0_
00000000 T __ZNSt15_List_node_base6unhookEv
00000000 T __ZNSt15_List_node_base7reverseEv
00000000 T __ZNSt15_List_node_base8transferEPS_S0_

compatibility-list-2.o:

compatibility-c++0x.o:

compatibility-atomic-c++0x.o:

compatibility-thread-c++0x.o:
kazade
Insane DCEmu
Insane DCEmu
Posts: 145
Joined: Tue May 02, 2017 3:11 pm
Has thanked: 3 times
Been thanked: 34 times

Re: Undefined references to C++ functions (stdc++)

Post by kazade »

I'm not sure what I could've done wrong... everything is based on the template set up. In fact, this was built as a docker image so I can show exactly the steps I took to build the toolchain:

Code: Select all

# Dockerfile for generating a Fedora image with the full KallistiOS SDK installed so you can compiled
# Dreamcast applications

FROM fedora
MAINTAINER kazade <kazade@gmail.com>

# RUN dnf -y update && dnf clean all
RUN dnf -y install git bzip2 patch gcc texinfo libjpeg-turbo-devel libpng-devel python && dnf clean all
RUN mkdir -p /opt/toolchains/dc
RUN git clone git://git.code.sf.net/p/cadcdev/kallistios /opt/toolchains/dc/kos
RUN git clone --recursive git://git.code.sf.net/p/cadcdev/kos-ports /opt/toolchains/dc/kos-ports
RUN cd /opt/toolchains/dc/kos/utils/dc-chain && sh ./download.sh && sh ./unpack.sh && make
RUN cp /opt/toolchains/dc/kos/doc/environ.sh.sample /opt/toolchains/dc/kos/environ.sh
RUN echo -e "\nsource /opt/toolchains/dc/kos/environ.sh" >> /etc/bash.bashrc
RUN source /etc/bash.bashrc; echo ${KOS_BASE}
RUN source /etc/bash.bashrc; cd /opt/toolchains/dc/kos && make
RUN source /etc/bash.bashrc; cd /opt/toolchains/dc/kos-ports/libGL && make install clean
RUN source /etc/bash.bashrc; cd /opt/toolchains/dc/kos-ports/libjpeg && make install clean
RUN source /etc/bash.bashrc; cd /opt/toolchains/dc/kos-ports/libpng && make install clean
RUN dnf -y install cmake
RUN sed -i 's/-fno-rtti//' /opt/toolchains/dc/kos/environ_base.sh
RUN sed -i 's/-fno-exceptions//' /opt/toolchains/dc/kos/environ_base.sh
RUN sed -i 's/-fno-operator-names//' /opt/toolchains/dc/kos/environ_base.sh
Anything there stand out as being wrong?
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5652
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: Undefined references to C++ functions (stdc++)

Post by BlueCrab »

Nothing there stands out as being wrong in any way that would cause that. I mean, you don't need --recursive on the kos-ports git clone, but that's not actually going to cause problems (and certainly not of that kind).
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 5 times

Re: Undefined references to C++ functions (stdc++)

Post by nymus »

Once when I experienced similar issues using a successfully compiled tool chain, it turned out to be related to the binutils used during compilation. There just seemed to be a discrepancy between what gcc and binutils wanted as far as directory structure goes.

I don't know if this is the issue you have, but it might help to check the preferred version of binutils, even if you might want a newer one. My last tool chain build used binutils-2.23.2.

I also used to compile gcc once for a while without issue but went back to the normal two-stage compile after kos integrated its libc and libstdc++ with gcc. Are you working in this area? I.e. Patching gcc?
behold the mind
inspired by Dreamcast
kazade
Insane DCEmu
Insane DCEmu
Posts: 145
Joined: Tue May 02, 2017 3:11 pm
Has thanked: 3 times
Been thanked: 34 times

Re: Undefined references to C++ functions (stdc++)

Post by kazade »

No, I'm just building the standard toolchain - but I have come up with a theory! I'm wondering if latest GCC (shipped with latest Fedora) doesn't build the old GCC + stdlib correctly. I tested bumping GCC to 4.8.2 and compilation failed because the newest host GCC doesn't like it (I found a bug report for this but it's only fixed in GCC 5.x not 4.x).

So I've changed the Docker image to source from Fedora 23 rather than latest, and I'm re-compiling 4.7.3 now, and already the output is much cleaner (before there were a *lot* of warnings)

I'll update the thread when I've tested the output.
kazade
Insane DCEmu
Insane DCEmu
Posts: 145
Joined: Tue May 02, 2017 3:11 pm
Has thanked: 3 times
Been thanked: 34 times

Re: Undefined references to C++ functions (stdc++)

Post by kazade »

OK, still the same problem - although the nm output has changed:

Code: Select all

compatibility.o:
         U __Unwind_Resume
00000000 T __ZNSi6ignoreEi
         U __ZNSi6ignoreEv
         U __ZNSi6sentryC1ERSib
00000000 T __ZNSt13basic_istreamIwSt11char_traitsIwEE6ignoreEi
         U __ZNSt13basic_istreamIwSt11char_traitsIwEE6ignoreEv
         U __ZNSt13basic_istreamIwSt11char_traitsIwEE6sentryC1ERS2_b
00000000 t __ZNSt15basic_streambufIcSt11char_traitsIcEE5sgetcEv.part.1
00000000 t __ZNSt15basic_streambufIwSt11char_traitsIwEE5sgetcEv.part.4
         U __ZNSt9basic_iosIcSt11char_traitsIcEE11_M_setstateESt12_Ios_Iostate
         U __ZNSt9basic_iosIcSt11char_traitsIcEE5clearESt12_Ios_Iostate
         U __ZNSt9basic_iosIwSt11char_traitsIwEE11_M_setstateESt12_Ios_Iostate
         U __ZNSt9basic_iosIwSt11char_traitsIwEE5clearESt12_Ios_Iostate
         U __ZTIN10__cxxabiv115__forced_unwindE
         U ___cxa_begin_catch
         U ___cxa_end_catch
         U ___cxa_rethrow
         U ___gxx_personality_v0

compatibility-debug_list.o:
00000000 T __ZNSt6__norm15_List_node_base4hookEPS0_
00000000 T __ZNSt6__norm15_List_node_base4swapERS0_S1_
00000000 T __ZNSt6__norm15_List_node_base6unhookEv
00000000 T __ZNSt6__norm15_List_node_base7reverseEv
00000000 T __ZNSt6__norm15_List_node_base8transferEPS0_S1_

compatibility-debug_list-2.o:

compatibility-list.o:
00000000 T __ZNSt15_List_node_base4hookEPS_
00000000 T __ZNSt15_List_node_base4swapERS_S0_
00000000 T __ZNSt15_List_node_base6unhookEv
00000000 T __ZNSt15_List_node_base7reverseEv
00000000 T __ZNSt15_List_node_base8transferEPS_S0_

compatibility-list-2.o:

compatibility-c++0x.o:

compatibility-atomic-c++0x.o:

compatibility-thread-c++0x.o:
Does anyone have any other ideas? I'm running out of options...
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 5 times

Re: Undefined references to C++ functions (stdc++)

Post by nymus »

It seems you are using CMake instead of the standard make for your project and rewriting the compile flags... Does it matter for your project that you don't have the -nodefaultlibs -nostdlib -nostartfiles flags?

I also just noticed that in the environ_base.sh kos is set to compile (itself?) with -std=gnu++98. Would this be an issue for how you are working?

It's also weird that one of the errors you got mention unknown emulation -4-single-only. Maybe it could help to describe how you are compiling your library?

e.g.

- have a working copy of gcc (kos-patched) from the toolchain that your are modifying
- running dc-chain script using modified gcc

or other method...
behold the mind
inspired by Dreamcast
kazade
Insane DCEmu
Insane DCEmu
Posts: 145
Joined: Tue May 02, 2017 3:11 pm
Has thanked: 3 times
Been thanked: 34 times

Re: Undefined references to C++ functions (stdc++)

Post by kazade »

I'm using CMake for my library and application - but that obviously shouldn't affect the build of libstdc++

I don't suppose someone can attach their version of libstdc++.a so I can compare the files? Presumably as they are built with the same toolchain they should be the same (right?).

Thanks
kazade
Insane DCEmu
Insane DCEmu
Posts: 145
Joined: Tue May 02, 2017 3:11 pm
Has thanked: 3 times
Been thanked: 34 times

Re: Undefined references to C++ functions (stdc++)

Post by kazade »

Success!

As a last resort I rolled back the Docker image to Fedora 21 (which ships with GCC 4.9.x) previously I'd tried latest (GCC 6.x) and Fedora 23 (GCC 5.x) It appears there is a bug somewhere when compiling with newer GCC.

I still have a few undefined references but those are my fault or due to incomplete C++11 in 4.7.3 (std::to_string isn't implemented)
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: Undefined references to C++ functions (stdc++)

Post by bogglez »

I only know that GCC 6 worked for me for C, didn't test C++.
Just a shot in the dark, but perhaps you can disable some newer C++ library features with command line flags? I'd read the changelog
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
Post Reply