Reputation: 1819
So i have folder that looks like this
OS
Makefile
/include
head.h
/src
main.cpp
head.cpp
/objects
How can i use Makefile to compile all .cpp files to objects folder then compile all .o files with include .h to my actual cpp program.I currently have:
CPP_FILES := $(wildcard src/*.cpp)
H_FILES := $(wildcard include/*.h)
OBJ :=$(wildcard objects/*.o)
LIBS := -Wall
CC := -std=c++14
What do I enter next to make all those .cpp files to .o and compile them with .h included.Thank you for your time
Upvotes: 0
Views: 1407
Reputation: 8537
You can use pattern rules (previously known as "suffix rules") for that. As you use GNU make, you can write this line to compile all .cpp
files in src/
to .o
files in objects/
, assuming the Makefile
is placed in the top directory:
objects/%.o: src/%.cpp
$(CXX) -c $(CFLAGS) $< -o $@
In GNU make syntax $<
denotes the dependency (a .cpp file in this case), $@
denotes the target (the object file).
The .h
files do not need to be compiled, just set up the correct include path as part of the string the CFLAGS variable contains:
CFLAGS += -I./include
Upvotes: 2