Creating a Simple Makefile

If you are sharing a C/C++ code with someone else, it is better to provide them a how to compile your code. But sometimes it takes so long!

A Makefile helps you compiling programs and building executables, object code, libraries, etc. For this we should have a file called Makefile that includes rules showing how to compile the program. Then we just type "make" and the "make" utility compiles the program via rules stated in the the "Makefile" file.

Then, let's create a simple file named "Makefile" and let others to compile your code via just typing $>make to the console.

Here is the inside of Makefile:

# Specifies the files that are included in the build process.
# e.g. server.c and client.c
all: server client


# Specify how to compile server.c
server: server.c
gcc -Wall -g -lm -o server server.c

# Specify how to compile client.c
client: client.c
gcc -Wall -g -lm -o client client.c

# -g option is required for debugging
# -Wall causes all compiler warnings to be shown
# -lm links with math library in case you would like to use a math function

# Specify a clean command to clean your build code or any
# temporary data for fresh start.
# You should type "make clean" in order to remove the
# files specified above, such as the executable.
clean:
rm -fr client server *~ core*

Comments

Popular posts from this blog

3 Must Haves of a Status Report

AWS encryption chart (SSE-S3 vs SSE-KMS vs SSE-C)

Curl "No status code specified in the response"