C 对“main'”的未定义引用;使用makefile时
我有四个文件C 对“main'”的未定义引用;使用makefile时,c,gcc,makefile,C,Gcc,Makefile,我有四个文件list.hlist.ctest\u list.cMakefile list.h #ifndef List_H #define List_H #endif /*nothing else*/ list.c #include "list.h" #include <stdio.h> #include <stdlib.h> /*nothing else*/ #include "list.h" #include <stdio.h> int main
list.h
list.c
test\u list.c
Makefile
list.h
#ifndef List_H
#define List_H
#endif
/*nothing else*/
list.c
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
/*nothing else*/
#include "list.h"
#include <stdio.h>
int main(){
return 0;
}
/*nothing else*/
#包括“list.h”
#包括
#包括
/*没有别的了*/
测试列表.c
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
/*nothing else*/
#include "list.h"
#include <stdio.h>
int main(){
return 0;
}
/*nothing else*/
#包括“list.h”
#包括
int main(){
返回0;
}
/*没有别的了*/
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
CC=CC
CXX=CC
CCFLAGS=-g-std=c99-Wall-Werror
全部:列表测试列表
%.o:%.c
$(CC)-c$(CCFLAGS)$<
测试列表:list.o测试列表.o
$(CC)-o测试列表。o测试列表。o
测试:测试列表
/测试列表
清洁:
rm-f core*.o测试列表
在shell中输入make时,出现以下错误:
/usr/bin/ld:/usr/lib/debug/usr/lib/i386 linux gnu/crt1.o(.debug_行):重新定位0具有无效的符号索引2
/usr/lib/gcc/i686 linux gnu/4.8/../../../..//i386 linux gnu/crt1.o:在函数\u start':(.text+0x18):未定义对
main的引用
collect2:错误:ld返回了1个退出状态
make:**[列表]错误1
这里有什么问题?您没有指定用于构建目标
列表的规则,因此make
正在推断以下规则,该规则失败,因为您的list.c
中没有main
函数
cc list.c -o list
由于list
无论如何都不应构建为可执行文件(无主文件),因此不要尝试在Makefile
中将list
构建为目标,然后test\u list
将正确构建
all: test_list
您定义了目标列表,但未为其定义规则。因此,make通过发出以下命令,尝试使用其隐式规则生成此目标规则
cc list.c -o list
由于list.c中没有名为main的符号,所以出现了链接错误
只需运行
make -r
您已经将程序构建为测试列表
,因此不需要列表
目标
更改:
all: list test_list
致:
另外,您的“%.o
规则与隐式规则的作用相同。但是你可能应该为list.o
,添加一个依赖项到list.h
。谢谢,这正好解决了我的问题,我的老师喜欢写all:test\u list test
,这更标准吗?我不知道链接后的目标是否都应该是二进制文件。@brickmanjim,是的,通常使用目标all
和实际的二进制目标作为依赖项。我是指all:test\u list test
中的第二个目标test
,它不是一个二进制文件,但它也在all
@brickmanjim中,我从两个方面都看到了它,但我更经常地看到test
目标独立于all
目标。也就是说,默认构建目标不执行测试。