Reported originally in c++/20020 ----- snip ----- $ cat main.cpp struct A { static constexpr const char *a = "a"; void foo() { } }; int main(void) { A a; a.foo(); } Compile with g++ 7.1 with C++17 semantics: g++-7 -std=c++17 -g -o main main.cpp Now run it with gdb: $ gdb main GNU gdb (Debian 7.11.1-2~bpo8+1) 7.11.1 (...) (gdb) b A::foo Breakpoint 1 at 0x40055e: file main.cpp, line 6. (gdb) r Starting program: main Breakpoint 1, A::foo (this=0x7fffffffe09f) at main.cpp:6 6 void foo() { } (gdb) print *this $1 = {static a = <optimized out>} ----- end -----
Just a note: I get *optimized out* with c++14 (the default), not c++17 So you want to compile with "g++-7 -g -o main main.cpp" to reproduce this bug. If you add -std=c++17, you're reproducing the segfault in c++/20020
To be 100% clear with the c++/20020 segfault fixed and c++17, you get "{static a = <error reading variable: Missing ELF symbol "A::a".>}" With c++14, you get "{static a = <optimized out>}"
I needed -fno-eliminate-unused-debug-types for this. This looks like a compiler bug to me. The DWARF for A::a is: <2><35>: Abbrev Number: 5 (DW_TAG_variable) <36> DW_AT_name : a <38> DW_AT_decl_file : 1 <39> DW_AT_decl_line : 2 <3a> DW_AT_decl_column : 34 <3b> DW_AT_linkage_name: (indirect string, offset: 0x0): _ZN1A1aE <3f> DW_AT_type : <0x60> <43> DW_AT_external : 1 <43> DW_AT_declaration : 1 <43> DW_AT_const_expr : 1 <43> DW_AT_inline : 1 (inlined) There's no location here, just a linkage name. However, that symbol isn't defined.