Problems compiling C++

Asked by ilovefpv

Hi - I'm mostly using C, but I would like to do some C++... I tried just changing the extension of the file from .c (which compiles OK) to .cpp and I get:

C:\project\ezarm\src>arm-none-eabi-gcc c:\project\ezarm\src\thermometer.cpp c
roject\ezarm\lib\startup\startup_ARMCM0.S -mthumb -mcpu=cortex-m0 -D__STARTUP
EAR_BSS -D__START=main -Os -ffunction-sections -fdata-sections -g --specs=nan
pecs -lc -lc -lnosys -Ic:\project\ezarm\lib -Lc:\project\ezarm\lib\ldscripts
c:\project\ezarm\lib\gcc.ld -Wl,--gc-sections -Wl,-Map=c:\project\ezarm\out\t
mometer.map -g -o c:\project\ezarm\out\thermometer-CM0.axf
c:/program files (x86)/gnu tools arm embedded/4.7 2013q2/bin/../lib/gcc/arm-n
-eabi/4.7.4/../../../../arm-none-eabi/lib/armv6-m\libc_s.a(lib_a-signalr.o):
function `_kill_r':
signalr.c:(.text._kill_r+0xe): undefined reference to `_kill'
c:/program files (x86)/gnu tools arm embedded/4.7 2013q2/bin/../lib/gcc/arm-n
-eabi/4.7.4/../../../../arm-none-eabi/lib/armv6-m\libc_s.a(lib_a-signalr.o):
function `_getpid_r':
signalr.c:(.text._getpid_r+0x2): undefined reference to `_getpid'
collect2.exe: error: ld returned 1 exit status

C:\project\ezarm\src>arm-none-eabi-size c:\project\ezarm\out\thermometer-CM0.

arm-none-eabi-size: 'c:\project\ezarm\out\thermometer-CM0.axf': No such file

Any ideas?

Question information

Language:
English Edit question
Status:
Answered
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Terry Guo (terry.guo) said :
#1

It seems you were using arm-none-eabi-gcc to compile c++ source files. Can you please try arm-none-eabi-g++?

Revision history for this message
ilovefpv (ilovefpv) said :
#2

Same results - can you please refer to the developer:

C:\project\ezarm\src>arm-none-eabi-g++ c:\project\ezarm\src\thermometer.cpp c:
roject\ezarm\lib\startup\startup_ARMCM0.S -mthumb -mcpu=cortex-m0 -D__STARTUP_
EAR_BSS -D__START=main -Os -ffunction-sections -fdata-sections -g --specs=nano
pecs -lc -lc -lnosys -Ic:\project\ezarm\lib -Lc:\project\ezarm\lib\ldscripts -
c:\project\ezarm\lib\gcc.ld -Wl,--gc-sections -Wl,-Map=c:\project\ezarm\out\th
mometer.map -g -o c:\project\ezarm\out\thermometer-CM0.axf
c:/program files (x86)/gnu tools arm embedded/4.7 2013q2/bin/../lib/gcc/arm-no
-eabi/4.7.4/../../../../arm-none-eabi/lib/armv6-m\libc_s.a(lib_a-signalr.o): I
function `_kill_r':
signalr.c:(.text._kill_r+0xe): undefined reference to `_kill'
c:/program files (x86)/gnu tools arm embedded/4.7 2013q2/bin/../lib/gcc/arm-no
-eabi/4.7.4/../../../../arm-none-eabi/lib/armv6-m\libc_s.a(lib_a-signalr.o): I
function `_getpid_r':
signalr.c:(.text._getpid_r+0x2): undefined reference to `_getpid'
collect2.exe: error: ld returned 1 exit status

C:\project\ezarm\src>arm-none-eabi-size c:\project\ezarm\out\thermometer-CM0.a

arm-none-eabi-size: 'c:\project\ezarm\out\thermometer-CM0.axf': No such file

Revision history for this message
Terry Guo (terry.guo) said :
#3

The __getpid is defined in lib rdimon. Just add option -lrdimon right before -lnosys, the issue should be fixed.

Revision history for this message
ilovefpv (ilovefpv) said :
#4

That seems to work, but adds 4.4K to the code.

Is this really needed for C++ ? Or is there some way to eliminate it?

Revision history for this message
Terry Guo (terry.guo) said :
#5

I doubt this is really needed. Currently my colleagues are not in office. I will notify them and hope they can get back to you soon.

Revision history for this message
Joey Ye (jinyun-ye) said :
#6

libnosys also has these symbols. It seems to be caused by recursive symbol dependence.

Can you please try --start-group -lc -lc -lnosys --end-group to replace original "-lc -lc -lnosys"?

- Joey

Revision history for this message
ilovefpv (ilovefpv) said :
#7

Here's the result:

C:\project\ezarm\bin>arm-none-eabi-g++ c:\project\ezarm\src\thermometer.cpp c:\p
roject\ezarm\lib\startup\startup_ARMCM0.S -mthumb -mcpu=cortex-m0 -D__STARTUP_CL
EAR_BSS -D__START=main -Os -ffunction-sections -fdata-sections -g --specs=nano.s
pecs --start-group -lc -lc -lnosys --end-group -Ic:\project\ezarm\lib -Lc:\proje
ct\ezarm\lib\ldscripts -T c:\project\ezarm\lib\gcc.ld -Wl,--gc-sections -Wl,-Map
=c:\project\ezarm\out\thermometer.map -g -o c:\project\ezarm\out\thermometer-CM0
.axf
arm-none-eabi-g++: error: unrecognized command line option '--start-group'
arm-none-eabi-g++: error: unrecognized command line option '--end-group'

Revision history for this message
Joey Ye (jinyun-ye) said :
#8

Sorry! As compiler option it should be -Wl,--start-group -lc -lc -lnosys -Wl,--end-group

- Joey

Revision history for this message
ilovefpv (ilovefpv) said :
#9

That seems to go back to the original problem.. Is anybody using C++ with these tools?

C:\project\ezarm\src>arm-none-eabi-g++ c:\project\ezarm\src\thermometer.cpp c:\p
roject\ezarm\lib\startup\startup_ARMCM0.S -mthumb -mcpu=cortex-m0 -D__STARTUP_CL
EAR_BSS -D__START=main -Os -ffunction-sections -fdata-sections -g --specs=nano.s
pecs -Wl,--start-group -lc -lc -lnosys -Wl,--end-group -Ic:\project\ezarm\lib -L
c:\project\ezarm\lib\ldscripts -T c:\project\ezarm\lib\gcc.ld -Wl,--gc-sections
-Wl,-Map=c:\project\ezarm\out\thermometer.map -g -o c:\project\ezarm\out\thermom
eter-CM0.axf
c:/program files (x86)/gnu tools arm embedded/4.7 2013q2/bin/../lib/gcc/arm-none
-eabi/4.7.4/../../../../arm-none-eabi/lib/armv6-m\libc_s.a(lib_a-signalr.o): In
function `_kill_r':
signalr.c:(.text._kill_r+0xe): undefined reference to `_kill'
c:/program files (x86)/gnu tools arm embedded/4.7 2013q2/bin/../lib/gcc/arm-none
-eabi/4.7.4/../../../../arm-none-eabi/lib/armv6-m\libc_s.a(lib_a-signalr.o): In
function `_getpid_r':
signalr.c:(.text._getpid_r+0x2): undefined reference to `_getpid'
collect2.exe: error: ld returned 1 exit status

Revision history for this message
Joey Ye (jinyun-ye) said :
#10

Sorry that this annoying issue continue boths you. C++ examples in my hands works well. Also there are some people using C++ without problem.

There are symbols _kill and _getpid defined in libnosys.a. Usually the way to work it out are either
- repeatedly adding library name to end of linker command. Say adding more -lnosys to the end, or
- include ALL libraries between --start-group and --end-group. Say adding -lstdc++ -lgcc there

Disassembly of section .text._kill:

00000000 <_kill>:

Disassembly of section .text._getpid:

00000000 <_getpid>:

Please try the approach and come back if with further problem.

Thanks,
Joey

Revision history for this message
ilovefpv (ilovefpv) said :
#11

I see a similar issue in Answers: 220354 and 203480 although I am not clear what the root cause of this was in those issues.

Can you post an example of a working C++ build command? Mine is a derivative on the C command which worked - maybe I am reinventing the wheel here..

Revision history for this message
Joey Ye (jinyun-ye) said :
#12

Please take a look the example at share\gcc-arm-none-eabi\samples\src\cpp

Revision history for this message
dikobraz (user61a) said :
#13

Same problem, compiling with gcc works well, unresolved externals with g++. Any combination of link flags doesn't help.

/usr/bin/arm-none-eabi-g++ -lnosys -lc -fno-common -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -ffunction-sections -fdata-sections -MD -std=c++11 -Wsign-promo -Woverloaded-virtual -Os -DNDEBUG -nostartfiles -Wl,--gc-sections -T/home/dikobraz/Programming/manipulator/src/c/firmware/stm32f4/stm32f4-discovery.ld CMakeFiles/firmware.elf.dir/cdcacm.c.obj CMakeFiles/firmware.elf.dir/main.cpp.obj -o firmware.elf -L/home/dikobraz/Programming/manipulator/lib/cpp/libopencm3/lib /home/dikobraz/Programming/manipulator/lib/cpp/libopencm3/lib/libopencm3_stm32f4.a
/usr/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7e-m/fpu/libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'
/usr/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7e-m/fpu/libc.a(lib_a-abort.o): In function `abort':
abort.c:(.text.abort+0xa): undefined reference to `_exit'
/usr/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7e-m/fpu/libc.a(lib_a-signalr.o): In function `_kill_r':
signalr.c:(.text._kill_r+0xe): undefined reference to `_kill'
/usr/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7e-m/fpu/libc.a(lib_a-signalr.o): In function `_getpid_r':
signalr.c:(.text._getpid_r+0x0): undefined reference to `_getpid'
collect2: error: ld returned 1 exit status

Revision history for this message
dikobraz (user61a) said :
#14

Resolved by moving -lnosys to the end of linker command:

/usr/bin/arm-none-eabi-g++ -mthumb -mcpu=cortex-m4 -flto -ffunction-sections -fdata-sections -mfloat-abi=hard -mfpu=fpv4-sp-d16 -std=c++11 -Wsign-promo -Woverloaded-virtual -Os -DNDEBUG -nostartfiles -Wl,--gc-sections -T/home/dikobraz/Programming/manipulator/src/c/firmware/stm32f4/stm32f4-discovery.ld CMakeFiles/firmware.elf.dir/cdcacm.c.obj CMakeFiles/firmware.elf.dir/main.cpp.obj -o firmware.elf -L/home/dikobraz/Programming/manipulator/lib/cpp/libopencm3/lib /home/dikobraz/Programming/manipulator/lib/cpp/libopencm3/lib/libopencm3_stm32f4.a -lnosys

Revision history for this message
dikobraz (user61a) said :
#15

I was not right, it isn't -lnosys at the end, it is -flto. But it causes other mystic problems.

Revision history for this message
Kenny Koller (kenny-koller) said :
#16

Hi,

Is this a name mangling issue? I can stub these out with definitions that use extern "C" and I can link.

I used *-objdump to verify that libnosys.a contains the missing symbols. Added it to my link step and:

arm-none-eabi-g++ -o SBuild/Faircharge SBuild/Application/Main.o -lc -lnosys

abort.c:(.text.abort+0x10): undefined reference to `_exit'
signalr.c:(.text._kill_r+0x1c): undefined reference to `_kill'
signalr.c:(.text._getpid_r+0x4): undefined reference to `_getpid'
sbrkr.c:(.text._sbrk_r+0x18): undefined reference to `_sbrk'

I've seen this problem posted many times but have yet to see an actual solution.

Revision history for this message
Yihui (xiongyihui) said :
#17

I got the problem solved with the options Joey mentioned

      -Wl,--start-group -lc -lc -lnosys -Wl,--end-group

Note: "-lc" must be added twice.

Can you help with this problem?

Provide an answer of your own, or ask ilovefpv for more information if necessary.

To post a message you must log in.