3

I'm writing a makefile and I can't figure out how to include all my source files without having to write all source file I want to use. Here is the makefile I'm currently using:

GCC = $(GNUARM_HOME)\bin\arm-elf-gcc.exe
SOURCES=ShapeApp.cpp Square.cpp Circle.cpp Shape.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=hello
all: $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS) 
#$(CC) $(LDFLAGS) $(OBJECTS) -o $@
.cpp.o:
 $(GCC) -c $< -o $@

How do I automatically add new source file without having to add it to the sources line?

Jonathan Leffler
759k145 gold badges961 silver badges1.3k bronze badges
asked Mar 20, 2010 at 16:46
1
  • 1
    Outside of toy projects, you do not want to do this very often. You get into trouble the first time you make a backup copy of a file, or leave a bit of test code lying around. Not listing exactly the modules that make up the program is a recipe for trouble. If each file was a complete self-contained program, which is what Dirk suggested, it is more sensible. Commented Mar 20, 2010 at 17:40

1 Answer 1

1

Here is something I have used in an examples/ directory where each file is mapped one-to-one to an executable:

sources := $(wildcard *.cpp)
programs := $(sources:.cpp=)
[ more settings about compiler flags, linker options, ...]
all : $(programs)

That can be enough as make knows how to turn a .cpp file into an object file and then into an executable. Note that this is on Linux so for Windoze you'd probably need to do

programs := $(sources:.cpp=.exe)

to append the .exe.

answered Mar 20, 2010 at 17:02
Sign up to request clarification or add additional context in comments.

2 Comments

The OP only wants one executable. (Although he can probably figure out the rest from your answer)
Correct. All he needs is the sources statement.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.