簡體   English   中英

未定義引用已定義的函數

[英]Undefined Reference to a function which is defined already

我正在嘗試構建一個表達式編譯器。 當我嘗試編譯以下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

任何導師會建議我解決這些錯誤嗎? Thanx提前。

您應該在main的定義之前添加函數的原型(特別是對於C99和后者)。

至少部分問題出在Makefile

這個:

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

main.o依賴於global.h ,但不能在main.c 這意味着即使你更正了main.c錯誤,輸入make也不會重新編譯它,它會嘗試重新鏈接從版本的main.c編譯的現有main.o

修復你的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

替換舊式定義可能無法解決您所看到的問題,如果您使用的是接受舊式代碼的編譯器,則可能不是完全必要的,但它會使代碼更易於維護和跟蹤任何問題。 例如,對於舊式定義,如果使用錯誤數量的參數調用函數,編譯器將不會抱怨; 原型,它會。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM