2

Based on the Limifrog STM32L board examples, I am trying to setup my own toolchain for ARM development (STM32F40x based).

I get an error:

C:\Users\pmu\embedded\empty_stm32>make
arm-none-eabi-gcc  -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -I
./inc -I./../Libraries/CMSIS/Device/ST/STM32F4xx/Include -I./../Libraries/CMSIS/
Include -Os -Wall -ffunction-sections -fdata-sections -fno-exceptions -Wno-write
-strings -o main src/main.c
c:/program files (x86)/gnu tools arm embedded/4.9 2015q3/bin/../lib/gcc/arm-none
-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7e-m/fpu\libc.a(lib_a-exit.o): In
function `exit':
exit.c:(.text.exit+0x16): undefined reference to `_exit'
collect2.exe: error: ld returned 1 exit status
make: *** [main] Error 1

when compiling:

#include "My_Globals.h"

int main(void) {

  while(1) {

  }

  return 0;
}

What does this error mean? Why does it not occur in the Board examples above?

The Makefile

# Compilation Tools
CC      = arm-none-eabi-gcc
LD      = arm-none-eabi-ld
CP      = arm-none-eabi-objcopy
OD      = arm-none-eabi-objdump
SIZE    = arm-none-eabi-size

# Optimization (ß,1,2,s)
OPT = s

ROOT        = ./..
CMSIS_DIR   = $(ROOT)/Libraries/CMSIS
HAL_DIR     = $(ROOT)/Libraries/CMSIS/Include
SRC         = ./src
INC_LOCAL  = ./inc

INCLUDES   =  \
    $(INC_LOCAL) \
    $(CMSIS_DIR)/Device/ST/STM32F4xx/Include \
    $(CMSIS_DIR)/Include

ARCH_FLAGS = -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16

LD_FLAGS   = 

# Compiler flags
CFLAGS =  \
    $(ARCH_FLAGS) \
    $(addprefix -I,$(INCLUDES)) \
    -O$(OPT) \
    -Wall \
    -ffunction-sections \
    -fdata-sections \
    -fno-exceptions \
    -Wno-write-strings

main:
    @echo "start"
    @echo $@
    $(CC) $(LDFLAGS) $(CFLAGS) -o $@ src/main.c

Now, what libraries can be used to re-use the implementation of _exit() from elsewhere?

poseid
  • 6,986
  • 10
  • 48
  • 78
  • 2
    The makefile for the other project links against some external libraries which this code does not; one of those is almost certainly providing an implementation of `_exit()` that [keeps the linker happy](http://stackoverflow.com/a/12574400/3156750). – Notlikethat Dec 23 '15 at 16:11
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Notlikethat Dec 23 '15 at 16:12
  • 1
    just add a few lines to define the exit function have it do nothing, infinite loop, whatever... – old_timer Dec 24 '15 at 01:46
  • thanks dwelch and Notlikethat, including _exit() manually solves the problem. However, looking deeper, I see _exit() is part of libs such as newlib http://wiki.osdev.org/Porting_Newlib - how/what options are there to re-use _exit() ? – poseid Dec 28 '15 at 12:55
  • 1
    Be aware that the C runtime typically does a whole bunch of work both before `main()` is called, and after it returns - on Linux and friends [`_exit()` is the syscall wrapper](http://man7.org/linux/man-pages/man2/exit.2.html) which is used to finally tell the OS "OK, this whole process is finished, stop running it and clean up." If you don't _have_ an OS, nor ever expect `main()` to return, there's really nothing to do except placate the C runtime code's demand for a symbol to link to. – Notlikethat Dec 29 '15 at 00:02
  • Added a Linker flag "--specs=nosys.specs"
    Refer to https://stackoverflow.com/questions/19419782/exit-c-text0x18-undefined-reference-to-exit-when-using-arm-none-eabi-gcc
    – Lak Fu Aug 21 '17 at 03:09

0 Answers0