Search code examples
cavratmegaavr-gcc

AVR GCC - Use static library - undefined reference errors


I'm currently trying to read values from a temperature sensor (Dallas ds18b20) with an AVR atmega328p. In order to read values, I need to import an external library (using this one). I have used the Makefile in the external repository to create a static library (libds18b20.a in lib directory). I also added the header files to my own source. I have the following Makefile:

PORT_ID=/dev/ttyACM0
MCU=atmega328p
F_CPU=1200000
CC=avr-gcc
PROGRAMMER_ID=stk500v1
OBJCOPY=avr-objcopy
CFLAGS=-std=c99 -Wall -g -Os -mmcu=${MCU} -DF_CPU=${F_CPU} -I.
TARGET=main
SRCS=main.c
BAUD_RATE=19200
PATH_DS18B20=./lib

all:
    ${CC} -L ${PATH_DS18B20} ${CFLAGS} -o ${TARGET}.bin ${SRCS}
    ${OBJCOPY} -j .text -j .data -O ihex ${TARGET}.bin ${TARGET}.hex

flash:
    avrdude -v -P ${PORT_ID} -b ${BAUD_RATE} -c ${PROGRAMMER_ID} -p ${MCU} -U flash:w:${TARGET}.hex

clean:
    rm -f *.bin *.hex

However, when I try to run this Makefile I got the following errors (output partially omitted):

/main.c:(.text.startup+0x2e): undefined reference to `ds18b20convert'
/main.c:60: undefined reference to `ds18b20read'

These functions are defined in the headers files. I'm expecting that the static library is not properly linked. What am I doing wrong here?


Solution

  • I managed to get this working by omitting the -L flag and put the path directly behind the ${SRCS}-variable including the name of the static library:

    PORT_ID=/dev/ttyACM0
    MCU=atmega328p
    F_CPU=1200000
    CC=avr-gcc
    PROGRAMMER_ID=stk500v1
    OBJCOPY=avr-objcopy
    CFLAGS=-std=c99 -Wall -g -Os -mmcu=${MCU} -DF_CPU=${F_CPU} -I.
    TARGET=main
    SRCS=main.c
    BAUD_RATE=19200
    PATH_DS18B20=./lib/libds18b20.a
    
    all:
        ${CC} ${CFLAGS} -o ${TARGET}.bin ${SRCS} ${PATH_DS18B20}
        ${OBJCOPY} -j .text -j .data -O ihex ${TARGET}.bin ${TARGET}.hex
    
    flash:
        avrdude -v -P ${PORT_ID} -b ${BAUD_RATE} -c ${PROGRAMMER_ID} -p ${MCU} -U flash:w:${TARGET}.hex
    
    clean:
        rm -f *.bin *.hex