Im fairly new to makefiles. I want to compile multiple executables through my makefile, and it was to my understanding that having a target with multiple entries would run the recipe for that target through all entries. My example is:
$(EXE): $(OBJS)
g++ -o $@ $< -L$(LIBPATH) -lOSApi -lrt -lpthread
My EXE variable contains all files that should be created, something like: prog1 prog2
and so on. My OBJS contains prog1.o prog2.o
and so on.
When running make i create all .o files perfectly, but i only create one executable. I have tried replacing $@
with $(EXE)
and such, but no luck so far.
Any help would be appreciated.
EDIT:
I found the solution through MadScientist, who suggested to add an all
target, and then changing my executable target to:
$(EXE): % : %.o
g++ -o $@ $< -L$(LIBPATH) -lOSApi -lrt -lpthread
.PHONY: all clean
all: $(EXE)
Which to my understanding makes every target in my EXE
target dependant on its corresponding .o file.
It would help greatly if you provided a full (small) sample. In the question you don't show us what the definition of EXE
or OBJS
is which makes it hard to say exactly.
Also, please be sure to format your question correctly.
By default make only builds the FIRST target in the makefile. It doesn't build ALL the targets in the makefile. So, if EXE
contains multiple targets and the first rule in your makefile is $(EXE) : ...
then only the first target in that list will be built.
You should add a new target before the above, saying that you want the default to build all the exe's. You can call it anything you like but the convention is to call it all
:
all: $(EXES)
(you can also add a .PHONY: all
for safety). Now the first target in the makefile is all
, and as prerequisites it will build all the targets in the EXES
variable.