The present bash script help to create makefiles for small C/C++ projects. The syntax is the following
./newproj -compiler namewhere -compiler stands for
-gccto build a C project with gnu compiler collection-clangto build a C project with llvm compilers-g++to build a C++ project with gnu compiler collection-clang++to build a C++ project wht llvm compilers
The output is a folder named name and with the following files and subfolders:
src/: the source filesinclude/: the headersbuild/: the objects (and the building rules, see below)bin/: the executablemakefile: the makefile (see below).clang-format: the configuration file forclang-format
The makefiles generated by the the scripts have the following options
| variable | description | default |
|---|---|---|
TARGET |
name of executable | main |
HEADS |
header files (without extension) | |
SRC |
source files (without extension) | |
LIBS |
libraries (-l and -L) |
|
TDIR |
target directory | bin/ |
IDIR |
header directory | include/ |
BDIR |
object directory | build/ |
SDIR |
source directory | src/ |
HEADEXT |
header file extension | C: h, C++: hh |
SRCEXT |
source file extension | C: c, C++: cc |
OBJEXT |
object file extension | o |
TRGEXT |
target file extension | x |
RUNOPTIONS |
options argument to run target executable | |
C(XX) |
compiler (resp for C and C++) | see above |
C(XX)FLAGS |
compiler flags | -Wall -Wextra [-g (debug) or -O2 -s (release)] |
CPPFLAGS |
preprocessor flags | -I $(IDIR) -MMD -MP |
LDFLAGS |
linker flags | -$(LIBS) |
NOTE:
- the preprocessor flag
-I $(DIR)allows to write#include <file>instead of#include "file"even iffileis an header - the preprocessor flags
-MMD -MPmake the compiler generate additional dependency rules (as.dfiles) that specify additional file that makefile has to compile for consistency reason. Suppose that in a certainmysource.cis includedmyheader.h; thanks to these rules the filemysource.cis compiled even ifmyheader.his modified. - in the case of a default compilation debugging hooks are included
-g, while for a final release optimization-O2and strip-sis enabled
Here are the option of makefile:
| option | explanation |
|---|---|
none or all |
compiles the project |
clean |
removes the target, the objects and the rules |
run |
run the project with RUNOPTIONS |
format |
formats the code using clang-format with the settings of .clang-format |
release |
compiles the project without debug hooks and with optimization (see above) |
valgrind |
execute valgrind --leak-check=full --track-origin=yes $(TDIR)/$(TARGET) $(RUNOPTIONS) |