使用Makefile后未定义的引用错误

时间:2014-03-24 06:57:13

标签: c makefile

我写了一个makefile来链接我想要的所有.c文件。但是我又得到了未定义引用的错误.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

然而,所有未定义的引用都出现在2个头文件中:1.stack.h 2.global.h..Still errors.please help!

我正在使用的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

2 个答案:

答案 0 :(得分:1)

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

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

常规包括没有ALLOC收益率,例如:

extern int Verbose;

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

当定义ALLOC时,声明成为初始化的定义:

int Verbose = 0;

选择其中一个包含global.h的文件并在包含ALLOC之前定义#define ALLOC #include "global.h" ,例如:

<强> Input.c中

Verbose

然后,您应该在int中将符号global.o定义为> nm input.o | grep Verbose 0000000000000000 B Verbose > nm set.o | grep Verbose U Verbose ,初始值为0:

nm

dumpbin是一个Linux实用程序,它列出了目标文件中的符号.Windows有U。)

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

答案 1 :(得分:0)

你应该在makefile中包含的所有文件中添加头文件... 否则这些文件将无法识别这些变量