type
status
date
slug
summary
tags
category
icon
password
C++ is a compiled language
- source code can't be run directly
- code is written to an abstract machine model (more on that later)
- compiler translates source code into binary machine code understood by the CPU
- program that can be run = binary executable file containing machine code
1.1 Compile
1.1.1 Preprocess
Details:
- Remove Comments: Delete the comments in the source code.
- Process macro definitions: Expand macro definitions.
- Process conditional compilation: Selectively include or exclude code based on conditional compilation directives such as
#ifdef,#ifndef,#if.
- Process inclusion of files: Insert the contents of header files into the source file, forming a single file.
- Process other preprocessing directives.
1.1.2 Compile
Details:
- Translate the preprocessed source code into assembly language code.
1.1.3 Assemble
Details:
- Translation of assembly code into machine code (object files)
1.1.4 Link
Details:
- Link all object files and possible library files together, resolve symbol references, and generate the final executable file.
- If shared libraries (dynamic link libraries) are used, the linker may generate dynamic link library files (
.soor.dll).
1.2 Terminology
Compiler Error: program not compilable, compiler will stop
Compiler Warning: program compilable, compiler will continue, but there is a problematic piece of code that might lead to runtime bugs
1.3 Compiler Flags
Recommended compiler flags for your first programs
g++ -std=c++20 -Wall -Wextra -Wpedantic -Wshadow input.cpp -o outputFlags | Function |
-std=c++20 | Sets compiler to the C++20 standard. Highly Recommended. |
-Wall
-Wpedantic
-Wextra
-Wshadow | Enable compiler warnings. Highly recommended. These don't really activate all warnings, but rather the most important ones that don't produce too much (false positive) noise. |
It's 2023 – set your compiler to C++20 (or at least to C++17 if you have to use an older compiler).
Don't Use
using namespace std;!Many code examples / tutorials show something like this:
… to avoid qualifying
cout with std::.But:
using a namespace drags all symbols from that namespace into the global namespace. This can lead to name conflicts and ambiguities and in some cases even bugs that will only manifest at runtime and are very hard to detect.Polluting the global namespace with all symbols from other namespaces is a serious liability in any production code base and you should avoid this anti-pattern from the very start.
1.4 Library
Write later
1.4.1 Static Library
fixed at compile time (baked into the executable file, not changeable at runtime)
1.4.2 Shared Library
changeable at runtime (possibly by user input)
- 作者:KaiGuo
- 链接:https://blog.kaiguov5.com/article/d197a6ce-4799-4eb3-85e0-c6c660d611f1
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。