Phần 1
1. Problem It Solves
C++ source code is plain text. A toolchain turns that source into a runnable program and reports many mistakes before the program reaches production.
For this lesson, the compiler must use C++11 mode. Warning flags are enabled so suspicious code is visible even when it is technically valid C++.
Phần 2
2. Prerequisites
No previous C++ lesson is required. You only need to know how to open a terminal,
move to a directory, and identify a file ending in .cpp.
Phần 3
3. Core Idea
A simple native build has four conceptual stages:
The preprocessor expands directives such as
#include.The compiler checks C++ grammar and types, then produces object code.
The linker combines object files with required library code.
The operating system loads and starts the executable at
main().
The executable is a separate file from the source code. A failed build does not replace an older executable, so running the command again after a failure may accidentally run stale code.
Phần 4
4. Minimal Commands
Compile with GCC:
g++ -std=c++11 -Wall -Wextra -Wpedantic source.cpp -o programRun on Linux or macOS:
./programRun on Windows PowerShell:
.\program.exeImportant options:
-std=c++11selects the C++11 language rules.-Wallenables many common diagnostics.-Wextraenables additional useful diagnostics.-Wpedanticreports extensions outside the selected standard.-o programchooses the executable name.
Use g++, not gcc, for the final C++ link command. g++ automatically links
the C++ standard library.
Phần 5
5. Compile and Link Separately
A multi-file build can compile each source file first:
g++ -std=c++11 -Wall -Wextra -Wpedantic -c feed.cpp -o feed.o
g++ -std=c++11 -Wall -Wextra -Wpedantic -c main.cpp -o main.oThen link the object files:
g++ feed.o main.o -o feed-checkThis separation matters because changing one source file should not require recompiling every other source file. Build systems such as CMake model these dependencies and issue the necessary compiler and linker commands.
Phần 6
6. Debug and Release Options
A useful local debug build commonly adds:
-O0 -g-O0 keeps optimization low, while -g emits debugging information. A release
build commonly uses an optimization level such as -O2. Optimization affects
performance and generated code; it is not a substitute for correctness checks,
tests, or warnings.
The exact compiler version and options are build inputs. CI and production should record them so a failure can be reproduced.
Phần 7
7. Exit Status
A return value of 0 from main() means success. A non-zero value normally
signals an error:
if (bid_price > ask_price) {
std::cerr << "Invalid quote\n";
return 1;
}Shell scripts and CI jobs use this exit status to decide whether a step passed.
Phần 8
8. Common Mistakes
Forgetting
-std=c++11, so the compiler default varies between machines.Ignoring warnings because the program still builds.
Using
gccinstead ofg++for the final C++ link command.Running an old executable after compilation failed.
Assuming successful compilation proves the program is correct.
Mixing incompatible compiler, standard-library, or ABI settings.
Applying
-Werrorblindly to third-party headers and turning external warnings into local build failures.
Phần 9
9. Trading-System Relevance
A tick-data program may validate a quote where the bid price must not exceed the ask price. The same source should behave consistently on a developer laptop, a test server, and a production machine. An explicit standard mode, strict warnings, repeatable compiler versions, and a failing exit status reduce environment-dependent surprises.
Phần 10
10. Key Takeaways
Select the C++ version explicitly.
Compile with warnings and investigate them.
Treat compilation and linking as distinct build stages.
Do not confuse a successful build with a correct program.
Make toolchain versions and options reproducible.
Phần 11
11. Self-Check Questions
What does
-std=c++11control?Why should warnings be fixed even when compilation succeeds?
What is the difference between a
.cppfile, an object file, and an executable?Why can running a command after a failed build execute stale code?
Why should CI preserve the compiler version and build options?
Mã mẫu hoàn chỉnh
cpp11/30_toolchain/main.cpp
#include <iostream>
int main() {
// Fixed market data so the output is easy to verify.
const char* symbol = "AAPL";
const double bid_price = 189.10;
const double ask_price = 189.14;
const int bid_volume = 500;
const int ask_volume = 400;
// A simple validity check for one quote snapshot.
const bool price_order_is_valid = bid_price <= ask_price;
const double spread = ask_price - bid_price;
std::cout << "C++11 quote check\n";
std::cout << "Symbol: " << symbol << '\n';
std::cout << "Bid: " << bid_price
<< " x " << bid_volume << '\n';
std::cout << "Ask: " << ask_price
<< " x " << ask_volume << '\n';
std::cout << "Spread: " << spread << '\n';
if (!price_order_is_valid) {
std::cerr << "Error: bid price is greater than ask price.\n";
return 1; // Non-zero means the program detected an error.
}
std::cout << "Quote is valid.\n";
return 0; // Zero means successful execution.
}Tự kiểm tra · không chấm điểm
Bạn đã hiểu những điểm nào?
Bài này chưa có danh sách tự kiểm tra trong nguồn.