I have a weird problem with gcc trying to include a file twice in one compilation unit, even though there's #pragma once in that file.
The files needed to reproduce the issue are:
CMakeLists.txt
cmake_minimum_required( VERSION 3.16 )project( example )add_library( example SHARED src/example/class_a.cpp )target_include_directories( example PRIVATE src )target_precompile_headers( example PRIVATE src/example/pch.h )
src/example/class_a.cpp
#include "class_a.h"
src/example/class_a.h
#pragma onceclass class_a { };
src/example/pch.h
#pragma once#include <example/class_a.h>
Trying to compile the project leads to:
[ 33%] Building CXX object CMakeFiles/example.dir/cmake_pch.hxx.gch[ 66%] Building CXX object CMakeFiles/example.dir/src/example/class_a.cpp.oIn file included from /src/example/class_a.cpp:1:/src/example/class_a.h:6:7: error: redefinition of ‘class class_a’ 6 | class class_a | ^~~~~~~In file included from /src/example/pch.h:2, from CMakeFiles/example.dir/cmake_pch.hxx:5, from <command-line>:/src/example/class_a.h:6:7: note: previous definition of ‘class class_a’ 6 | class class_a | ^~~~~~~
If I switch the #pragma once guards to old-fashioned #ifndef XX, it compiles fine.
Also it compiles without errors if I change to "class_a.h" in pch.h file.
Am I doing something really wrong here? It really feels like a gcc bug not being able to figure out that #pragma once means "only once".