At times you may need to test some rather simple code. You could just use the commands in the shell which will soon become a pain as you try to clean and recompile the test code. Instead, a
Makefile would be handy.
Build over simplified
The process of creating native applications/libraries (in C/C++) essentially consists of the following.
- Compile each source file (
.cpp) to an object file (
- Link all the object files and dependent libraries
gcc -o test.o -c test.cpp
gcc -o test test.o -lstdc++
These compilation and link steps can be combined into a “script” called a
The Makefile shown below (and associated sources) can be accessed from GitHub https://github.com/cognitivewaves/Simple-Makefile
# Specify compiler CC=gcc # Specify linker LINK=gcc .PHONY : all all : app # Link the object files into a binary app : main.o $(LINK) -o app main.o -lstdc++ # Compile the source files into object files main.o : main.cpp $(CC) -c main.cpp -o main.o # Clean target .PHONY : clean clean : rm main.o app
As mentioned earlier, this is an extremely simple example. More details can be found at Makefiles and Visual Studio, especially if you are coming from a Visual Studio background.