Tricks for C++ Simple App Development
This tutorial is not a primer guide, nor will there be much detail.
My scenarios: Algorithm Learning.
Switch to Linux
Compilers work faster on Linux most of the time, and many tools only run on Linux.
Fedora is the distribution I'm using.
Then you should install the GCC, on Fedora it's dnf install gcc-c++
. Search "Install GCC Your Distribution
" for more.
MOLD Linker
Install mold. Although we are just writing a simple app, mold
still made the build a little faster.
VSCode and Clangd
Install VSCode for editing and Clangd Extension for intellisense.
Clangd is much faster than Microsoft's C/C++ extension.
If you want a debug support, try Native Debug or CodeLLDB.
Project Structure & Others
Create a folder named hello-cpp
like this:
├─ build
├─ src
│ └─ main.cc
└─ utils
├─ mold
├─ mold-wrapper.so
└─ run.sh
The content of ./utils/run.sh
:
# \time -f %e \
~/utils/mold -run g++ ./src/main.cc -o ./build/main -Wall -Wextra -g -fsanitize=undefined -fsanitize=address -fno-omit-frame-pointer
ulimit -t 1 # avoid infinity loop
[ $? -eq 0 ] && ./build/main
# readelf -p .comment ./build/main
Switch -fsanitize=address
is used to enable AddressSanitizer.
Then you can create a build task to execute ./utils/run.sh
and bind a key shortcut.
Include less headers
Don't include unnecessary headers. #include <bits/stdc++.h>
caused heavily compile time, only use it when submitting to Online Judge.
Redirect cin
The common way to redirect the stdin is freopen("in.txt", "r", stdin)
, but creating extra file is annoying, so here a more concise way:
stringstream fcin(R"(1
3
1 2 3)");
istream std::cin(fcin.rdbuf());
Old compilers may be unsupported.