2017-07-13 19 views
-1

複数のファイルをコンパイルするmakefileを作成しようとしています。あなたはおそらくメイクファイルから言うことができるようにC複数のファイルを含むMakefile

CC = gcc 
CFLAGS = -ansi -Wall -g -O0 -Wwrite-strings -Wshadow \ 
    -pedantic-errors -fstack-protector-all 
PROGS = myprog.o test1 test2 test3 test4 \ 

all: $(PROGS) 

clean: 
     rm -f *.o $(PROGS) *.tmp 

$(PROGS): myprog.o 

myprog.o: prog-implementation.h myprog.c myprog.h 
    gcc $(CFLAGS) -c myprog.c 
memory_checker: memory_checker.o 
    gcc -o memory_checker memory_checker.o 
memory_checker.o: memory_checker.c memory_checker.h 
    gcc $(CFLAGS) -c memory_checker.c 
test1: test1.o 
    gcc -o test1 test1.o myprog.o 
test1.o: test1.c myprog.h myprog.c 
    gcc $(CFLAGS) -c test1.c myprog.c 
test2: test2.o 
    gcc -o test2 test2.o myprog.o 
test2.o: test2.c myprog.h 
    gcc $(CFLAGS) -c test2.c 
test3: test3.o 
    gcc -o test3 test3.o myprog.o 
test3.o: test3.c myprog.h 
    gcc $(CFLAGS) -c test3.c 
test4: test4.o 
    gcc -o test4 test4.o myprog.o memory_checker.c 
test4.o: test4.c myprog.h memory_checker.h 
    gcc $(CFLAGS) -c test4.c 

は、メインコードがmyprog.cであり、myprog.hとPROG-implementation.hが含まれています。ここに私の試みです。私はまた、私のコードのための4つのテストを持って、最後にメモリを確保するためのチェックを持っているすべての動的に割り当てられたメモリを解放します。テストはmain()メソッドが* .cファイルであるところです。

私はメイクファイルを使用しようとするたびに、それは私にエラー

make: Circular myprog.o <- myprog.o dependency dropped. 
make: Nothing to be done for `all'. 

を与える私のidentsのすべてが、タブではなくスペースであるので、それは問題ではありません。私はまた、私のコードに大きな問題がないことをかなり確信しているので、makefileは私の問題でなければなりません。どんな助けでも大歓迎です。

+0

エラーを探してみましたか?私は[この投稿](https://stackoverflow.com/questions/21324697/makefile-circular-dependency-dropped)を見つけました。それは面白そう! –

+0

'$(PROGS)'を 'myprog.o'に依存するように定義したようですが、' PROGS'もその依存関係を持っています。ちょうど推測。これは複雑なことです。 makeはすべてのオブジェクトファイルをワイルドカードを使ってすべてのCファイルに関して定義することができます。チュートリアルを探してみてください。 – jiveturkey

+0

学習目的でない場合は、Makefileを手動で使用しないでください。 cmakeのような最新のツールを使用してください。 –

答えて

1

このエラーメッセージはかなり明確にする必要があり:

make: Circular myprog.o <- myprog.o dependency dropped. 

それは、myprog.omyprog.oに依存していることを述べています。すなわち、myprog.oを作るには、最初にmyprog.oにする必要があります。これは論理エラーです。それはあなたのMakefileでこの部分である理由:

PROGS = myprog.o test1 test2 test3 test4 \ 

....  

$(PROGS): myprog.o 

2行目は、すべてのPROGSmyprog.oに依存していることを述べています。 PROGSのいずれかがmyprog.oであるため、現在はそれ自身に依存しています。

修正するには、PROGSのリストからmyprog.oを削除します。

+0

ありがとう!それを修正した –

0

これは、多くのエラーや不要な複雑性を持っているので、私はちょうど(それはあなたがGNUのを作るを使用すると仮定し)、あなたにいくつかのコメントとのより良い解決策を提示します:

CC := gcc 
CFLAGS := -ansi -Wall -g -O0 -Wwrite-strings -Wshadow \ 
    -pedantic-errors -fstack-protector-all 

PROGS := test1 test2 test3 test4 

# check these lists of object files needed for each binary, I extracted 
# it from your rules in the question: 
test1_OBJS := test1.o myprog.o 
test2_OBJS := test2.o 
test3_OBJS := test3.o myprog.o 
test4_OBJS := test4.o myprog.o memory_checker.o 
OBJS := $(sort $(test1_OBJS) $(test2_OBJS) $(test3_OBJS) $(test4_OBJS)) 

all: $(PROGS) 

clean: 
    rm -f *.o *.d $(PROGS) *.tmp 

# the following rules link your final targets from the object files 
# specified in the variables above: 

test1: $(test1_OBJS) 
    $(CC) [email protected] $^ 

test2: $(test2_OBJS) 
    $(CC) [email protected] $^ 

test3: $(test3_OBJS) 
    $(CC) [email protected] $^ 

test4: $(test4_OBJS) 
    $(CC) [email protected] $^ 

# this automatically generates dependency files (with your headers) 
%.d: %.c 
    $(CC) -MM -MT"[email protected] $(@:.d=.o)" [email protected] $(CFLAGS) $< 

# and this includes them 
ifneq ($(MAKECMDGOALS),clean) 
-include $(OBJS:.o=.d) 
endif 

# finally, all you need is a pattern rule compiling your object files: 

%.o: %.c 
    $(CC) -c [email protected] $(CFLAGS) $< 

.PHONY: all clean 
+0

あなたの提案したメイクファイルを使ってみましたが、 "all 'のために何もしない"というエラーがありました。 –

+0

あなたのソースファイル( 'test1.c'、' myprog.c'など)は現在のディレクトリ、あなたはありませんか? –

+0

はい、私はすべてのファイルがカレントディレクトリにあります –

関連する問題