Reusable example Makefile for C/C++ projects
This is an example project that demonstrates a simple C++ project structure with some modules written in C using a Makefile.
src
: Contains the.cpp
and.c
source files.include
: Contains the.hpp
and.h
header files.bin
: The output directory for the compiled binary.build
: The directory for object files.
makefile-example/
+-- src/
¦ +-- main.cpp
¦ +-- utils/
¦ ¦ +-- utils.c
¦ +-- helper.cpp
+-- include/
¦ +-- utils/
¦ ¦ +-- utils.h
¦ +-- helper.hpp
+-- bin/
+-- build/
+-- Makefile
- Ensure you have
gcc
andg++
installed. - Run
make
to build the project. - The executable will be created in the
bin/
directory. - Run
make run
to run the executable. - Run
make clean
to remove the built files.
$ git clone https://github.com/diffstorm/makefile-example.git
$ cd makefile-example
$ make -j4
mkdir -p build/utils/
mkdir -p build/
mkdir -p build/
gcc -Iinclude -O1 -Wall -c src/utils/utils.c -o build/utils/utils.o
g++ -Iinclude -O1 -Wall -c src/helper.cpp -o build/helper.o
g++ -Iinclude -O1 -Wall -c src/main.cpp -o build/main.o
g++ -Iinclude -O1 -Wall -o bin/project build/utils/utils.o build/helper.o build/main.o -lm
$ make run
Hello from C++ code!
This is a helper function from C++ code.
This is a utility function from C code.
$ make clean
rm -f -r build bin/project