我有四个文件list.h
list.c
test_list.c
makefile
列表。h
#ifndef List_H
#define List_H
#endif
/*nothing else*/
列表。c
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
/*nothing else*/
test_list.c
#include "list.h"
#include <stdio.h>
int main(){
return 0;
}
/*nothing else*/
生成文件
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
当我在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):未定义对
main'collect2:错误:ld返回1退出状态生成:***[list]错误1
这里怎么了?
您尚未指定用于生成目标列表
的规则,因此make
正在推断以下规则,但由于列表中没有
main
函数,该规则失败。
cc list.c -o list
由于list
无论如何都不应该被构建为可执行文件(没有main),所以不要尝试将list
构建为makefile
中的目标,然后test_list
将正确构建。
all: test_list
您定义了目标列表,但没有为其定义规则。因此make尝试了它的隐式规则,通过发出以下命令来生成这个目标规则
cc list.c -o list
因此,由于在list.c中没有名为main的符号,您得到了链接错误
您可以通过运行
make -r
您已经将程序构建为test_list
,因此不需要list
目标。
更改:
all: list test_list
致:
all: test_list