0

Makefile:

all: a.out

a.out: b.o a.o 
    gcc -o b.o a.o

a.o: a.c 
    gcc -c a.c

b.o: b.c 
    gcc -c b.c

.PHONY:clean

clean:
    rm *.o a.out

with make, give information:

error: undefined reference to 'main'

collect2: ld return 1

make: * [a.out] error 1

But when put source file a.c and b.c into Eclipse CDT, it compiles well. Please explain what is wrong with my makefile?

PS: a.c:

int global_1 = 100;

b.c:

extern int global_1;

int main()
{
    int global_2 = global_1 * 2;
    return 0;
}
4

2 に答える 2

1

このルールは、正しい結果ファイルを指定していません:

a.out: b.o a.o 
    gcc -o b.o a.o

そのはず

a.out: b.o a.o 
    gcc -o "$@" b.o a.o

これは、 Paul の Makefile の規則 の1 つに違反した場合に得られるものです。ところで、「エラーが発生する」のはメイクファイルではなく、コンパイル、特にリンカーです。

于 2013-09-06T14:29:53.123 に答える
0

これ:

a.out: b.o a.o 
    gcc -o b.o a.o
        \----/
           |
           |
     this says "name
     the output b.o"

b.oオブジェクトファイルを出力ファイルで上書きしています。make入力なしで必要なコマンドを理解するのに十分賢いと確信しているので、2行目を削除してみてください。

于 2013-09-06T14:29:31.063 に答える