It’s difficult to know what’s best for you without knowing exactly what you’re trying to compile. What you are showing
looks suspiciously Window-based (e.g. *.cpp files and your_program_exe) but otherwise it’s mere guesswork.
If you are trying to compile an executable from C++ files then the makefile job is very simple with two stages:
- Compile all each source C++ file to an object.
- Link all objects with library dependencies and the main object to create an executable.
If you are compiling a library, it’s a little different but I suspect you are not.
So here’s one useful tutorial with examples:
http://makepp.sourceforge.net/1.19/makepp_tutorial.html
For example, here’s a (completely untested) example generic C++ makefile (the file must be called `makefile’) (for
C++11) compiling src/.cc files using headers contained in include/, and library dependencies in lib/ (which archive
libarchive.a). Object builds are compiled to build/.o and linked to bin/a.out:
CC := g++
SRCDIR := src
BUILDDIR := build
BINDIR := bin
TARGET := bin/a.out
SRCEXT := cc
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
CFLAGS := -std=c++11
LIB := -L lib -larchive
INC := -I include -I build
$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
@mkdir -p $(BUILDDIR)
@echo " $(CC) $(CFLAGS) $(INC) -c -o $@ $<"; $(CC) $(CFLAGS) $(INC) -c -o $@ $<
$(TARGET): $(OBJECTS)
@mkdir -p $(BINDIR)
@echo " $(CC) $^ -o $(TARGET) $(LIB)"; $(CC) $^ -o $(TARGET) $(LIB)
This makefile is based on a template from:
http://hiltmon.com/blog/2013/07/03/a-simple-c-plus-plus-project-structure/
If you want to call your makefile hellomake', you can, _but_ rather than invoking
make’ you must invoke `make -f
hellomake’.
The other thing is - do you need as something as sophisticated as make? If you’re just compiling 7 C++ sources and only
linking to -lSDL2 you can shell-script that in two lines e.g.
sh-4.3$ g++ -c -std=c++11 file1.cpp file2.cpp file3.cpp file4.cpp file5.cpp file6.cpp file7.cpp
sh-4.3$ g++ -std=c++11 main.cpp file1.o file2.o file3.o file4.o file5.o file6.o -o executable.run -lSDL2 -L/path/to/libSDL2.object_or_archive
> Code:
> --------------------
> jeff@linux-3bv9:~/noob> make
> g++ main.cpp graphics.cpp graphics.h input.cpp input.h thing.cpp thing.h -o your_program_exe hellomake
> g++: error: hellomake: No such file or directory
> makefile:10: recipe for target ‘hellomake’ failed
> make: *** [hellomake] Error 1
> jeff@linux-3bv9:~/noob>
>
> --------------------
Are you trying to compile the makefile? If so, you need to go back to the tutorials and read them much more slowly. The
makefile is a specification file for what files to compile, how, and where. You can also create specifications for
linking and archiving objects.
HTH.