【问题标题】:Makefile possibly wrong, undefined reference to function in CMakefile 可能错误,未定义对 C 中函数的引用
【发布时间】:2018-10-20 06:01:05
【问题描述】:

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

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

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

这是我的 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

【问题讨论】:

  • 欢迎来到 Stack Overflow。请尽快(重新)阅读 AboutHow to Ask 页面,但更重要的是,请阅读有关如何创建 MCVE (How to create a Minimal, Complete, and Verifiable Example?) 或 SSCCE (Short, Self-Contained, Correct Example) 的信息。
  • 报告缺少哪个函数?它是在哪个源文件中定义的?我们应该怎么猜?当询问有关链接错误的问题时,请包括实际的错误消息(或者可能是每个函数的第一条消息,对于前几个函数)。没有这些信息,就不可能为您提供太多帮助。有了这些信息,可能很容易提供帮助。例如,如果缺少的函数是标准函数,例如 sqrt(),我们可以诊断出您需要数学库(通常是 -lm,在完全需要它的系统上)。

标签: c makefile terminal compiler-errors compilation


【解决方案1】:

尝试编译我的程序,该程序有两个源文件,分别为 hencode.chdecode.chtable.c 文件包含两个文件都使用的所有函数,htable.h 文件包含原型。

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

我的hencode 文件编译正确,但我的hdecode 文件编译不正确。我使用这个命令编译文件: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

【讨论】:

  • 我正在使用 Makefile
  • @AmyWilson:在这种情况下,问题似乎出在代码中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
  • 1970-01-01
  • 1970-01-01
  • 2021-06-18
  • 1970-01-01
相关资源
最近更新 更多