获得;collect2:错误:ld返回1退出状态“;在C中

获得;collect2:错误:ld返回1退出状态“;在C中,c,compiler-errors,makefile,C,Compiler Errors,Makefile,这是我的编译器生成文件 CC = arm-none-eabi-gcc AS = arm-none-eabi-as LD = arm-none-eabi-ld OD = arm-none-eabi-objdump CFLAGS = -g -c -O1 -mcpu=cortex-m0 -mthumb AFLAGS = -g -c -mcpu=cortex-m0 -mthumb LDFLAGS = -g -mcpu=cortex-m0 -mthumb --specs=rdimon.specs -lc

这是我的编译器生成文件

CC = arm-none-eabi-gcc
AS = arm-none-eabi-as
LD = arm-none-eabi-ld
OD = arm-none-eabi-objdump

CFLAGS = -g -c -O1 -mcpu=cortex-m0 -mthumb
AFLAGS = -g -c -mcpu=cortex-m0 -mthumb
LDFLAGS = -g -mcpu=cortex-m0 -mthumb --specs=rdimon.specs -lc -lrdimon
ODFLAGS = -d

all: HW1.elf

test: HW1.elf
        @ echo "...copying"
        qemu-system-arm  -machine versatilepb -cpu cortex-m3 -nographic -monitor null -serial null -semihosting -k\
ernel HW1.elf

HW1.elf: test.o t1.o
        $(CC) $(LDFLAGS) test.o t1.o -o HW1.elf

test.o: test.c t1.o
        @ echo ".compiling"
        $(CC) $(CFLAGS) test.c t1.o -o test.o

t1.o: t1.s
        @ echo ".assembling"
        $(AS) $(AFLAGS) t1.s -o t1.o

clean:
        rm -rf t1.o test.o HW1.elf
当我做所有的
时,我得到

.assembling
arm-none-eabi-as -g -c -mcpu=cortex-m0 -mthumb t1.s -o t1.o
.compiling
arm-none-eabi-gcc -g -c -O1 -mcpu=cortex-m0 -mthumb test.c t1.o -o test.o
arm-none-eabi-gcc: warning: t1.o: linker input file unused because linking not done
arm-none-eabi-ld -g -mcpu=cortex-m0 -mthumb --specs=rdimon.specs -lc -lrdimon test.o -o HW1.elf
arm-none-eabi-ld: unrecognised emulation mode: thumb
Supported emulations: armelf
make: *** [HW1.elf] Error 1
我查看了StackOverflow上的无数collect2错误,发现有一个函数未定义或标记错误

我已经在~/.bashrc文件中输入了调用arm none eabi库函数的目录。我知道这个错误与t1.s和/或test.c文件无关,只与处理makefile的不正确的符号有关,但我不知道我做错了什么。我知道这是用
test.o
做的

我将提供test.c和t1.s,以防它们出现问题

测试c

include <stdio.h>

extern int inc(int);

#define RED_COUNT 5
#define YELLOW_COUNT 2
#define GREEN_COUNT 5

enum light_states {RED, YELLOW, GREEN};
static int state = RED;

void stop_light () {
  static int state_counter = 0;

  switch (state) {
  case RED:
    printf("RED\n");
    if (++state_counter >= RED_COUNT) {
      state_counter = 0;
      state = GREEN;
    }
    break;

  case GREEN:
    printf("GREEN\n");
    if (++state_counter >= GREEN_COUNT) {
      state_counter = 0;
      state = YELLOW;
    }
    break;

  case YELLOW:
    printf("YELLOW\n");
    if (++state_counter >= YELLOW_COUNT) {
      state_counter = 0;
      state = RED;
    }
    break;

  default:
    state = RED;
    state_counter = 0;
    break;
  }
}

main(){
  int i;

  while (i < 36) {
    stop_light();
    i = inc(i);
  }
}
编辑1: 更新了丢失的'-c',我现在收到一个关于LD的新错误

编辑2:
更新了LD错误。现在一切都修好了,答案非常有用

您在
CFLAGS
中忘记了
-c
。否则,生成文件将导致生成尝试将每个对象链接为可执行文件,而不是仅将其构建为
.o
。不要使用
ld
链接!调用C编译器进行链接,它使用正确的选项和操作数调用
ld
。最好将Makefiles编译和链接分开。未定义的引用是在libc中找到的,那么对于您的目标体系结构,正确的C库在哪里?您是否链接到正确构建的东西?@kaylum ah thanky.gcc有合理的默认ld选项供正常使用,使其比ld更易于使用,但是,如果您正在交叉编译,并且在本例中,gcc试图链接并失败,那么它们可能是不正确的,因为kaylum指出缺少-c选项
    .text
    .syntax unified
    .thumb
    .global inc
    .type   inc, %function
inc:
    adds    r0,r0,#1
    bx  lr