c++ - Cmake target_precompile_headers 导致重新定义错误

标签 c++ cmake precompiled-headers

使用 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>
没有别的。
感谢您的任何建议!

最佳答案

原来这是一个bug in GCC .
我最终包装了我们 pch.h 的内容在包括 guard :

// 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。

关于c++ - Cmake target_precompile_headers 导致重新定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64030325/

相关文章:

c++ - 使用 Android NDK 构建 mariadb 客户端

c++ - 关于RAII、STL pop、PIMPL的基本问题

c++ - InstallShield 先决条件(错误 -7067)

c++ - 32 位共享对象的链接问题

cmake - CMake 项目中的组构建目标

c++ - 为什么使用基于 boost 的库时生成的预编译文件如此大?

c++ - 如果 stdafx.h 中已包含特定 header - 我是否需要(必须/应该)将其显式包含在 .cpp 文件中?

c++ - 卡在螺旋三角形

c++ - 对在Boost.statechart中完成的所有正交状态使用react

compilation - 误删除了.pch文件