makefile制作.gch文件而不是.o文件的问题

时间:2009-09-25 03:53:27

标签: c++ makefile precompiled-headers

所以,我正在制作一个程序来测试某些数据结构的效率。我有所有的.h文件,我制作了一个非常可怕的makefile,可能是错的,虽然它似乎达到了一定程度。它不是制作.o文件而是制作.gch文件,所以当它试图访问所有.o文件时,它们都找不到。这是我的makefile

prog1: main.o dsexceptions.o BinarySearchTree.o SplayTree.o RedBlackTree.o AvlTree.o
                g++ -Wall -g -o prog1 main.o dsexceptions.h.gch BinarySearchTree.h.gch SplayTree.h.gch RedBlackTree.h.gch AvlTree.h.gch

main.o: main.cpp AvlTree.h RedBlackTree.h SplayTree.h BinarySearchTree.h dsexceptions.h
                g++ -Wall -g -c main.cpp

#shape.o: shape.cpp shape.h grid.h
#               g++ -Wall -g -c shape.cpp

dsexceptions.o: dsexceptions.h
                g++ -Wall -g -c dsexceptions.h

BinarySearchTree.o: BinarySearchTree.h dsexceptions.h
                    g++ -Wall -g -c BinarySearchTree.h

SplayTree.o: SplayTree.h dsexceptions.h
             g++ -Wall -g -c SplayTree.h

RedBlackTree.o: RedBlackTree.h dsexceptions.h
                g++ -Wall -g -c RedBlackTree.h

AvlTree.o: AvlTree.h dsexceptions.h
           g++ -Wall -g -c AvlTree.h

clean:
                rm -f main main.exe  main.o dsexceptions.o BinarySearchTree.o SplayTree.o RedBlackTree.o AvlTree.o *.gch

3 个答案:

答案 0 :(得分:18)

您不希望将.h文件提供给编译器。仅编译.cpp文件,该文件应包含.h文件。 (.gch文件是预编译的头文件。)您的头文件不需要.o文件,只需在.cpp文件中#include它们。

prog1: main.o
        g++ -Wall -g -o prog1 main.o

main.o: main.cpp AvlTree.h RedBlackTree.h SplayTree.h BinarySearchTree.h dsexceptions.h
        g++ -Wall -g -c main.cpp

clean:
        rm -f prog1 main.o

答案 1 :(得分:1)

你已经有了bstpierre的解决方案,但只是为了好玩,这是我的makefile版本:

CC = g++ -Wall -g -o $@

MODULE = AvlTree BinarySearchTree RedBlackTree SplayTree
OBJECTS = $(addsuffix .o,$(MODULES))

prog1: main.o dsexceptions.o $(OBJECTS)
       $(CC) $^ 

main.o: $(addsuffix .h,$(MODULES))

$(OBJECTS) main.o : %.cpp %.h dsexceptions.h
    $(CC) -c $<

clean:
 rm -f main main.exe *.o *.gch

答案 2 :(得分:1)

只是为了好的衡量,这是我的SConstruct,因为SCons好得多:)

Program('main.cpp') # Yeah, it's that simple :)

您可以查看SCons here