97

I'm new to the field of microcontrollers. I need to port an IDE which is compiling programs for AVR microcontrollers to compile programs for ARM microcontrollers. I have just replaced the compiler from AVR to ARM and added some options as told by someone. The following is the command:

\ARM-GCC\bin\arm-none-eabi-gcc -O0 -ffunction-sections -Wall -std=gnu99 -mfloat-abi=soft 
-Wa,-adhlns="$@.lst" -fmessage-length=0 -mcpu=cortex-m0 -mthumb -g3 -gdwarf-2 -Wl,
--gc-sections -o <Dir_name>\Build_Files\Blink_arm.cpp.elf  <Dir_name>\Build_Files\Blink_arm.cpp.o <Dir_name>\Build_Files\core.a 
-L<Dir_name>\Build_Files -lm 

When I execute it I get the follwing error:

tools/arm-gcc/bin/../lib/gcc/arm-none-eabi/4.6.2\libc.a(lib_a-exit.o): In function `exit':
exit.c:(.text+0x18): undefined reference to `_exit'  
collect2: ld returned 1 exit status

May I get some help on what is this error and how can I solve it. And also I don't know what those options in the command line specify.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Sharath U
  • 971
  • 1
  • 6
  • 4
  • 4
    in your ARM libc (that is bundled with your compiler), the `exit()` function makes a call to `_exit()`, which isn't defined in any library. your compiler must be set up incorrectly, although you could perform a quick fix by defining `void _exit(int status)` yourself (NOTE: this won't work is your libc declares _exit() with a different signature. your `_exit()` could simply halt in an infinite loop like its AVR counterpart. fixing your GCC installation would be the recommendable option. – Dylan Oct 17 '13 at 06:55
  • 1
    See: [newlib syscalls](https://sourceware.org/newlib/libc.html#Syscalls); you need to implement these, if they are not implemented for your board. Also, there can be issues with your code. Newlib doesn't support everything; See [Gatliff's newlib](http://www.billgatliff.com/newlib.html) overview. – artless noise Oct 17 '13 at 17:32
  • 1
    I think the second link in the above comment is dead now. Archive version: https://web.archive.org/web/20130802225623/http://www.billgatliff.com/newlib.html – AJM Jul 14 '22 at 15:20

3 Answers3

114

This happens when compiling a file with arm-none-eabi-gcc in one machine/architecture to load it in an ARM target machine. Most probably you are not making use of semihosting, you want to retarget.

ARM® Compiler toolchain Version 4.1 indicates:

Semihosting is a mechanism that enables code running on an ARM target to communicate and use the Input/Output facilities on a host computer that is running a debugger.

From the toolchain's readme.txt (under folder .../gcc-arm-none-eabi-VERSION/share/doc/gcc-arm-none-eabi):

** non-semihosting/retarget

If you are using retarget, linking like: $ arm-none-eabi-gcc --specs=nosys.specs $(OTHER_LINK_OPTIONS)

For me --specs=nosys.specs was enough ;)

Calamar
  • 1,547
  • 1
  • 13
  • 25
  • I did a quick search for the ARM compiler documentation quoted above. I couldn't find docs as far back as 4.1, but the ["ARM Compiler Software Development Guide Version 5.06"](https://developer.arm.com/documentation/dui0471/m/what-is-semihosting-/what-is-semihosting-?lang=en) seems to be its successor, and does include that definition of semihosting. – AJM Jul 14 '22 at 15:54
87

Use --specs=nosys.specs:

arm-none-eabi-gcc --specs=nosys.specs $(OTHER_LINK_OPTIONS)

Reference:

Finomnis
  • 18,094
  • 1
  • 20
  • 27
ele1000
  • 871
  • 6
  • 3
  • Why `--specs=nosys.specs` is not specified by default? – pmor Jan 21 '22 at 14:51
  • 2
    If anyone reading this has enough rep for a 1-character edit, there's now an https version of the "ARM Options" URL. – AJM Jul 18 '22 at 11:55
8

I had the same problem. The solution was as follows:

  • Add options -ffreestanding -flto into compiler call line.
  • And add options -flto -ffreestanding -nostdlib into linker call line.
User Human
  • 81
  • 1
  • 1
  • I think -f options are not allowed for the linker, right? Here is my shiny error `arm-none-eabi-ld: -f may not be used without -shared` – debuti Mar 05 '20 at 11:47