gpt4 book ai didi

c - 使用Makefile后出现 undefined reference 错误

转载 作者:行者123 更新时间:2023-11-30 15:36:21 26 4
gpt4 key购买 nike

我编写了一个 makefile 来链接我想要的所有 .c 文件。但是我再次收到 undefined reference 的错误。makefile 的代码是:

FILES.o=set.o hash.o nfa.o printnfa.o input.o terp.o dfa.o minimize.o defnext.o print_ar.o pairs.o squash.o print.o assort.o prnt.o printv.o bintoasc.o ferr.o onferr.o fputstr.o pchar.o driver.o searchenv.o hashadd.o 

PROGRAM= lexer

all: ${PROGRAM}

${PROGRAM}: ${FILES.o}
${CC} -o $@ ${CFLAGS} $^ ${LDFLAGS} ${LDLIBS}

但它仍然会导致未定义的错误,例如:

/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o: In function   `_start':(.text+0x20): undefined reference to `main'
nfa.o: In function `parse_err':
nfa.c:(.text+0x21): undefined reference to `Actual_lineno'
nfa.o: In function `save':
nfa.c:(.text+0x2d1): undefined reference to `Lineno'
nfa.o: In function `advance':
nfa.c:(.text+0x8b4): undefined reference to `esc'
nfa.o: In function `rule':
nfa.c:(.text+0xae1): undefined reference to `Unix'
nfa.o: In function `term':
nfa.c:(.text+0xfbd): undefined reference to `Unix'
nfa.c:(.text+0x10a2): undefined reference to `Unix'
nfa.o: In function `thompson':
nfa.c:(.text+0x1355): undefined reference to `CLEAR_STACK'
nfa.c:(.text+0x13ab): undefined reference to `Verbose'
nfa.c:(.text+0x13d3): undefined reference to `printf_nfa'
nfa.c:(.text+0x13d9): undefined reference to `Verbose'
input.o: In function `get_expr':
input.c:(.text+0x13): undefined reference to `Input_buf'
input.c:(.text+0x20): undefined reference to `Verbose'
input.c:(.text+0x2b): undefined reference to `Actual_lineno'
input.c:(.text+0x52): undefined reference to `Actual_lineno'
input.c:(.text+0x7a): undefined reference to `Actual_lineno'
input.c:(.text+0x83): undefined reference to `Actual_lineno'
input.c:(.text+0x8a): undefined reference to `Input_buf'
input.c:(.text+0x93): undefined reference to `Input_buf'
input.c:(.text+0xe2): undefined reference to `Ifile'
input.c:(.text+0x10c): undefined reference to `Verbose'
input.c:(.text+0x11c): undefined reference to `Input_buf'
input.c:(.text+0x136): undefined reference to `Input_buf'
dfa.o: In function `dfa':
dfa.c:(.text+0x69): undefined reference to `Verbose'
dfa.c:(.text+0x1c6): undefined reference to `Verbose'
dfa.c:(.text+0x215): undefined reference to `Verbose'
dfa.o: In function `get_unmarked':
dfa.c:(.text+0x3a8): undefined reference to `Verbose'
minimize.o: In function `init_groups':
minimize.c:(.text+0x28c): undefined reference to `Verbose'
minimize.o:minimize.c:(.text+0x550): more undefined references to `Verbose' follow
print.o: In function `pdriver':
print.c:(.text+0x483): undefined reference to `No_lines'
print.c:(.text+0x4eb): undefined reference to `No_lines'
print.c:(.text+0x4f6): undefined reference to `Input_file_name'
print.c:(.text+0x585): undefined reference to `No_lines'
collect2: error: ld returned 1 exit status
make: *** [lexer] Error 1

但是,所有 undefined reference 都存在于 2 个头文件中:1.stack.h 2.global.h..仍然是错误。请帮忙!

我使用的global.h文件是:

#ifndef __GLOBAL_H 
#define __GLOBAL_H
#include <stdio.h>

#ifdef ALLOC
# define CLASS
# define I(x) x
#else
# define CLASS extern
# define I(x)
#endif
#define MAXINP 2048

CLASS int Verbose I(=0);
CLASS int No_lines I(=0);
CLASS int Unix I(=0);
CLASS int Public I(=0);
CLASS char *Template I(="lex.par");
CLASS int Actual_lineno I(=1);
CLASS int Lineno I(=1);

CLASS char Input_buf[MAXINP]; //line buffer for input
CLASS char *Input_file_name; //Input file name
CLASS FILE *Ifile; //Input Stream
CLASS FILE *ofile; //Output Stream

#endif

最佳答案

您的问题与 Makefile 本身无关。仅 header 包含文件 global.h 包含变量声明。这些声明必须在一个编译单元中转化为实际定义,以便在您的目标文件之一中为它们分配空间。

根据头文件的设计方式,变量 ALLOC 的定义决定了变量是刚刚声明还是定义。

不带 ALLOC 的常规包含结果例如:

extern int Verbose;

extern 关键字表示变量 Verbose 未定义。没有为其分配内存(因此它不能有初始化程序),并且它可能是在另一个对象文件中定义的。

ALLOC被定义时,声明就变成了带有初始化的定义:

int Verbose = 0;

选择包含 global.h 的文件之一并在包含它之前定义 ALLOC,例如:

输入.c

#define ALLOC
#include "global.h"

然后,您应该在 global.o 中将符号 Verbose 定义为初始值为 0 的 int:

> nm input.o | grep Verbose
0000000000000000 B Verbose
> nm set.o | grep Verbose
U Verbose

(nm 是一个 Linux 实用程序,用于列出目标文件中的符号。Windows 有 dumpbin。)

目标文件中没有ALLOCU表示该符号未定义,即在该目标文件中被引用但未定义。 input.o 中的 B 表示定义符号的部分。链接时,可以有许多 U,但对于目标文件中未定义的每个符号,您还需要一个定义该符号的目标文件。

关于c - 使用Makefile后出现 undefined reference 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22602847/

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