尝试编译我的程序,该程序包含三个源文件,分别是 hencode.c
、hdecode.c
和 htable.c
。 htable.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
andhdecode.c
. Thehtable.c
file contains all the functions that the two files both use and thehtable.h
file contains the prototypes.
请注意,您的程序由三个 源文件组成,而不是两个。链接最终程序时,您需要使 htable.c
中定义的符号可用。换句话说,#include
-ing 文件不会在最终程序中自动编译或链接它。
My
hencode
file is compiling correctly, however myhdecode
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/