Git Product home page Git Product logo

make_tutorials's Introduction

Cpp Makefile Tutorial

GCC/Clang Compiler Steps

Compilation (Assembling)

  • Checks the C/C++ language syntax for error
  • Generates object files
  • Command: g++ main.cc -c
  • Produces: main.o

Linker

  • Linking all the source files together, that is all the other object codes in the project.
  • Generates the executable file
  • Command: g++ main.o -o main
  • Produces: main.out (.exe for Windows)

Compiler Flags

  • Debug: -g
  • Release: -O0 -O1 -O2 -O3 -Og
  • Includes: -I
  • Warnings: -Wall -Wextra -Wpedantic -Wconversion

Makefile Commands of the Template

Makefile Variables

Convention is naming in upper snake_case.

  VARIABLE_NAME = Value

Variables can be called by $(VARIABLE_NAME)

  $(VARIABLE_NAME)

Makefile Targets

Convention is naming in snake_case or camelCase.

  targetName: Dependecies
    Command

Targets can be called by the make command.

  make targetName

Makefile Phony Target

Sometimes you want your Makefile to run commands that do not represent files, for example the "clean" target. You may potentially have a file named clean in your main directory. In such a case Make will be confused because by default the clean target would be associated with this file and Make will only run it when the file doesn't appear to be up-to-date.

.PHONY: clean
clean:
  rm -rf *.o

In terms of Make, a phony target is simply a target that is always out-of-date, so whenever you ask make <phony_target>, it will run, independent from the state of the file system.

Build the Executable

Create the executable in either Debug or Release mode.

  make build DEBUG=0 # Build type is debug
  make build DEBUG=1 # Build type is release

Run the Executable

Run the executable in either Debug or Release mode.

  make execute DEBUG=0 # Build type is debug
  make execute DEBUG=1 # Build type is release

Variables of the Makefile Template

  • Debug Mode: 1 (True) or 0 (False)
  • ENABLE_WARNINGS: 1 (True) or 0 (False)
  • WARNINGS_AS_ERRORS: 1 (True) or 0 (False)
  • CPP_STANDARD: c++11, c++14, c++17, etc.

Important Shortcuts of the Makefile Template

  • $@: the file name of the target
  • $<: the name of the first dependency
  • $^: the names of all dependencies

make_tutorials's People

Contributors

sayeed97 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.