minimum c++ make file for linux
Solution 1
If it is a single file, you can type
make t
And it will invoke
g++ t.cpp -o t
This doesn't even require a Makefile in the directory, although it will get confused if you have a t.cpp and a t.c and a t.java, etc etc.
Also a real Makefile:
SOURCES := t.cpp
# Objs are all the sources, with .cpp replaced by .o
OBJS := $(SOURCES:.cpp=.o)
all: t
# Compile the binary 't' by calling the compiler with cflags, lflags, and any libs (if defined) and the list of objects.
t: $(OBJS)
$(CC) $(CFLAGS) -o t $(OBJS) $(LFLAGS) $(LIBS)
# Get a .o from a .cpp by calling compiler with cflags and includes (if defined)
.cpp.o:
$(CC) $(CFLAGS) $(INCLUDES) -c $<
Solution 2
Here is a generic makefile from my code snippets directory:
SOURCES=$(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
DEPS=$(SOURCES:.cpp=.d)
BINS=$(SOURCES:.cpp=)
CFLAGS+=-MMD
CXXFLAGS+=-MMD
all: $(BINS)
.PHONY: clean
clean:
$(RM) $(OBJECTS) $(DEPS) $(BINS)
-include $(DEPS)
As long as you have one .cpp source producing one binary, you don't need anything more. I have only used it with GNU make, and the dependency generation uses gcc syntax (also supported by icc). If you are using the SUN compilers, you need to change "-MMD" to "-xMMD". Also, ensure that the tab on the start of the line after clean:
does not get changed to spaces when you paste this code or make
will give you a missing separator error.
Solution 3
Have you looked at SCons?
Simply create a SConstruct file with the following:
Program("t.cpp")
Then type:
scons
Done!
Solution 4
Assuming no preconfigured system-wide make
settings:
CXX = g++
CPPFLAGS = # put pre-processor settings (-I, -D, etc) here
CXXFLAGS = -Wall # put compiler settings here
LDFLAGS = # put linker settings here
test: test.o
$(CXX) -o $@ $(CXXFLAGS) $(LDFLAGS) test.o
.cpp.o:
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $<
test.cpp: test.h
Solution 5
a fairly small GNU Makefile, using predefined rules and auto-deps:
CC=c++
CXXFLAGS=-g -Wall -Wextra -MMD
LDLIBS=-lm
program: program.o sub.o
clean:
$(RM) *.o *.d program
-include $(wildcard *.d)
Related videos on Youtube
RichieHH
Updated on January 28, 2020Comments
-
RichieHH about 4 years
I've looking to find a simple recommended "minimal" c++ makefile for linux which will use g++ to compile and link a single file and h file. Ideally the make file will not even have the physical file names in it and only have a .cpp to .o transform. What is the best way to generate such a makefile without diving into the horrors of autoconf?
The current dir contains, for example
t.cpp t.h
and I want a makefile for that to be created. I tried autoconf but its assuming .h is gcc instead of g++. Yes, while not a beginner, I am relearning from years ago best approaches to project manipulation and hence am looking for automated ways to create and maintain makefiles for small projects.
-
Martin York over 15 yearsMake is great for simple projects (and for playing with). But maintaining a big project becomes difficult to do correctly (you can hodge podge it easily but correctly is hard). Use tools 'like scons' to build your make file
-
RichieHH over 15 yearsscons looks nice. Certainly easier than Autoconf.
-
bltxd over 15 yearsOMake is great and takes over when make shows its limits.
-
-
David Nehme over 15 yearsthis also doesn't create any dependencies.
-
Martin York over 15 yearsThough I love fiddling with makefile (its a language unto itself). I think this is the best answer, but I think you need to expand this answer a bit to explain why using "SCons" is better than using Makefiles directly. I am convinced but there is not enough here to convice others.
-
RichieHH over 15 yearsBut why do you have specific rule for t: ? It kind of defeats the point of having a cpp.o rule.
-
RichieHH over 15 yearsAgain this his a specific line for test. Why?
-
hazzen over 15 yearsThe t rule generates, from the .o files, the binary by calling the linker. If I had four sources [abcd].cpp and a binary named t, the rule would link all four of those generated objects into one executable.
-
Alnitak over 15 yearsbecause you always have to specify what the Makefile will actually build. With suitable global defs, you might just be able to say just "test:" in the file, but the file above is guaranteed to work regardless of any global rules or macros.
-
JesperE over 15 yearsIf OMake had better support for out-of-source builds and could generate Visual Studio project, it would be very close to perfect.
-
bltxd over 15 yearsWe do out-of-source builds with OMake here. There are a few things to know but otherwise it works (0.9.8.5).
-
jdkoftinoff about 9 yearsOops! Sorry for the broken links. I moved it to github. github.com/jdkoftinoff/magicmake - that being said I use CMake now instead.
-
KRoy about 6 years
all
should bephony
too . -
Daniel Stevens over 5 yearsYou should use
CXX
andCXXFLAGS
for C++ files, rather thanCC
andCFLAGS
. Source: GNU make implicit variables. Note thatCPPFLAGS
are for the C Pre-Processor, which applies to both C and C++. -
Daniel Stevens over 5 years