# Why undefined function compiled (C++)? When I read through below example in [cpprefernce](https://en.cppreference.com/w/cpp/language/abstract_class) ```cpp!= struct Abstract { virtual void f() = 0; // pure virtual }; // "Abstract" is abstract struct Concrete : Abstract { void f() override {} // non-pure virtual virtual void g(); // non-pure virtual }; // "Concrete" is non-abstract struct Abstract2 : Concrete { void g() override = 0; // pure virtual overrider }; // "Abstract2" is abstract int main() { // Abstract a; // Error: abstract class Concrete b; // OK Abstract& a = b; // OK to reference abstract base a.f(); // virtual dispatch to Concrete::f() // Abstract2 a2; // Error: abstract class (final overrider of g() is pure) } ``` my first thought was: How is this program compiled? since `Concrete::g` is not defined. But when I click `Run` button, it did exit normally. Then, I copy whole example and write a simple cmake file to compile on my machine. Of course, it won't compile. `CMakeLists.txt` ```cmake!= cmake_minimum_required(VERSION 3.10) project(main) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_CXX_STANDARD 17) add_executable(main ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cc) ``` The error showed: ```sh! /usr/bin/ld: CMakeFiles/main.dir/src/main.cc.o: warning: relocation against `_ZTV8Concrete' in read-only section `.text' /usr/bin/ld: CMakeFiles/main.dir/src/main.cc.o: in function `main': /home/sw/GitHub/dap/src/main.cc:53: undefined reference to `vtable for Concrete' /usr/bin/ld: warning: creating DT_TEXTREL in a PIE collect2: error: ld returned 1 exit status ``` This definitely indicates that the virtual `g()` is undefined. So how did the online compiler work without error? I checked the online compile argument. It shows: ```sh! g++ -std=c++23 -O2 -Wall -Wextra -pedantic -pthread -pedantic-errors main.cpp -lm -latomic && ./a.out ``` Ok, there got to be some magic here. ## The Answer TLDR, the optimization `-O2` flag did the trick. According to latest GCC [doc](https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Optimize-Options.html): > `-O1` > Optimize. Optimizing compilation takes somewhat more time, and a lot more memory for a large function. > With -O, the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal of compilation time. > ... > `-O2` > Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. As compared to -O, this option increases both compilation time and the performance of the generated code. > -O2 turns on all optimization flags specified by -O1. Godbolt example: [link](https://godbolt.org/z/GfGGKenW9) (you can try to remove `-O2` flag and see what happened) However, I couldn't find what specific flag remove the dead code or prevent the error. Maybe someone can tell me... :joy: --- *So why cppreference provide such an example? I know they want to show `Abstract2` is not constructable, but this confuse me at the first time. (~~or maybe just the skill issue lol~~)*