gpt4 book ai didi

c - 使用 makefile 时未定义对 `main' 的引用

转载 作者:太空宇宙 更新时间:2023-11-04 05:05:07 24 4
gpt4 key购买 nike

我有四个文件 list.h list.c test_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*/

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): relocation 0 has invalid symbol index 2 /usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o: In function _start':(.text+0x18): undefined reference tomain' collect2: error: ld returned 1 exit status make: *** [list] Error 1

这里有什么问题吗?

最佳答案

您尚未指定构建目标的规则list , 所以 make正在推断以下规则,该规则失败,因为您没有 main在你的 list.c 中发挥作用.

cc     list.c   -o list

list无论如何都不应该构建为可执行文件(没有主程序),只是不要尝试构建 list作为你的目标 Makefile , 然后 test_list将正确构建。

all:  test_list

关于c - 使用 makefile 时未定义对 `main' 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34782943/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com