在使用Cmake target_precompile_headers时,我们会遇到很多重新定义错误,比如
/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’还有不计其数的其他函数,都来自于std库函数。
我们的Cmake设置是尽可能基本的,我们使用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)我们有一个src和一个pch文件夹。系统是Ubuntu 20。看起来这个问题应该很常见,但到目前为止我们什么也没发现。预编译头仅为
#pragma once
#include <iostream>别无他法。
谢谢你的建议!
发布于 2021-06-24 01:03:19
事实证明,这是一个bug in GCC。我最终将pch.h的内容包装在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另一种解决方案是从源代码中删除BOM。
https://stackoverflow.com/questions/64030325
复制相似问题