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.
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 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
Download or clone the complete source code from the GitHub link provided. Always read the README. It has all the details.
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.
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
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++.
WHY WE NEED FUNCTIONS & PATTERNS
Understand the problem of adding new source files in a module.
Rules & Recipes in Makefile
An example of 'db_mgr' is taken. If adding a new file db_writer.cpp, we need to modify the Makefile to build newly added .cpp files. Don't you think, we need an automated way, by which make can auto-detect our modules?
Understanding FUNCTION&PATTERN
Decide/Configure obj file name & binary file names from source file
Using function & pattern - The beginning
Substitution reference
ASSIGNMENT IN VIDEO: PRINT ALL C & C++ FILES PRESENT IN CURRENT DIRECTORY
An assignment given in the previous section is covered here.
LIVE DEMO: Detecting all source, Determine obj & binary file names.
LIVE DEMO:
Create 3 cpp files t1.cpp t2.cpp t3.cpp & write a simple makefile to generate the binaries as t1 t2 t3.
Write a simple makefile to build all cpp files present in a specific directory
Demo shown. But no obj generated.
Live demo-How to generate Object files(Without using the pattern in target)
Explain the evaluation of the previously discussed makefile and do the modification with the concepts that we already discussed previously.
Generate object/bin files using patterns in Rules- Failed!
Live demo: Trying to generate object files using patterns, in the Rules in makefile.
Approach Using pattern fails & Why it fails, is explained.
Nice Trick is explained, how to convert to pattern.
OPTIMIZED MAKEFILE:
How to generate Object files(Using patterns in rules)
The trick is explained.
Discuss the plan to modify the Common Makefile.
Why $$ ?
In old Common Makefile, what are we doing?
What things can be optimised? Step by step explanation.
OCM-Optimize Common Makefile
How to write a function and call a function
Modify our Common Makefile by using the above concept of Function & Patterns
Calling the function in the proper place with proper arguments
Difference Between OLD & New Makefile
The problem in the running make(i.e 2Times Make run is needed)
How to write our new makefile that can run only once to do the build perfectly.
CAN SKIP-LIVE DEMO OF 2TIMES MAKE RUN & Understand the root cause.
Live Demo: How to handle dependency in makefile.
Modify db_mgr makefile and run common make file only once to get the libraries & binaries.
$(MAKE) & make
Just copy the Project folder from MAC to Linux, and see without any modification in makefile, it works fine.
Live Demo by runing uic binary.
Tips For C Programmers.
OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.
Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.
Find this site helpful? Tell a friend about us.
We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.
Your purchases help us maintain our catalog and keep our servers humming without ads.
Thank you for supporting OpenCourser.