Files
Home-of-CS/content/post/dev_20251229/index.md
T
2026-04-23 13:39:28 +08:00

6.7 KiB
Raw Blame History

+++ date = '2025-12-29T11:10:37+08:00' draft = false license = 'MIT Licence' title = 'Clang编译器使用bits/stdc++.h头文件' tags = ['技术栈'] description = '众所周知,Clang++不自带万能头。' +++ 我的Clang版本是17.0.0macOS版本是26.0.1。

使用方法

在你使用#include<bits/stdc++.h>的源文件相同文件夹里面,新建一个stdc++.h文件。
粘贴以下代码:
(注意:这些代码来自GNU官网的GCC编译器项目目录,我于今天上午10点左右下载,注意该代码是否过时。)
(在GNU官网检查。)

// 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
// <http://www.gnu.org/licenses/>.

/** @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 <cctype>
#include <cfloat>
#include <climits>
#include <csetjmp>
#include <cstdarg>
#include <cstddef>
#include <cstdlib>

#if __cplusplus >= 201103L
#include <cstdint>
#if __cplusplus < 201703L
#include <ciso646>
#endif
#endif

// C++
// #include <bitset>
// #include <complex>
#include <algorithm>
#include <bitset>
#include <functional>
#include <iterator>
#include <limits>
#include <memory>
#include <new>
#include <numeric>
#include <typeinfo>
#include <utility>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <initializer_list>
#include <ratio>
#include <scoped_allocator>
#include <tuple>
#include <typeindex>
#include <type_traits>
#endif

#if __cplusplus >= 201402L
#endif

#if __cplusplus >= 201703L
#include <any>
// #include <execution>
#include <optional>
#include <variant>
#include <string_view>
#endif

#if __cplusplus >= 202002L
#include <bit>
#include <compare>
#include <concepts>
#include <numbers>
#include <ranges>
#include <span>
#include <source_location>
#include <version>
#if __cpp_impl_coroutine
# include <coroutine>
#endif
#endif

#if __cplusplus > 202002L
#include <expected>
#include <stdatomic.h>
#endif


// #if _GLIBCXX_HOSTED // Clang++就要强制使用捏
// C
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>

#if __cplusplus >= 201103L
#include <cfenv>
#include <cinttypes>
#include <cstdint>
#include <cuchar>
#if __cplusplus < 201703L
#include <ccomplex>
#include <cstdalign>
#include <cstdbool>
#include <ctgmath>
#endif
#endif

// C++
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

#if __cplusplus >= 201402L
#include <shared_mutex>
#endif

#if __cplusplus >= 201703L
#include <any>
#include <charconv>
// #include <execution>
#include <filesystem>
#include <optional>
#include <memory_resource>
#include <variant>
#endif

#if __cplusplus >= 202002L
#include <barrier>
#include <bit>
#include <compare>
#include <concepts>
#include <format>
#include <latch>
#include <numbers>
#include <ranges>
#include <span>
#include <stop_token>
#include <semaphore>
#include <source_location>
#include <syncstream>
#include <version>
#endif

#if __cplusplus > 202002L
#include <expected>
#include <flat_map>
// #include <flat_set> // 好像没有捏
// #include <generator> // 好像没有捏
#include <mdspan>
#include <print>
// #include <spanstream> // 好像没有捏
// #include <stacktrace> // 好像没有捏
#include <stdatomic.h>
// #include <stdfloat> // 好像没有捏
#endif

#if __cplusplus > 202302L
// #include <debugging> // 好像没有捏
// #include <inplace_vector> // 好像没有捏
// #include <text_encoding> // 好像没有捏
// #include <stdbit.h> // 好像没有捏
#include <stdckdint.h>
#endif

// #endif // Clang++就要强制使用捏
// HOSTED

注意:有带“捏”注释的代码行是我的修改部分。

  • 如果您需要自行修改,首先需要把#if _GLIBCXX_HOSTED与对应的#endif注释掉(对应上面的// Clang++就要强制使用捏代码行),否则不能用于Clang
  • // 好像没有捏注释的代码行是当前GCC编译器具备,而Clang编译器不具备的头文件。如果您想要自己测试,请随便写一个Hello World程序,先不用注释掉那些#include语句(注意上面那步注释是必须执行的),然后#include"stdc++.h",不断编译,根据编译器的报错来选择性注释掉没有的头文件。
  • 再次注意:原有源文件的#include<bits/stdc++.h>需要全部改成#include"stdc++.h"。这会让您的编译器在源代码文件夹里查找stdc++.h,而不是错误地在编译器目录查找。
  • 之后使用clang++ <源代码目录>指令编译就有用啦。