Trying to use <string>

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

oszfather
Posts: 11
Joined: Tue Jun 14, 2022 5:27 pm
Has thanked: 2 times

Trying to use <string>

Postby oszfather » Fri Dec 30, 2022 5:16 pm

my 'main' source file is a cpp file. I've been using this fine with chibios along with some cpp libraries for some time (the makefile knows theyre cpp). I want to perform some string manipulation and so need the string library so I import the following:

Code: Select all

#include <string.h>
#include <stdlib.h>
#include <iostream>

using namespace std;


and make use of the string data type and strncmp function:

Code: Select all

 
string test = "blah";

if(strncmp("GT",(char*)buffer,2) == 0){
      cmd = 1;


Upon building I get the following errors:

Code: Select all

Linking build/ch.elf
lto-wrapper.exe: warning: Options to Xassembler do not match: -alms=./build/lst/crt1.lst, -alms=./build/lst/hal.lst, dropping all -Xassembler and -Wa options.
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\zinvi\AppData\Local\Temp\ch.elf.WOywVb.ltrans0.ltrans.o: in function `main':
c:\chibistudio\tools\gnu tools arm embedded\10.3 2021.07\arm-none-eabi\include\c++\10.3.1\bits/basic_string.tcc:225: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy_chars(char*, char const*, char const*)'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\zinvi\AppData\Local\Temp\ch.elf.WOywVb.ltrans0.ltrans.o: in function `_GLOBAL__sub_I__Z15ApplicationInitv':
c:\chibistudio\tools\gnu tools arm embedded\10.3 2021.07\arm-none-eabi\include\c++\10.3.1/iostream:74: undefined reference to `std::ios_base::Init::Init()'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\zinvi\AppData\Local\Temp\ch.elf.WOywVb.ltrans0.ltrans.o:c:\chibistudio\tools\gnu tools arm embedded\10.3 2021.07\arm-none-eabi\include\c++\10.3.1/iostream:74: undefined reference to `__dso_handle'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\zinvi\AppData\Local\Temp\ch.elf.WOywVb.ltrans0.ltrans.o:c:\chibistudio\tools\gnu tools arm embedded\10.3 2021.07\arm-none-eabi\include\c++\10.3.1/iostream:74: undefined reference to `std::ios_base::Init::~Init()'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-fini.o): in function `__libc_fini_array':
fini.c:(.text.__libc_fini_array+0x20): undefined reference to `_fini'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-abort.o): in function `abort':
abort.c:(.text.abort+0xa): undefined reference to `_exit'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-signalr.o): in function `_kill_r':
signalr.c:(.text._kill_r+0x12): undefined reference to `_kill'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-signalr.o): in function `_getpid_r':
signalr.c:(.text._getpid_r+0x0): undefined reference to `_getpid'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: build/ch.elf: hidden symbol `__dso_handle' isn't defined
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: final link failed: bad value
collect2.exe: error: ld returned 1 exit status
make: *** [../../../os/common/startup/ARMCMx/compilers/GCC/mk/rules.mk:217: build/ch.elf] Error 1
"make -j8 all" terminated with exit code 2. Build might be incomplete.

16:07:13 Build Failed. 17 errors, 5 warnings. (took 3s.187ms)


I tried adding the path 'C:\ChibiStudio\tools\GNU Tools ARM Embedded\10.3 2021.07' in both the C and C++ CDT user setting entries in the project preprocesser include paths (in project properties). This didnt fix anything. I'm not exactly an expert on the C build process and toolchain details, why wont these libraries be imported/linked correctly and how do I fix this?

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Trying to use <string>

Postby Giovanni » Fri Dec 30, 2022 5:33 pm

Hi,

Default makefiles link using gcc not g++ in order to save some space.

You may try to add:

LD = $(TRGT)g++

to your makefile soon after the inclusion of arm-none-eabi.mk in order to override this.

Giovanni

oszfather
Posts: 11
Joined: Tue Jun 14, 2022 5:27 pm
Has thanked: 2 times

Re: Trying to use <string>

Postby oszfather » Fri Dec 30, 2022 7:05 pm

Giovanni wrote:Hi,

Default makefiles link using gcc not g++ in order to save some space.

You may try to add:

LD = $(TRGT)g++

to your makefile soon after the inclusion of arm-none-eabi.mk in order to override this.

Giovanni



I added that line like so:

Code: Select all

# Common rules
#

RULESPATH = $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk
include $(RULESPATH)/arm-none-eabi.mk
include $(RULESPATH)/rules.mk

#
# Common rules
##############################################################################

##############################################################################
# Custom rules
LD = $(TRGT)g++
##############################################################################


But gave me almost double the errors:

Code: Select all

Linking build/ch.elf
lto-wrapper.exe: warning: Options to Xassembler do not match: -alms=./build/lst/crt1.lst, -alms=./build/lst/hal.lst, dropping all -Xassembler and -Wa options.
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\zinvi\AppData\Local\Temp\ch.elf.34hhZP.ltrans0.ltrans.o: in function `_GLOBAL__sub_I__Z15ApplicationInitv':
c:\chibistudio\tools\gnu tools arm embedded\10.3 2021.07\arm-none-eabi\include\c++\10.3.1/iostream:74: undefined reference to `__dso_handle'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libstdc++.a(system_error.o): in function `_GLOBAL__sub_I__ZSt20__throw_system_errori':
system_error.cc:(.text.startup._GLOBAL__sub_I__ZSt20__throw_system_errori+0x1c): undefined reference to `__dso_handle'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libstdc++.a(cxx11-ios_failure.o): in function `(anonymous namespace)::__io_category_instance()':
cxx11-ios_failure.cc:(.text._ZN12_GLOBAL__N_122__io_category_instanceEv+0x38): undefined reference to `__dso_handle'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-abort.o): in function `abort':
abort.c:(.text.abort+0xa): undefined reference to `_exit'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-fstatr.o): in function `_fstat_r':
fstatr.c:(.text._fstat_r+0x12): undefined reference to `_fstat'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-sbrkr.o): in function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-signalr.o): in function `_kill_r':
signalr.c:(.text._kill_r+0x12): undefined reference to `_kill'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-signalr.o): in function `_getpid_r':
signalr.c:(.text._getpid_r+0x0): undefined reference to `_getpid'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-writer.o): in function `_write_r':
writer.c:(.text._write_r+0x14): undefined reference to `_write'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-closer.o): in function `_close_r':
closer.c:(.text._close_r+0xc): undefined reference to `_close'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-fini.o): in function `__libc_fini_array':
fini.c:(.text.__libc_fini_array+0x20): undefined reference to `_fini'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-isattyr.o): in function `_isatty_r':
isattyr.c:(.text._isatty_r+0xc): undefined reference to `_isatty'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-lseekr.o): in function `_lseek_r':
lseekr.c:(.text._lseek_r+0x14): undefined reference to `_lseek'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-readr.o): in function `_read_r':
readr.c:(.text._read_r+0x14): undefined reference to `_read'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: build/ch.elf: hidden symbol `__dso_handle' isn't defined
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: final link failed: bad value
collect2.exe: error: ld returned 1 exit status
make: *** [../../../os/common/startup/ARMCMx/compilers/GCC/mk/rules.mk:217: build/ch.elf] Error 1
"make -j8 all" terminated with exit code 2. Build might be incomplete.

18:04:20 Build Failed. 31 errors, 6 warnings. (took 5s.398ms)



Did i add it incorrectly (I realised TRGT isnt an actual path but what should be there instead)?
how i do get the project to compile with g++ and not gcc.

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Trying to use <string>

Postby Giovanni » Fri Dec 30, 2022 7:22 pm

Hi,

Place it this way:

Code: Select all

include $(RULESPATH)/arm-none-eabi.mk
LD = $(TRGT)g++
include $(RULESPATH)/rules.mk


It is rules.mk that uses that variable.

Giovanni

oszfather
Posts: 11
Joined: Tue Jun 14, 2022 5:27 pm
Has thanked: 2 times

Re: Trying to use <string>

Postby oszfather » Fri Dec 30, 2022 7:24 pm

Giovanni wrote:Hi,

Place it this way:

Code: Select all

include $(RULESPATH)/arm-none-eabi.mk
LD = $(TRGT)g++
include $(RULESPATH)/rules.mk


It is rules.mk that uses that variable.

Giovanni


Hi, i've added it this way but it still gives me those 31 errors in my last reply.

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Trying to use <string>

Postby Giovanni » Fri Dec 30, 2022 7:29 pm

Hi,

Are you including /os/various/syscalls.c in your project? it is where those symbols are defined.

Giovanni

oszfather
Posts: 11
Joined: Tue Jun 14, 2022 5:27 pm
Has thanked: 2 times

Re: Trying to use <string>

Postby oszfather » Fri Dec 30, 2022 7:44 pm

Giovanni wrote:Hi,

Are you including /os/various/syscalls.c in your project? it is where those symbols are defined.

Giovanni


I couldnt see it anywhere so I've added it like so:

Code: Select all

CSRC = $(ALLCSRC) \
       $(TESTSRC) \
       $(CHIBIOS)/os/various/syscalls.c


I'm getting 11 errors now

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Trying to use <string>

Postby Giovanni » Fri Dec 30, 2022 8:20 pm

Hi,

The OS includes a limited support for the newlib, apparently you are using library functions that rely on unimplemented system calls.

You need to provide a stub implementation for those undefined symbols like syscalls.c does.

More information is available here: http://sourceware.org/newlib/

Giovanni

oszfather
Posts: 11
Joined: Tue Jun 14, 2022 5:27 pm
Has thanked: 2 times

Re: Trying to use <string>

Postby oszfather » Fri Dec 30, 2022 11:02 pm

Giovanni wrote:Hi,

The OS includes a limited support for the newlib, apparently you are using library functions that rely on unimplemented system calls.

You need to provide a stub implementation for those undefined symbols like syscalls.c does.

More information is available here: http://sourceware.org/newlib/

Giovanni


Thanks for the advice thus far and to clear things up for my understanding. You're recommending I use an alternate to library like 'newlib' in the link you sent to act as a substitute for the string.h and stdlib.h etc. libraries. because these libraries rely on 'unimplemented system calls'? Is there anyway to use these original libraries as also I soon want to add libraries like 'Eigen' for C++ and do not want to run into similar linking issues?

Are there any docs on how ChibiOS build process works so I can improve my understanding of the entire thing?

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Trying to use <string>

Postby Giovanni » Sat Dec 31, 2022 7:35 am

What version of GCC compiler are you using? it is already using newlib, my suggestion is to provide those unimplemented functions in your own project in order to silence those linker errors, the linker I sent is the documentation for those functions.

The build process is nothing peculiar, it is enclosed in that rules.mk file and it is shared among all projects. You can enable the "verbose" option in your makefile and you will see exactly all commands during build including the final link phase.

BTW, we have a discord server, you may try to ask in there too: https://discord.gg/s8b8v7VtrM

Giovanni


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 11 guests