简体   繁体   English

未定义的引用而不是makefile

[英]Undefined reference instead of makefile

The code i want to run has the makefile and it shows the error: 我要运行的代码具有makefile,并且显示错误:

/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o: In function `_start':(.text+0x20): undefined reference to `main'
 collect2: error: ld returned 1 exit status
  make: *** [Nfa] Error 1

The file with the main function is terp.c. 具有主要功能的文件是terp.c。
The part of the code with main() is: main()的代码部分是:

#ifdef MAIN
#define ALLOCATE
#include "global.h" /*externs for Verbose*/

#define SIZE 256

PRIVATE char BUf[BSIZE] //input buffer
PRIVATE char *Pbuf=BUf; //current position in input buffer
PRIVATE char *Expr; //regular expression from command Line

...

Skipping some code here until main... 在这里跳过一些代码,直到主要...

void main (int argc,char *argv[])
{
 int sstate;    //Starting NFA state
 SET *start_dfastate;//Set of starting DFA states
 SET *current;  //current DFA state
 SET *next; 
 int accept;    //current Dfa state is an accept
 int c;     //current input character 
 int anchor;

 if (argc==2)
     fprintf(stderr,"Expression is %s\n",argv[1]);
 else
 {
     fprintf(stderr,"Usage:terp pattern < input\n");
     exit(1);
 }
 //Compile the NFA create the initial state,and initialize the current state to the  start state    
Expr=argv[1];
 sstate=nfa(getline);
 next=newset();
 ADD(next,sstate);
 if (!(start_dfastate=e_closure(next,&accept,&anchor)))
 {
     fprintf(stderr,"Internal error:State machine is empty\n");
     exit(1);
 }
 current=newset();
 assign(current,start_dfastate);

 while (c=nextchar())
 {
     if (next=e_closure(move(current,c),&accept,&anchor))
     {
         if (accept)
             printbuf();
         else
         {
            delset(current);
            current=next;
            continue;
        }
    }
    delset(next);
    assign(current,start_dfastate);
}
}

 #endif

The makefile i am using: 我正在使用的makefile:

 FILES.o=set.o hash.o printnfa.o input.o nfa.o terp.o assort.o prnt.o printv.o bintoasc.o ferr.o onferr.o fputstr.o pchar.o driver.o searchenv.o hashadd.o esc.o 

 PROGRAM= Nfa
 INC := -I./debug.h -I./global.h 

 all: ${PROGRAM}

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

Since your first line is: 由于您的第一行是:

#ifdef MAIN

I would say you need to define that when compiling. 我会说您需要在编译时定义它。
Use -DMAIN as a preprocessor option for gcc in the makefile (you can put this line below the INC line): -DMAIN用作makefile gcc的预处理程序选项(可以将此行放在INC行下方):

CFLAGS=-DMAIN

This way, it will be included when the compiler is actually called: 这样,将在实际调用编译器时将其包括在内:

${CC} -o $@ ${CFLAGS} $(INC) $^ ${LDFLAGS} ${LDLIBS}
                ▲
                ║ 
                ╚═══ This will include the `MAIN` definition for compiling

The other option is to remove the #ifdef MAIN alltogether. 另一个选择是一起删除#ifdef MAIN Don't forget the remove the corresponding #endif from the end of the file. 不要忘记从文件末尾删除相应的#endif

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

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