Search code examples
ctoolchainbare-metal

Undefined reference to strcmp with toolchain


I am trying to write a simple bare-metal program on the raspberry pi, but when I am trying to use to use strcmp I am getting the following error:

arm-none-eabi-ld -o main.elf  -T ./src/memmap vectors.o led.o main.o uart.o general.o
main.o: In function `main':
main.c:(.text.startup+0x80): undefined reference to `strcmp'

This is my make file:

ARMGNU = arm-none-eabi


ARMGNU ?= arm-none-eabi

AOPS = --warn --fatal-warnings -mcpu=arm1176jzf-s -march=armv6
COPS = -Wall -O2 -nostdlib -nostartfiles -ffreestanding -mcpu=arm1176jzf-s -mtune=arm1176jzf-s -mhard-float 

LIBRARIES= -I ./include -I ./src 

all : kernel.img 
    rm -f *.bin
    rm -f *.o
    rm -f *.elf
    rm -f *.list
    rm -f *.hex

vectors.o : ./src/vectors.s
    $(ARMGNU)-as $(AOPS) $(LIBRARIES) -mfpu=vfp ./src/vectors.s -o vectors.o

main.o : ./src/main.c
    $(ARMGNU)-gcc $(COPS) $(LIBRARIES) -c ./src/main.c -o main.o

led.o : ./src/led.c
    $(ARMGNU)-gcc $(COPS) $(LIBRARIES) -c ./src/led.c -o led.o

uart.o : ./src/uart.c
    $(ARMGNU)-gcc $(COPS) $(LIBRARIES) -c ./src/uart.c -o uart.o

general.o : ./src/general.c
    $(ARMGNU)-gcc $(COPS) $(LIBRARIES) -c ./src/general.c -o general.o

kernel.img : ./src/memmap vectors.o main.o led.o uart.o general.o
    $(ARMGNU)-ld -o main.elf  -T ./src/memmap vectors.o led.o main.o uart.o general.o
    $(ARMGNU)-objdump -D main.elf > main.list
    $(ARMGNU)-objcopy main.elf kernel.img -O binary


clean:
    rm -r *.img

Solution

  • Here is a simple recursive strcmp (your compiler should optimize away the recursion though)

    int strcmp(const char *a,const char *b){
      if (! (*a | *b)) return 0;
      return (*a!=*b) ? *a-*b : strcmp(++a,++b);
    }