Undefined reference to 'add' (external assembler function)

时间:2019-04-08 14:00:57

标签: c assembly makefile

I have an example with 4 files: add_main.c, add.h, Makefile and add.s. When I run make command on terminal I get an error "add_main.c:11: undefined reference to 'add'". I checked out answers to similar problems but they didn't work for me.

I tried declaring function add in add_main.c with and without keyword extern and writing function name as _add. That didn't help.

make response:

user@ser:~/add$ make
arm-linux-gnueabi-gcc -g -mcpu=xscale -O0 -Wall -o add_main.o-c add_main.c
/tmp/cc3HUJRk.o: In function 'main':
/home/user/add/add_main.c:11: undefined reference to 'add'
collect2: error: ld return 1 exit satus
Makefile:34: recipe for target 'add_main.o' failed
make: *** [add_main.o] Error 1
//add.h
int add(int a, int b);
//add_main.c
#include <stdio.h>
#include <stdlib.h>
#include "add.h"

extern int add(int a, int b); 
.............
//add.s
    .text
    .align  2
    .global add
    .type   add, %function
add:
    add r0, r0, r1
    bx lr
    .size   add, .-add
//Makefile
TARGET := add

OBJ := $(TARGET)_main.o $(TARGET).o

ASFLAGS = -mcpu=xscale -alh=$*.lis -L
CFLAGS = -mcpu=xscale -O0 -Wall
LDFLAGS = -g

CC := arm-linux-gnueabi-gcc
AS := arm-linux-gnueabi-as

.PHONY: test all clean distclean

test:   all
    qemu-arm $(TARGET)

all:    $(TARGET)

clean:
    $(RM) $(TARGET) *.o

distclean:  clean
    $(RM) *.lis *~

allhfiles := $(wildcard *.h)

$(TARGET):  $(OBJ)
    $(CC) $(LDFLAGS) -o $@ $^

%.o:    %.s
    $(AS) -g $(ASFLAGS) -o $@ $<

%.o:    %.c $(allhfiles)
    $(CC) -g $(CFLAGS) -o $@ -c $<

%.s:    %.c $(allhfiles)
    $(CC) $(CFLAGS) -fomit-frame-pointer -o $@ -S $<

0 个答案:

没有答案