
Learn to write and optimize GNU Make makefiles for C/C++ projects on Linux and Mac, from requirements to template creation, covering targets, dependencies, and advanced features.
Learn how Makefile targets work. If no target is specified, the first target acts as the default target, which you can override with a default goal across multiple targets.
Download or clone the complete source code from the GitHub link provided. Always read the README. It has all the details.
Plan and build a multi-module C/C++ project with makefiles, placing binaries in bins, libraries in libs, and objects in objs, linking the binary with a dynamic and a static library.
The problem in the above makefile
Incremental build issue
Fix the above issue by updating the makefile
Step by step, we will improve the makefile once we feel we really should do it!
Need Of Variables/Macro
Declaring Variables, accessing variables
Make tool internal variables & their purpose($^ $< $@)
How do you generate object files using makefile?
ADD LINKER FLAG TO MAKEFILE
Revisit source code.
Understand the need for a linker flag.
Understand how to link the library to an executable
Reason for linking math library
Reason for not linking libc while using printf() from stdio.h
How to know what linker flag to use?
Where to add the linker flag?
PLACEMENT OF COMPILER FLAG
Example code demonstrates why we should use a warning flag
Unused variable detection technique
Feel the necessity of compiler flag
Where to keep compiler flag
Explanation of Wall & Werror
Improving our makefile by adding Compiler flags(CFLAGS) and Linker flags(LDFLAGS)
Modify mod1 C code to C++ code
Concatenate variables in the makefile
Accessing the path of libs, and objs in a robust way!
Clean targets
Copy module 1 makefile & do the necessary modifications to achieve makefile for .so
Use -fPIC in Makefile
test target introduced to generate test binary
build_dir target introduced to generate directories
What happens if the output binary name has having .cpp extension?
Where Are We(MOD-2)
'install' target is introduced to copy headers to share header location to make its access easier.
Proceed to implement modulatory logic in the same way as the previous binary test for DSTO five, mirroring the approach used for the three.
Implementing MOD-3
Modify MOD-1 Makefile to create mod-3 Makefile
Without writing the path of header files in the C/C++ source file, how to position it in a makefile? (INCLUDE FILE search path)
More optimization in Makefile.
Cmd to use for static lib generation
What is a static library
Why rcs
Understand changes needed for converting .so Makefile to '.a' makefile
Writing test module to verify if generated '.a' is usable.
Command line demo(linking to .a file)
MOD-5 Source code & Makefile
Add 'install' target to mod-4
Minimum what we should know for writing makefile.
Place for compiler & linker flags.
Understand CFLAGS & LDFLAGS.
Accessing macro in the program without defining it.
Passing macro from the command line. The same way can be followed inside a makefile.
Demonstrate std::async
Why need -std
When -std is mandatory
Where to write it in makefile
Compiling using C++2a
C++20 Vs C++17 Vs C++2a
How to detect the C/C++ standard to use?
What is Lazy initialization & Instant initialization
Why == & :=
When := is used
Failing under a situation where == fails and := solves the problem
Demonstrate usages of .PHONY target
How to decide the target can be PHONY
If not defining PHONY, what problem may arise?
From the understanding of makefile from above PROJECT#01, we are preparing a template which can be used in future projects.
Additionally, we are adding the following:
BUILD_TIME_STAMP which will be helpful to track the release binaries' build time(Helpful if any issue comes from the customer place. Detecting if the shared package is correct or not)
INSTALLATION_PATH: This makes the system handy for deployment.
Cross Compilation feature
A better way of accessing bins, libs and other folders
Copy headers during the building
Understand each attribute used in the template makefile.
Object name collision issue
Understanding attributes used in makefile
Understanding attributes used in makefile
Discuss What we are going to do in this project. (Project explanation with High-Level Design)
Project Source code explanation. Discuss about the plan for makefile & common makefile(Single make to build all modules)
How to use the makefile template to create an employee static library.
How to use the makefile template to create database manager shared object(Dynamic Library).
Create a makefile for the UI Controller module from the template that we have already created.
See how the build is successful but later finds the run fails.
See how run fails. It is a common issue you might have seen in realtime projects. .so not found!
Fix .so not found issue in MAC
Fix .so not found issue in Linux
What Will Learn In Upcoming Release!
1. How to write a common makefile to call other makefiles.
2. Build using single make.
3. Run the binary. (Run from such a location from where our executable should be able to find the dependent libs)
One way: Make our binary run from any location and detect the dependent libraries.
Tips on how to debug dependency issues in MAC & Linux.
Another robust way to run our binary from anywhere which can detect the dependent libraries.
Do export inside the makefile.
How To Do CROSS-COMPILER Setup for C & C++.
Understand make tool & write better makefile by understanding what is happening behind the scene(Creation of intermediate files) during building C/C++ programs. We will understand the makefile by using a real time example( "Creation Of Egg Omelet" ).
We are going to do 2 projects during the learning process of makefile.
PROJECT#1: Without knowing/remembering too many things you will be able to write a makefile for generating binary, shared object(.so), static object(.a), and also get a trick to link from anywhere. After completion of this project Idea of makefile will be cleared. We are creating a makefile template after completing this project and which can be easily used in any project.
PROJECT#2: During implementing this project, we'll improve our makefile and makefile template created in Project#1 by applying patterns, functions.
Every step we will face some problem and then apply the solution.
We will begin with the PROJECT FOLDER STRUCTURE creation(THIS CAN BE USED IN REAL TIME PROJECTS) after understanding all the possible output of a C/C++ program. Then we will come across more technical issues in programming and try to solve them one by one during this journey. Our project structure is in such a way that deployment process will become much easier.
We'll start writing simple makefiles and keep on improving as we really need it! We are doing lots of exercises and facing lots of issues and also fixing all on the fly!
We are going to write a makefile for all possible outputs. In this course you will always learn WHY??? and HOW???
I will show you the real requirement of a particular concept and then we will learn and apply that.
Highlights:
1. Write makefile by knowing minimal things.
2. One Makefile to call all makefiles
3. Makefile with Cross Compilation feature
4. Makefile considering DEBUG & RELEASE version of binary.
5. Passing MACRO from makefile to other makefile
6. Detect environment variables of terminal and one make pass the same to other make.
7. We will discuss high level design(HLD) of the projects and from that you will feel how easy it is to write a makefile.
8. You will be more comfortable with command line compilation
9. Understand shared object linking issues and how to fix it temporarily as well as permanently.
10. Project will be done in MAC and copied to Linux. We will directly do a single make which will call all the modules makefile and build the executables, shared object, static objects seamlessly. IMPORTANT thing is WITHOUT ANY SINGLE LINE OF CODE CHANGE FOR LINUX!
11. GitHub Actions Essentials: Create workflows from scratch, integrate make build, and run on Linux/macOS runners.
12. Cross‑Platform Builds: Test code on Linux and macOS without installing those OS locally, thanks to GitHub Actions.