Will03uk
Will03uk

Reputation: 3444

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

Upvotes: 1

Views: 136

Answers (3)

Sagar
Sagar

Reputation: 9503

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.

Upvotes: 0

Fred Foo
Fred Foo

Reputation: 363817

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.)

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385395

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 $@

Upvotes: 2

Related Questions