未定义的引用而不是makefile

我想运行的代码有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 

具有main函数的文件是terp.c.
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 ... 

在此处跳过一些代码直到主…

 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 

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

由于你的第一行是:

 #ifdef MAIN 

我想说你需要在编译时定义它。
使用-DMAIN作为makefile gcc的预处理器选项(可以将此行放在INC行下面):

 CFLAGS=-DMAIN 

这样,它将在实际调用编译器时包含:

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

另一种选择是完全删除#ifdef MAIN 。 不要忘记从文件末尾删除相应的#endif