开发者

How do you automate a make file?

How do you make a makefile go through a list of .cpp files and compile then separately, just like this does statically?

test: test.o
        g++ -o test test.o开发者_如何学编程
test.o: test.cc test.hh
        g++ -c test.cc

but dynamically from this?

SOURCES = main.cpp someClass.cpp otherFile.cpp


OBJECTS = main.o someClass.o otherFile.o

all: $OBJECTS 
%.o: %.cc
   g++ $< -c -o $@

If you want to enforce like-named headers for each module, too, then you can:

OBJECTS = main.o someClass.o otherFile.o

all: $OBJECTS 
%.o: %.cc %.hh
   g++ $< -c -o $@


automake makes this even easier:

program_SOURCES = main.cpp someClass.cpp otherFile.cpp

will build program from the listed source files. The only problem is that it works in conjunction with autoconf, which may take some time to set up properly. (At least, I never get it right the first time.)


You could also try:

SOURCES=$(shell ls *.cpp)
OBJECTS=$(SOURCES:%.cpp=%.o)

%.o: %.cpp
    g++ -c %< -o $@

This way you don't have to specify any cpp names. Any new source files you add will automatically be picked up. However, this would only work in Unix shells.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜