Command Line
Makefile
Programming
Variable Passing
Software Development

Passing additional variables from command line to make

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

GNU Make is a tool which controls the generation of executables and other non-source files of a program from the program's source files. Make gets its knowledge of how to build your program from a file called the makefile, which lists each of the non-source files and how to compute it from other files. It also provides the functionality to pass additional variables from the command line, enhancing its flexibility and usability in complex builds.

Understanding Make Variables

Variables in make are a key feature, allowing the definition of text strings that can be reused in multiple parts of the makefile. These variables can be defined within the makefile itself or can be passed from the command line, providing a dynamic way to influence the build process.

Types of Variables in Makefiles

  1. Automatic Variables: Variables like $@ and $<, which have values computed afresh for each rule that is executed, based on the target and prerequisites of the rule.
  2. Recursively Expanded Variables: Variables defined using =; make does not expand these until their values are needed.
  3. Simply Expanded Variables: Defined with :=, these variables are expanded once immediately when they are defined.

Passing Variables from the Command Line

Passing variables from the command line is straightforward in make. When invoking make, you can set values of variables by using the syntax:

bash
make VARIABLE=value

These values will override whatever assignment the variable may have had in the makefile. This is particularly useful for passing settings that might vary with each invocation, such as directories for output files or build configurations.

Example Usage

Consider a scenario where you need to compile a program for different environments like development or production, which could differ in optimizations, debug symbols, etc.

Makefile sample:

makefile
1CFLAGS_DEV = -g -O0
2CFLAGS_PROD = -O2
3
4all:
5    gcc $(CFLAGS) hello.c -o hello

You can invoke make for a development build or a production build as follows:

bash
make CFLAGS=$(CFLAGS_DEV)  # Development build
make CFLAGS=$(CFLAGS_PROD) # Production build

Benefits of Using Command Line Variables

  • Flexibility: Customize the build without having to modify the makefile.
  • Reusability: One makefile can handle different environments or configurations.
  • Simplification: Reduces the complexity within a makefile by eliminating conditional statements based on configuration.

Key Points Summary

FeatureDescriptionUtility
Override CapabilityCommand line variables can override makefile variables.High
Flexibility & CustomizationAdapt to different build environments without altering makefiles.Very High
Simplicity in Makefile DesignReduces the need for complex conditional logic in makefiles.Moderate
Immediate ExpansionVariable values on the command line are expanded immediately.High

Advanced Usage

Target-specific Variables

Variables can also be set on a per-target basis in the makefile itself, providing an even finer-grained level of control over the build process. For example:

makefile
1lib: CFLAGS += -fPIC
2lib: library.o
3
4library.o: library.c
5    gcc $(CFLAGS) -c $< -o $@

Using Environment Variables

Another approach is to pass environment variables. These are not specifically command line arguments to make, but they can influence the build process if the makefile is designed to use them:

bash
export CFLAGS="-O2"
make

It's important to note that command line variable settings take precedence over environment variables.

In conclusion, the ability to pass additional variables from the command line to GNU Make provides developers with powerful control over the build process, allowing for flexible and dynamic build environments. This capability, combined with the inherent features of make, makes it an indispensable tool in modern software development.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions