c - undefined reference 而不是 makefile

标签 c 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

我正在使用的生成文件:

 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

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

CFLAGS=-DMAIN

这样,编译器实际调用的时候就会被包含进来:

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

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

关于c - undefined reference 而不是 makefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22611696/

相关文章:

c - 为 Windows 编写正确的 Ansi C makefile

c - 在我的 makefile 中设置我的 LD_LIBRARY_PATH 有问题

linux - 在 Linux 上运行 openwebrtc 应用程序

bash - 自动运行端口转发的脚本

c - 为什么 less 改变输出顺序?

c - 为什么我每次使用 malloc 都会收到警告?

c - 在 C 中实现一个 shell

c - 如何使用 2 个应用程序同时连接两个智能卡读卡器

c - `({...})` 是如何返回值的?

makefile - 使用clearmake时,MAKEFILE_LIST是一个空列表