0

式コンパイラを構築しようとしています。次の 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

これらのエラーを解決するためのアドバイスをくれるメンターはいますか? 事前にサンクス。

4

2 に答える 2

1

問題の少なくとも一部は、Makefile.

これ:

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

main.oに依存しているが、に依存してglobal.hいないと述べていますmain.c。つまり、 のエラーを修正してもmain.c、入力しても再コンパイルされず、古いバージョンのからコンパイルされmakeた既存の と再リンクしようとします。main.omain.c

それぞれが対応する に依存するMakefileように修正します。foo.ofoo.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

古いスタイルの定義を置き換えても、発生している問題が解決されない可能性があります。また、古いスタイルのコードを受け入れるコンパイラを使用している場合は、必ずしも必要ではないかもしれませんが、コードの保守と追跡が容易になります。問題。たとえば、古いスタイルの定義では、間違った数の引数で関数を呼び出しても、コンパイラは文句を言いません。プロトタイプを使えば、そうなるでしょう。

于 2013-11-10T08:51:59.537 に答える
1

main の定義の前に関数のプロトタイプを追加する必要があります (特に C99 以降の場合)。

于 2013-11-10T08:23:21.407 に答える