【问题标题】:Undefined Reference to a function which is defined already未定义对已定义函数的引用
【发布时间】:2013-11-10 08:19:10
【问题描述】:

我正在尝试构建一个表达式编译器。 当我尝试编译我的以下 main.c 时,它给了我以下错误:

    cc -O -o compile error.o lex.o table.o main.o code.o
    main.o: In function `terms':
    main.c:(.text+0x1be): undefined reference to `popopand'
    main.c:(.text+0x1c5): undefined reference to `popopand'
    main.c:(.text+0x1e7): undefined reference to `popoptor'
    main.o: In function `term':
    main.c:(.text+0x26b): undefined reference to `factoor'
    main.o: In function `expresses':
    main.c:(.text+0x302): undefined reference to `popopand'
    main.c:(.text+0x309): undefined reference to `popopand'
    main.c:(.text+0x32b): undefined reference to `popoptor'
    main.o: In function `stmt':
    main.c:(.text+0x4cf): undefined reference to `popopand'
    main.c:(.text+0x4d6): undefined reference to `popopand'
    main.c:(.text+0x4dd): undefined reference to `popoptor'
    main.o: In function `stmts':
    main.c:(.text+0x582): undefined reference to `eerror'
    collect2: ld returned 1 exit status
    make: *** [compile] Error 1

我的 main.c 如下代码:

#include "global.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define STACK 100

int opandstk[STACK],optorstk[STACK];
int topoptor=-1,topopand=-1;
int curtoken;

main(argc,argv) int argc;char *argv[];{

    char msg[BUFSIZ];
    char *sp;

    if(argc!=2){
        sprintf(msg,"usage: %s filename",argv[0]);
        error(msg,PANIC);
    }
    emit(TEXT,BLANK,BLANK);
    curtoken=yygettoken();
    if(stmts()){
        if(curtoken=='\n' || curtoken==EOFILE){
            curtoken=yygettoken();
            if(curtoken==EOFILE){
                emit(EXIT,insert("0"),BLANK);
                data();
            }
        }
    }
    else
        error("end of file or end of line expected.",PANIC);
    exit(0);
}
stmts(){
    if(curtoken=='\n' || curtoken==EOFILE)
        return 1;
    if(curtoken==IDENT){
        if(stmt())
            if(stmts())
                return 1;
    }
    error("end of file or end of line or identifier expected.",PANIC);
}
stmt(){
    int rhs;
    if(curtoken==IDENT){
        pushopand(lookup(yytext));
        curtoken=yygettoken();
        if(curtoken=='='){
            pushoptor(ASSIGN);
            curtoken=yygettoken();
            if(express()){
                if(curtoken==';'){
                    curtoken=yygettoken();
                    rhs=popopand();
                    emit(popoptor(),popopand(),rhs);
                    return 1;
                }
                error("';' expected.",PANIC);
            }

        }
        error("'=' expected.",PANIC);
    }
    error("identifier expected.",PANIC);
}
express(){
    if(curtoken==IDENT || curtoken==INTEGER || curtoken=='('){
        if(term() && expresses())
            return 1;
    }
    error("identifier,integer or '(' expected.",PANIC);
}
expresses(){
    int lhs,rhs,temp;
    if(curtoken==')' || curtoken==';')
        return 1;
    if(curtoken=='-' || curtoken=='+'){
        if(curtoken=='-')
            pushoptor(SUB);
        else
            pushoptor(ADD);
        curtoken=yygettoken();
        if (term()){
            rhs=popopand();
            lhs=popopand();
            temp=mktmp();
            emit(ASSIGN,temp,lhs);
            emit(popoptor(),temp,rhs);
            pushopand(temp);
            if(expresses())
                return 1;
        }
    }
    error("')',';','-' or '+'expected.",PANIC);
}
term(){
    if(curtoken==IDENT || curtoken==INTEGER || curtoken=='(')
        if(factor() && terms())
            return 1;
    error("identifier,integer or '(' expected.",PANIC);
}
terms(){
    int lhs,rhs,temp;
    if(curtoken=='/' || curtoken=='*'){
        if(curtoken=='/')
            pushoptor(DIV);
        else
            pushoptor(MULT);
        curtoken=yygettoken();
        if (factor()){
            rhs=popopand();
            lhs=popopand();
            temp=mktmp();
            emit(ASSIGN,temp,lhs);
            emit(popoptor(),temp,rhs);
            pushopand(temp);
            if(terms())
                return 1;
        }

    }
    else if(curtoken=='-' || curtoken=='+'|| curtoken==')' || curtoken==';')
        return 1;
    error("'/','*','-','+',';'or ')'expected.",PANIC);
}
factor(){
    if(curtoken=='('){
        curtoken=yygettoken();
        if (express()){
            if(curtoken==')'){
                curtoken=yygettoken();
                return 1;
            }
            error("')' expected",PANIC);
        }

    }
    if(curtoken==INTEGER || curtoken==IDENT){
        pushopand(lookup(yytext));
        curtoken=yygettoken();
        return 1;
    }
    error("'(',integer or identifier expected",PANIC);
}
pushopand(i) int i;{
    if(++topopand==STACK)
        error("internal error: operand stack overflow",PANIC);
    opandstk[topopand]=i;
}
int 
popopand(){
    if(topopand==-1)
        error("internal error: operand stack underflow",PANIC);
    return (opandstk[topopand--]);
}
pushoptor(i) int i;{
    if(++topoptor==STACK)
        error("internal error: operator stack overflow",PANIC);
    opandstk[topoptor]=i;
}
int 
popoptor(){
    if(topoptor==-1)
        error("internal error: operator stack underflow",PANIC);
    return (opandstk[topoptor--]);
}
int 
mktmp(){
    static int seed=0;
    char name[BUFSIZ];
    sprintf(name,"-xxx%d",seed++);
    return (insert(name,NONLITERAL));
}

我的 Makefile 是:

compile: error.o lex.o table.o code.o main.o
    cc -O -o compile error.o lex.o table.o main.o code.o
main.o: global.h
    cc -c -O main.c
code.o: global.h
    cc -c -O code.c
error.o:global.h
    cc -c -O error.c    
lex.o:  global.h
    cc -c -O lex.c
table.o: global.h
    cc -c -O table.c

任何导师会建议我解决这些错误吗? 提前谢谢。

【问题讨论】:

  • @hacks ??? main() 就在 OP 发布的代码中...
  • @hacks:1980 年的代码是“正确的”......
  • @hacks 这在技术上是正确的,但强烈建议不要这样做。
  • @hacks:是的。就像 OP 不一致使用的隐式 int 返回类型一样。 user2015915,帮自己一个忙,为您的函数使用适当的原型,将编译器的警告打开到最大级别并修复它不满意的任何内容。
  • 混合使用 ANSI 和 K&R 风格的函数定义可能会导致问题。

标签: c compiler-errors makefile


【解决方案1】:

至少部分问题出在您的Makefile

这个:

main.o: global.h
    cc -c -O main.c

main.o 依赖于global.h,但不依赖于main.c。这意味着即使您更正了main.c 中的错误,输入make 也不会重新编译它,它会尝试重新链接从 版本编译的现有main.o main.c.

修复您的Makefile,使每个foo.o 依赖于对应的foo.c

至于您的代码,它使用的是旧式函数定义,自 1989 年 ANSI C 标准以来已过时。它还在函数被声明或定义之前调用函数,这在 1999 ISO C 标准中是无效的(这将导致编译时警告或错误消息,而不是您看到的链接时错误)。

我要做的第一件事(在修复 Makefile 之后)是将所有定义更新为更现代的样式,并将原型添加到源文件的顶部,以便在调用之前声明所有内容。例如,改变这个:

main(argc,argv) int argc;char *argv[];{

到这里:

int main(int argc, char *argv[]) {

还有这个:

stmts(){

到这里:

void stmts(void) {

如果您使用 gcc(cc 通常是指向 gcc 的符号链接),请使用会产生更多警告的选项,例如 [g]cc -std=c99 -pedantic -Wall -Wextra

替换旧式定义可能无法解决您遇到的问题,如果您使用的是接受旧式代码的编译器,这可能不是完全必要的,但它会使代码更易于维护和追查任何问题。例如,对于旧式定义,如果您调用带有错误数量参数的函数,编译器不会报错;有了原型,它会的。

【讨论】:

    【解决方案2】:

    您应该在 main 定义之前添加函数的原型(特别是对于 C99 和更高版本)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 2020-02-10
      • 2021-06-08
      • 2022-05-03
      相关资源
      最近更新 更多