5

When using Cmakes target_precompile_headers we get a lot of redefinition errors like

/usr/include/c++/8/bits/stringfwd.h:70:37: error: redefinition of default argument for ‘class _Traits’
/usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h:21:3: error: conflicting declaration ‘typedef struct __mbstate_t __mbstate_t’
/usr/include/x86_64-linux-gnu/bits/types/__locale_t.h:28:8: error: redefinition of ‘struct __locale_struct’
/usr/include/c++/8/bits/postypes.h:112:7: error: redefinition of ‘class std::fpos<_StateT>’
/usr/include/c++/8/bits/postypes.h:216:1: error: redefinition of ‘template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)’
/usr/include/c++/8/bits/postypes.h:221:1: error: redefinition of ‘template<class _StateT> bool std::operator!=(const std::fpos<_StateT>&, const std::fpos<_StateT>&)’
/usr/include/c++/8/iosfwd:76:70: error: redefinition of default argument for ‘class _Traits’
/usr/include/c++/8/iosfwd:79:70: error: redefinition of default argument for ‘class _Traits’
/usr/include/c++/8/iosfwd:82:70: error: redefinition of default argument for ‘class _Traits’

and countless others, all from std library functions.

Our Cmake setup is as basic as possible and we use g++-8.

cmake_minimum_required(VERSION 3.1)
set (CMAKE_CXX_STANDARD 14)
project(VoxelGrid LANGUAGES CXX)
file(GLOB srcfiles 
${PROJECT_SOURCE_DIR}/src/*.h   
${PROJECT_SOURCE_DIR}/src/*.cpp
)
add_executable(VoxelGridTest exe/main.cpp ${srcfiles})
target_include_directories(VoxelGridTest PUBLIC ${PROJECT_SOURCE_DIR}/src)
target_precompile_headers(VoxelGridTest PUBLIC ${PROJECT_SOURCE_DIR}/pchs/pch.h)

we have one src and one pch folder. The sytem is Ubuntu 20. It seems that this problem should be common but we have found nothing so far. The precompiled header is only

#pragma once
#include <iostream>

with nothing else.

Thanks for any advice!

TPandia
  • 51
  • 4

1 Answers1

1

It turns out that this is a bug in GCC. I ended up wrapping contents of our pch.h in include guards:

// Not using #pragma once here
// see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56549
#ifndef My_PCH_GUARD
#define My_PCH_GUARD

#include <iostream>

#endif

Another solution is to remove BOM from your sources.

Osyotr
  • 784
  • 1
  • 7
  • 20