gpt4 book ai didi

c - undefined reference 而不是 makefile

转载 作者:太空宇宙 更新时间:2023-11-04 07:19:01 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com