c - Makefile 可能错误,未定义对 C 中函数的引用

标签 c makefile terminal compiler-errors compilation

尝试编译我的程序,该程序包含三个源文件,分别是 hencode.chdecode.chtable.chtable.c 文件包含前两个文件使用的所有函数,htable.h 文件包含原型(prototype)。这是我的 Makefile,但我一直在终端上收到 undefined reference to 'function' 错误。

两个文件都没有正确编译。 我使用此命令编译文件:gcc -o hdecode -Wall -ansi -pedantic hdecode.c

我不知道哪里出了问题。我的所有文件中都包含了 "htable.h"。我认为错误可能在 Makefile 中,但我无法发现它。感谢您的帮助。

这是我的生成文件:

CC      = gcc
CFLAGS = -Wall -ansi -pedantic -g -std=c99
LD      = gcc

all: hdecode hencode

hdecode: htable.o hdecode.o
    $(LD) $(LDFLAGS) htable.o hdecode.o -o hdecode

hencode: htable.o hencode.o
    $(LD) $(LDFLAGS) htable.o hencode.o -o hencode

hdecode.o: hdecode.c
    $(CC) $(CFLAGS) -c hdecode.c

hencode.o: hencode.c
    $(CC) $(CFLAGS) -c hencode.c

htable.o: htable.c
    $(CC) $(CFLAGS) -c htable.c

clean:
    @rm *.o hdecode hencode

最佳答案

Trying to compile my program which has two source files called hencode.c and hdecode.c. The htable.c file contains all the functions that the two files both use and the htable.h file contains the prototypes.

请注意,您的程序由三个 源文件组成,而不是两个。链接最终程序时,您需要使 htable.c 中定义的符号可用。换句话说,#include-ing 文件不会在最终程序中自动编译或链接它。

My hencode file is compiling correctly, however my hdecode file is not. I use this command to compile the file: gcc -o hdecode -Wall -ansi -pedantic hdecode.c

这还不清楚——您是使用 Makefile 还是手动编译?如果是后者,则需要传递两个源文件(或目标文件,如果以前编译过):

# Note the added htable.c
gcc -o hdecode -Wall -ansi -pedantic htable.c hdecode.c

最后,还要注意 Makefile 可以简化:

CC      = gcc
CFLAGS = -Wall -ansi -pedantic -g -std=c99

all: hdecode hencode

hdecode: htable.o hdecode.o
hencode: htable.o hencode.o
hdecode.o: hdecode.c
hencode.o: hencode.c
htable.o: htable.c

clean:
    @rm *.o hdecode hencode

关于c - Makefile 可能错误,未定义对 C 中函数的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50264874/

相关文章:

python - 使用 python-wnck 生成和移动后窗口位置稍微偏离

makefile - QtCreator .pro 文件中的 *= 是什么意思?

c - Apache APR 函数 apr_procattr_cmdtype_set 混淆

c - 复杂数据类型的 Typedef

c - 使用静态链接库中的枚举

makefile - Sphinx 的默认 Makefile

c - 平台条件 makefile

linux - 终端似乎没有反向搜索(ctrl-r 什么都不做)

emacs - M-x shell 中 vt100/ansi-color 转义的非临时方式

c - 如何从函数返回指向结构的指针?