4

makefileを取得して、実行が必要なファイルをコンパイルしようとしました-std=c99。この場合、「forループ」を通過させます。

これは私のコードです(「$(CC)」の前の「タブ」で使用されています):

CC = gcc
CFLAGS = -c -std=c99

...

Download.o : Download.c
    $(CC) $(CFLAGS) Download.c

Download.cには、Webから要素をダウンロードするために使用されるメソッドが含まれています

エラーメッセージ

$ make
gcc -c -std=c99 Download.c
gcc Download.c -o Program
Download.c: In function ‘downloadImageparts’:
Download.c:11:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
Download.c:11:2: note: use option -std=c99 or -std=gnu99 to compile your code
Download.c:13:3: error: ‘for’ loop initial declarations are only allowed in C99 mode
make: *** [comp] Error 1

デバッグを試みる

gcc -c -std=c99 Download.cターミナルで実行すると、正常に動作します。

この問題は、Linuxで実行すると発生します。

解決済み:

問題を解決するために、講師に見せるためのダミープロジェクトを作成しました。ダミープロジェクトでは、説明されているコードですべて正常に機能します。何らかの理由で、私のコードはその場で機能しますが、他の場所では機能しません。これを読んでいる人が私と同じ問題を抱えていて、サンプルプロジェクトを見たい場合。私に知らせてください、そして私はここにコードを書きます。ありがとう

4

2 に答える 2

11

あなたは間違ったルールを見ています。 Download.c実際には正常にコンパイルされていますが、リンク段階が間違っています。

$ make
gcc -c -std = c99 Download.c   #コンパイル
gcc Download.c -oプログラム   #リンク

プログラムをリンクするmakeルールを修正します。おそらく次のようになります。

Program: a.o b.o c.o
    $(CC) $(LDFLAGS) -o $@ $^ $(LIBS)

あなたがそれにいる間、私はより完全なMakefileが次のようになることを提案します:

all: Program
clean:
    rm -f Program *.o
.PHONY: all clean

# -c is implicit, you don't need it (it *shouldn't* be there)
# CC is also implicit, you don't need it
CFLAGS := -std=c99 -g -Wall -Wextra

Program: a.o b.o c.o
    $(CC) $(LDFLAGS) -o $@ $^ $(LIBS)

# Make will automatically generate the necessary commands
# You just need to name which headers each file depends on
# (You can make the dependencies automatic but this is simpler)
a.o: a.c header.h
b.o: b.c header.h header2.h
c.o: c.c header.h

それを間違える方法の例

リンカーフラグは実際にはかなり扱いにくいです!必ず上記の行を私が書いたとおりに入力してください。あなたが書いたものが同等であると思い込まないでください。間違っているため使用してはならないわずかに異なるコマンドの例を次に示します。

# WRONG: program must depend on *.o files, NOT *.c files
Program: a.c b.c c.c
    $(CC) ...

# WRONG: -c should not be in CFLAGS
CFLAGS := -c -std=c99

Program: a.o b.o c.o
    # WRONG: $(CFLAGS) should not be here
    # you are NOT compiling, so they do not belong here
    $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)

    # WRONG: $(LIBS) MUST come at the end
    # otherwise linker may fail to find symbols
    $(CC) $(LDFLAGS) -o $@ $(LIBS) $^

    # WRONG: do not list *.o files, use $^ instead
    # otherwise it is easy to get typos here
    $(CC) $(LDFLAGS) -o $@ a.o b.o c.o $(LIBS)

    # WRONG: $(LDFLAGS) must be at the beginning
    # it only applies to what comes after, so you
    # MUST put it at the beginning
    $(CC) -o $@ $(LDFLAGS) $^ $(LIBS)

    # WRONG: -c flag disables linking
    # but we are trying to link!
    $(CC) $(LDFLAGS) -c -o $@ $^ $(LIBS)

    # WRONG: use $(CC), not gcc
    # Don't sabotage your ability to "make CC=clang" or "make CC=gcc-4.7"
    gcc $(LDFLAGS) -o $@ $^ $(LIBS)

    # WRONG: ld does not include libc by default!
    ld $(LDFLAGS) -o $@ $^ $(LIBS)
于 2012-11-11T04:15:15.480 に答える
0

makefile内でタブの代わりにスペースを使用した場合も同じ結果が表示され、の出力はmakeルールが使用されていないことを示しています。

$ make
cc    -c -o Download.o Download.c
Download.c: In function ‘main’:
Download.c:4:3: error: ‘for’ loop initial declarations are only allowed in C99 mode
Download.c:4:3: note: use option -std=c99 or -std=gnu99 to compile your code
make: *** [Download.o] Error 1

tabで始まる行の前にaを試してくださいgcc


元の質問への更新を見た後:

$ make
gcc -c -std=c99 Download.c
gcc Download.c -o Program

最初の(コンパイル)行はエラーを示していません。失敗するのは、再コンパイル
する2行目です。Dietrich Eppが提案しているように、ファイル をリンクして実行可能ファイルをここに作成することをお勧めします。 Download.c
.oProgram

于 2012-11-11T03:52:38.983 に答える