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.
精彩评论