Here’s a two-part recipe of compiler/linker flags you can sprinkle on your build to both enforce a lean “C-with-classes” / low-level C++ style and strip out as much bloat as possible:
- “Style Enforcement” flags
- “Size/Bloat Removal” flags
—————————————————————————————
- Style-Enforcement Flags
Use these to banish fancy C++ features, force stricter C-style casts, catch implicit conversions/shadowing, and turn warnings into hard errors:
-std=c++98 # or c++03 if you need even older compatibility
-pedantic # pedantically conform to the chosen standard
-Wall -Wextra # all the usual suspects
-Wold-style-cast # forbid C++-style casts; leaves only C-style casts
-Wconversion # warn on implicit type conversions
-Wshadow # warn on variable shadowing
-Wpointer-arith # pointer arithmetic warnings
-Wstrict-overflow=2 # aggressive overflow assumptions
-Werror # treat all warnings as errors
Explanation:
• -std=c++98 (or -std=c++03) keeps you out of the C++11/14/17 land—no auto, lambdas, noexcept, <mutex>, etc.
• -pedantic + -Wall -Wextra guard you from sneaking in non-standard extensions or sloppy code.
• -Wold-style-cast forces you to use the same (T)x style you already had in your C-style code.
• -Wconversion + -Wshadow unwind subtle bugs at compile time.
• -Werror makes sure you can’t ignore any of the above.
————————————————————————————
- Size/Bloat-Removal Flags
Strip out RTTI, exceptions, unused code, and optimize for binary size:
-fno-rtti # no run-time type information
-fno-exceptions # no C++ exceptions
-Os # optimize for size
-ffunction-sections # put each function in its own section
-fdata-sections # put each data in its own section
-fno-use-cxa-atexit # avoid extra __cxa_atexit bookkeeping
-Wl,--gc-sections # linker: discard unused sections
-lc -lc_nonshared -lm # link only the C library and math (no libstdc++)
-nostdlib # drop all standard libs if you’re rolling your own startup
Explanation:
• -fno-rtti & -fno-exceptions remove the exception machinery and RTTI tables entirely.
• -Os gives you maximum code-size savings (it’s like -O2 but tuned for small binaries).
• -ffunction-sections + -fdata-sections + --gc-sections let the linker garbage-collect anything you don’t actually call or reference.
• -fno-use-cxa-atexit avoids extra wrappers around global destructors.
• -nostdlib / manual -lc -lm linking is optional but guarantees you won’t accidentally pull in <iostream> or other C++ runtime bits.
————————————————————————————
Example full command line
g++ \
-std=c++98 -pedantic \
-Wall -Wextra -Wold-style-cast -Wconversion -Wshadow -Wpointer-arith \
-Wstrict-overflow=2 -Werror \
-Os -ffunction-sections -fdata-sections -fno-rtti -fno-exceptions \
-fno-use-cxa-atexit \
-nostdlib \
-lc -lm \
-Wl,--gc-sections \
-o brainfuck_vm \
main.cpp
This setup:
• Enforces your “C+classes” coding style.
• Snips out all the extra RTTI/exception baggage.
• Dead-code-eliminates any unused functions or data.
• Gives you a tiny, blazing-fast VM binary.
Happy hacking! 👾