https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79819
Bug ID: 79819 Summary: collect2 undefined reference when -O0. Regression (or bugfix?) since gcc5 Product: gcc Version: 6.3.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: nheart at gmail dot com Target Milestone: --- Created attachment 40874 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=40874&action=edit console output from failed compilation and *.ii files Hey, I have a file that fails to link to the main when compiling with -O0 and -g ONLY on gcc6 (works fine on gcc-5 and clang 3.9. What happens is that there is a static bool condition set at compile time, which if set to true causes a certain function to be called which is only compiled if that condition is set to true (If the condition is not set to true, then the function would only be declared). This seems to work fine, unless gcc6 -O0 compilation is used, at which point during the link stage there's an undefined reference error. The gcc6 error is easily fixed if I add a definition of the offending function, something like void foo() {} As far as I recall the compiler is going to try to execute all code paths, even if the code would never be reached (eg. hidden by a static bool). However does it complain if the function is only declared but not implemented. Which one is the correct behavior? The attachment contains the loader_factory.ii files from gcc5 with -O0, gcc5 with -O3, gcc6 with -O3 and gcc6 with -O0, as well as console output of the failed compilation. The only problematic case is gcc6 with -O0 Compilers used: gcc5: 5.4.0 gcc6: 6.3.1 20170109 Gcc compiled with: gcc/configure --prefix=/usr \ --libdir=/usr/lib \ --libexecdir=/usr/lib \ --mandir=/usr/share/man \ --infodir=/usr/share/info \ --with-bugurl=https://bugs.archlinux.org/ \ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ \ --enable-shared \ --enable-threads=posix \ --enable-libmpx \ --with-system-zlib \ --with-isl \ --enable-__cxa_atexit \ --disable-libunwind-exceptions \ --enable-clocale=gnu \ --disable-libstdcxx-pch \ --disable-libssp \ --enable-gnu-unique-object \ --enable-linker-build-id \ --enable-lto \ --enable-plugin \ --enable-install-libiberty \ --with-linker-hash-style=gnu \ --enable-gnu-indirect-function \ --disable-multilib \ --disable-werror \ --enable-checking=release On an Arch Linux x86_64 Haswell system. Flags used for both O3 and O0 builds: set(CMAKE_CXX_FLAGS_RELEASE "-std=c++14 -fPIC -O3 -Ofast -m64 -march=native -funroll-loops -ffinite-math-only -Wno-unused-result -Wno-deprecated -pthread") set(CMAKE_CXX_FLAGS_DEBUG "-std=c++14 -fPIC -g -O0 -m64 -march=native -funroll-loops -ffinite-math-only -Wno-unused-result -Wno-deprecated -pthread") Please ask me if you have any more questions.