Copy and paste a file (usually, a .h
header) into a source code file.
Looks for the .h
file relative to this source file:
#include "file.h"
Looks for the .h
file in the system's include paths
These files | become this |
---|---|
factorial.h
main.cc
|
|
factorial.h
comb.h
|
main.cc
|
factorial.h
comb.h
|
main.cc
Prevent double inclusion |
In C++, all header files should be protected by guards.
It is too easy to mess it up
factorial.h
|
comb.h
|
In C++, all header files should be protected by guards.
factorial.h
|
comb.h
|
Convert source code into a machine-code or lower-level form in which the program can be executed
-O0
- No optimization, faster compile time, better for debugging-O2
- Default optimization-O3
- Higher optimization, slower compile time, better for production-g
- Include debugging symbols -s
- Remove all debugging informationFor debugging:
g++ -O0 -g file.cpp -o executable
For production (release):
g++ -O3 -s file.cpp -o executable
-Wall
- Turns on many warnings-Wextra
- Enables extra warnings-Wpedantic
- Even more warnings-Werror
- Transforms warnings into errorsUse all warning flags! It is better to detect errors during compilation than during execution.
For debugging:
g++ -Wall -Wextra -Wpedantic -Werror -O0 -g file.cpp -o executable
For production (release):
g++ -Wall -Wextra -Wpedantic -Werror -O3 -s file.cpp -o executable
g++ $FLAGS file.cpp -o executable
g++ $FLAGS file1.cpp file2.cpp ... -o executable
g++ $FLAGS -c file1.cpp
g++ $FLAGS -c file2.cpp
...
g++ $FLAGS file1.o file2.o ... -o executable
g++ $FLAGS -c file1.cpp
g++ $FLAGS -c file2.cpp
...
g++ $FLAGS file1.o file2.o ... -o executable
.h and .cpp
editionmake
make
is a build system: it drives the compiler
and other tools to build your code.
It is useful when builds require multiple files, as it keeps track of their updates
Configurable through a Makefile
. Based on a set of rules that are
executed if files have changed.
file1.o: file1.cpp file1.h
g++ -c file1.cpp
file2.o: file2.cpp file2.h
g++ -c file2.cpp
file3.o: file3.cpp file3.h
g++ -c file3.cpp
executable: file1.o file2.o file3.o
g++ file1.o file2.o file3.o -o executable
Configurable through a Makefile
. Based on a set of rules that are
executed if files have changed.
CXX = g++
CXXFLAGS = -Wall -Wextra -Wpedantic -Werror -O0 -g
file1.o: file1.cpp file1.h
$(CXX) $(CXXFLAGS) -c file1.cpp
file2.o: file2.cpp file2.h
$(CXX) $(CXXFLAGS) -c file2.cpp
file3.o: file3.cpp file3.h
$(CXX) $(CXXFLAGS) -c file3.cpp
executable: file1.o file2.o file3.o
$(CXX) $(CXXFLAGS) file1.o file2.o file3.o -o executable
Configurable through a Makefile
. Based on a set of rules that are
executed if files have changed.
objects = file1.o file2.o file3.o
CXX = g++
CXXFLAGS = -Wall -Wextra -Wpedantic -Werror -O0 -g
$(objects): %.o: %.cpp %.h
$(CXX) $(CXXFLAGS) -c $^
executable: $(objects)
$(CXX) $(CXXFLAGS) $(objects) -o executable
Configurable through a Makefile
. Based on a set of rules that are
executed if files have changed.
cmake
is a build system generator: it can
produce Ninja build files, Visual Studio projects, Makefiles...
It is based on a configuration file named CMakeLists.txt
CMakeLists.txt
are cross-plattform. Then you can
generate a particular build system for your particular plattform.
CMakeListstxt
should be in the root folder of the project
CMakeLists.txt
cmake_minimum_required(VERSION 3.15...3.30)
project(
MyCMakeProject
VERSION 1.0
LANGUAGES CXX
)
add_executable(executable file1.cpp file2.cpp file3.cpp)
#You can have as many executables as possible (maybe for testing)
CMakeLists.txt
cmake_minimum_required(VERSION 3.15...3.30)
project(
MyCMakeProject
VERSION 1.0
LANGUAGES CXX
)
#For all executables in project
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Werror")
add_executable(executable file1.cpp file2.cpp file3.cpp)
#You can have as many executables as possible (maybe for testing)
CMakeLists.txt
cmake_minimum_required(VERSION 3.15...3.30)
project(
MyCMakeProject
VERSION 1.0
LANGUAGES CXX
)
add_executable(executable file1.cpp file2.cpp file3.cpp)
#You can have as many executables as possible (maybe for testing)
target_compile_options(executable "-Wall -Wextra -Wpedantic -Werror")
#For a particular executable
For make...
> mkdir build
> cd build
> cmake .. -G "Unix Makefiles"
> make
> mkdir build
> cd build
> cmake .. -G Ninja
> ninja
Internally inside an IDE
An integrated development environment (IDE) enables programmers to consolidate the different aspects of writing a computer program.
IDEs increase programmer productivity by combining common activities of writing software into a single application: editing source code, building executables, and debugging.
Install extensions
For each project:
CMakeLists.txt
on its root folderYou can have a look at simple CMakeList.txt examples that work in Visual Studio Code on the course's Github repository