使用makefile时对"main"的未定义引用

L's*_*rld 0 c gcc makefile

我有四个文件 list.h list.c test_list.c Makefile

list.h

#ifndef List_H
#define List_H
#endif
/*nothing else*/
Run Code Online (Sandbox Code Playgroud)

list.c

#include "list.h"
#include <stdio.h>
#include <stdlib.h>
/*nothing else*/
Run Code Online (Sandbox Code Playgroud)

test_list.c

#include "list.h"
#include <stdio.h>   
int main(){
    return 0;
}
/*nothing else*/
Run Code Online (Sandbox Code Playgroud)

Makefile文件

CC=cc
CXX=CC
CCFLAGS= -g -std=c99 -Wall -Werror

all: list test_list

%.o : %.c
    $(CC) -c $(CCFLAGS) $<

test_list: list.o test_list.o
    $(CC) -o test_list list.o test_list.o

test: test_list
    ./test_list

clean:
    rm -f core *.o test_list
Run Code Online (Sandbox Code Playgroud)

当我在shell中输入make时,出现错误:

/ usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_line):重定位0具有无效的符号索引2/usr/lib/gcc/i686-linux- gnu/4.8 /../../../ i386-linux-gnu/crt1.o:在函数_start':(.text+0x18): undefined reference tomain'collect2:error:ld返回1退出状态make:***[list]错误1

这有什么不对?

mer*_*011 7

您尚未指定用于构建目标的规则list,因此make推断出以下规则,该规则因您没有main功能而失败list.c.

cc     list.c   -o list
Run Code Online (Sandbox Code Playgroud)

既然list不应该构建为可执行文件(没有主要版本),只是不要尝试list在您的构建中作为目标Makefile,然后test_list将正确构建.

all:  test_list
Run Code Online (Sandbox Code Playgroud)