简体   繁体   English

使用Makefile后出现未定义的参考错误

[英]Undefined reference error after using Makefile

I wrote a makefile to link all the .c files that i wanted to.But then again i get errors of undefined reference.The code for the makefile is: 我写了一个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}

But still it leads to undefined errors like : 但是仍然会导致未定义的错误,例如:

/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

All the undefined references are however present in 2 header files :1.stack.h 2.global.h..Still the errors.please help! 但是所有未定义的引用都存在于2个头文件中:1.stack.h 2.global.h ..仍然存在错误。请帮助!

The global.h file i am using is: 我正在使用的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

Your problem isn't related to the Makefile per se. 您的问题与Makefile本身无关。 The header-only include file global.h contains declarations of variables. 仅标头的包含文件global.h包含变量的声明。 These declarations have to be turned into actual definitions in one compilation unit, so that they have space for them allocated in one of your object files. 必须在一个编译单元中将这些声明转换为实际定义,以便在您的一个目标文件中为其分配空间。

The way your header file is designed, the definition of the variable ALLOC determines whether the variables are just declared or defined. 头文件的设计方式是,变量ALLOC的定义确定是声明变量还是定义变量。

The regular include without ALLOC yields for example: 不包含ALLOC收益的常规包括,例如:

extern int Verbose;

The extern keyword indicates that the variable Verbose is not defined. extern关键字指示未定义变量Verbose No memory is allocated for it (and it therefore can't have an initialiser) and that it is probably defined in another object file. 没有为其分配内存(因此它不能具有初始化程序),并且可能是在另一个目标文件中定义的。

When ALLOC is defined, the declaration becomes a definition with initialisation: 定义ALLOC ,该声明将成为具有初始化的定义:

int Verbose = 0;

Pick one of the files that include global.h and define ALLOC before including it, for example: 选择一个包含global.h的文件,并在包含它之前定义ALLOC ,例如:

input.c Input.c中

#define ALLOC
#include "global.h"

You should then have the symbol Verbose defined as an int with initial value 0 in global.o : 然后,应该在global.o符号Verbose定义为一个初始值为0的int

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

( nm is a Linux utility that lists the symbols in an object file. Windows has dumpbin .) nm是一个Linux实用程序,它列出了目标文件中的符号。Windows具有dumpbin 。)

The U in the object file without ALLOC means that the symbol is undefined, ie referenced but not defined in this object file. 没有ALLOC的目标文件中的U表示该符号未定义,即在该目标文件中已引用但未定义。 The B in input.o denotes the section where the symol is defined. input.oB表示定义input.o的部分。 When you link, you can have many U s, but for each symbol that is undefined in an object file, you also need one object file where the symbol is defined. 链接时,可以有多个U ,但是对于对象文件中未定义的每个符号,还需要一个用于定义符号的对象文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM