Makefile Tutorial to Create Client-Server Program

Makefile is an iconic approach for compiling and building C/C++ programs. It’s mostly used by developers on Unix, Linux, and similar platforms. It aims to simplify the build process for program executables that may include several modules. In this Makefile tutorial, we’ll briefly discuss, what are its features and how to create a client-server program in C using Makefile.

In the first section of this post, we are covering the Makefile essentials so that you can easily use them in your projects. And then, there is the full source code of the client-server program written in C language. We’ve used socket programming concepts to implement this basic application. Since the objective of this Makefile tutorial is to train you in using make as a build tool, so we’ll create a Makefile to build the socket program.

Before you dive in further to read the post, it’s important to know the target platform version and the tools specification used.

OS Used: Linux Ubuntu

OS Version: ubuntu-13.10- amd64

Make Version: GNU Make 4.0

GCC Version: 4.8

GDB Version: 7.6.1

The above information was also necessary as you may need to create a similar build environment for practice. Though, it’s not mandatory to have a one-on-one match of the tools version. The code and concepts mentioned here are generic, should work ideally with any version.

Need your attention here, while working on a C/C++ project, it’s inevitable to use GDB for debugging. So if you want to learn a few quick tips, then please go through the below post.

Next, reading a few C/C++ tips won’t bother you much. Find out if the below tutorial does have something new for you.

Now, carefully watch out how to create a client-server program in C and build it using the Makefile.

Makefile Tutorial – How To.

What is a Makefile and how does it work?

Makefile manifest a set of rules to locate the dependencies, produces object codes and build the target modules. Processing a Makefile requires the use of Make tool. It can automatically select a Makefile available in the current directory or we can specify one as its command line argument. The first step for using the Make tool is to prepare the user-defined makefiles.

As referred above, the makefile drives its significance by compiling/building a project which has multiple C/C++ files to build. Here is the syntax to use the Make tool from command line.

Usually, we apply the “-f” option when there are more than one Makefiles in a directory.

Note- If we don’t give any file name, then the Make tool will search for a file which could have names like Makefile or makefile (generally this is the by default name of the makefile.)

How to write a Makefile?

A Makefile typically begins with a few variable definitions. Then, there comes a set of target entries for build-specific targets (e.g. “.o” and executable files in C/C++, and “.class” files in Java). Next, there could be a group of commands to execute for the target label.

Below is a generic Makefile template which anyone can follow to create his own.


Create a client-server program in C using Makefile.

In this part of the Makefile tutorial, we are going to implement Client-Server communication using socket programming in C. For this, we’ll create the following two separate modules.

  • Client Socket Module (client.c)
  • Server Socket Module (server.c)

For establishing a connection, we need to perform the following steps.

First are the steps involved in establishing a socket on the client side.

1. Create a socket with the <socket()> system call.
2. Connect the socket to the address of the server using the <connect()> system call.
3. Start sending and receiving data. There are many ways you can do this. But the simplest is by using the <read()> and <write()> system calls.

Next are the steps required to implement a socket on the server side.

1. Create a socket with the <socket()> system call.
2. Bind the socket to an address using the <bind()> system call. For a server socket on the Internet, an address consists of a port number on the host machine.
3. Accept a connection by making the <accept()> system call. It’ll usually block until a client connects to the server.
4. Send and receive data.

Please note that it’s the following socket structure which we’ve used in both client and server programs.


Let’s now check out the source code of the client socket.

Client Socket Program.


Now, let’s see what’s inside the server-side code.

Server Socket Program.


We are now attaching the Makefile created for building the above two modules simultaneously.

Makefile to build Client-Server Programs.


Here are a few notes on the Makefile given above.

  • To use a variable defined in the Makefile, prefix it with the dollar symbol.
  • All, client_target & clean are the targets present in the Makefile.
  • If you don’t provide any target in the Makefile, then the Make tool will build all of them from the top to bottom.

Summary – Makefile Tutorial to Create Client-Server Program.

We are hopeful that the above Makefile tutorial would help you immensely. And you’ll use it efficiently in your new C/C++ projects.

It would be great if you let us know your feedback on this post.

Also, you can ask us to write on a topic of your choice. We’ll add it to our writing roadmap.

Lastly, if you’d enjoyed the post, then please care to share it with friends and on social media.


Enjoy Learning,


Leave a Reply