gpt4 book ai didi

c - main.o :main. c:function main: 错误:未定义对 'variable_name' 的引用

转载 作者:行者123 更新时间:2023-11-30 17:34:42 28 4
gpt4 key购买 nike

我是 C 语言的新手(6 小时前开始),我知道有大量的在线引用资料,我应该(并且将会)详细查看,但现在,我有紧急情况需要帮助。我有一个包含以下文件的项目文件夹:

boundary_val.c
boundary_val.h
helper.c
helper.h
init.c
init.h
main.c
main.o
sor.c
sor.h
uvp.c
uvp.h
visual.c
visual.h
makefile

main.c 应该调用在列出的文件中找到的其他函数。

makefile 内容如下:

CC = gcc
CFLAGS = -Wall -pedantic -Werror
.c.o: ; $(CC) -c $(CFLAGS) $<

OBJ = helper.o\
init.o\
boundary_val.o\
uvp.o\
main.o\
sor.o\
visual.o


all: $(OBJ)
$(CC) $(CFLAGS) -o sim $(OBJ) -lm

%.o : %.c
$(CC) -c $(CFLAGS) $*.c -o $*.o

clean:
rm $(OBJ)

helper.o : helper.h
init.o : helper.h init.h
boundary_val.o: helper.h boundary_val.h
uvp.o : helper.h uvp.h
visual.o : helper.h
sor.o : sor.h

main.o : helper.h init.h boundary_val.h uvp.h visual.h sor.h

当我在终端中运行 make main 时,出现以下错误:

gcc   main.o   -o main
main.o:main.c:function main: error: undefined reference to 'read_parameters'
main.o:main.c:function main: error: undefined reference to 'matrix'
main.o:main.c:function main: error: undefined reference to 'matrix'
main.o:main.c:function main: error: undefined reference to 'matrix'
main.o:main.c:function main: error: undefined reference to 'matrix'
main.o:main.c:function main: error: undefined reference to 'init_uvp'
main.o:main.c:function main: error: undefined reference to 'calculate_dt'
main.o:main.c:function main: error: undefined reference to 'boundaryvalues'
main.o:main.c:function main: error: undefined reference to 'calculate_fg'
main.o:main.c:function main: error: undefined reference to 'calculate_rs'
main.o:main.c:function main: error: undefined reference to 'sor'
main.o:main.c:function main: error: undefined reference to 'calculate_uv'
main.o:main.c:function main: error: undefined reference to 'write_vtkFile'
main.o:main.c:function main: error: undefined reference to 'write_vtkFile'
main.o:main.c:function main: error: undefined reference to 'free_matrix'
main.o:main.c:function main: error: undefined reference to 'free_matrix'
main.o:main.c:function main: error: undefined reference to 'free_matrix'
main.o:main.c:function main: error: undefined reference to 'free_matrix'
collect2: ld returned 1 exit status
make: *** [main] Error 1

这些 undefined reference 是在其他文件中定义的函数,并通过以下内容链接到 main.c 文件:

#include "helper.h"
#include "visual.h"
#include "init.h"
#include "sor.h"
#include "boundary_val.h"
#include "uvp.h"
#include <stdio.h>

看起来make正在读取main.o而不是main.c,有人可以告诉我发生了什么事吗?

编辑:

运行make clean并收到此错误:

rm helper.o init.o boundary_val.o uvp.o main.o sor.o visual.o
rm: cannot remove `helper.o': No such file or directory
rm: cannot remove `init.o': No such file or directory
rm: cannot remove `boundary_val.o': No such file or directory
rm: cannot remove `uvp.o': No such file or directory
rm: cannot remove `sor.o': No such file or directory
rm: cannot remove `visual.o': No such file or directory
make: *** [clean] Error 1

最佳答案

当您调用 make main 时,您实际上是使用目标 main 调用 make,而该目标在您的 makefile 中并不存在。目标 all 是一个特殊目标,在未指定目标时构建,在本例中是单独使用 make 构建的。当您调用 make main 时,未找到名为 main 的目标,因此 make 推断它应该使用已存在的 构建该目标main.o 目标文件。您可以从该行看到这一点

gcc   main.o   -o main

此外,您应该将 -f 标志添加到 clean 目标中的 rm 命令,以便它忽略不存在的文件(毕竟你只想清除所有内容)。

clean:
rm -f $(OBJ)

TLDR;您应该单独使用 make 来构建 all 目标。

有关如何构建 makefile 的更多信息,请参阅此 makefile 教程:http://www.gnu.org/software/make/manual/make.html#Introduction

关于c - main.o :main. c:function main: 错误:未定义对 'variable_name' 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23224397/

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