+++ date = '2025-12-29T11:10:37+08:00' draft = false license = 'MIT Licence' title = 'Clang编译器使用bits/stdc++.h头文件' tags = ['技术栈'] description = '众所周知,Clang++不自带万能头。' +++ 我的Clang版本是17.0.0,macOS版本是26.0.1。 ## 使用方法 在你使用`#include`的源文件相同文件夹里面,新建一个`stdc++.h`文件。 粘贴以下代码: (注意:这些代码来自GNU官网的GCC编译器项目目录,我于今天上午10点左右下载,注意该代码是否过时。) (在[GNU官网](https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/include/precompiled/stdc%2B%2B.h;h=54baed43a5e2d4775b11429837c7c2e0cf05fc13;hb=HEAD)检查。) ```c // C++ includes used for precompiling -*- C++ -*- // Copyright (C) 2003-2025 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the // Free Software Foundation; either version 3, or (at your option) // any later version. // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // Under Section 7 of GPL version 3, you are granted additional // permissions described in the GCC Runtime Library Exception, version // 3.1, as published by the Free Software Foundation. // You should have received a copy of the GNU General Public License and // a copy of the GCC Runtime Library Exception along with this program; // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see // . /** @file stdc++.h * This is an implementation file for a precompiled header. */ // 17.4.1.2 Headers // C // Don't include cassert, it's not suitable for PCH or header unit. #include #include #include #include #include #include #include #if __cplusplus >= 201103L #include #if __cplusplus < 201703L #include #endif #endif // C++ // #include // #include #include #include #include #include #include #include #include #include #include #include #if __cplusplus >= 201103L #include #include #include #include #include #include #include #include #endif #if __cplusplus >= 201402L #endif #if __cplusplus >= 201703L #include // #include #include #include #include #endif #if __cplusplus >= 202002L #include #include #include #include #include #include #include #include #if __cpp_impl_coroutine # include #endif #endif #if __cplusplus > 202002L #include #include #endif // #if _GLIBCXX_HOSTED // Clang++就要强制使用捏 // C #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if __cplusplus >= 201103L #include #include #include #include #if __cplusplus < 201703L #include #include #include #include #endif #endif // C++ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if __cplusplus >= 201103L #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #endif #if __cplusplus >= 201402L #include #endif #if __cplusplus >= 201703L #include #include // #include #include #include #include #include #endif #if __cplusplus >= 202002L #include #include #include #include #include #include #include #include #include #include #include #include #include #include #endif #if __cplusplus > 202002L #include #include // #include // 好像没有捏 // #include // 好像没有捏 #include #include // #include // 好像没有捏 // #include // 好像没有捏 #include // #include // 好像没有捏 #endif #if __cplusplus > 202302L // #include // 好像没有捏 // #include // 好像没有捏 // #include // 好像没有捏 // #include // 好像没有捏 #include #endif // #endif // Clang++就要强制使用捏 // HOSTED ``` 注意:有带“捏”注释的代码行是我的修改部分。 - 如果您需要自行修改,首先需要把`#if _GLIBCXX_HOSTED`与对应的`#endif`注释掉(对应上面的`// Clang++就要强制使用捏`代码行),否则不能用于Clang; - `// 好像没有捏`注释的代码行是当前GCC编译器具备,而Clang编译器不具备的头文件。如果您想要自己测试,请随便写一个Hello World程序,先不用注释掉那些`#include`语句(注意上面那步注释是必须执行的),然后`#include"stdc++.h"`,不断编译,根据编译器的报错来选择性注释掉没有的头文件。 - 再次注意:原有源文件的`#include`需要全部改成`#include"stdc++.h"`。这会让您的编译器在源代码文件夹里查找stdc++.h,而不是错误地在编译器目录查找。 - 之后使用`clang++ <源代码目录>`指令编译就有用啦。