如何在Xcode中引入<bits/stdc++.h>头文件

45

我试图在我的C++代码中包含头文件bits/stdc++,但似乎编译器不支持它。有没有办法让它工作?

我使用的是OS X Yosemite 10.10.2和Xcode 6.1.1。


10
你不应该直接包含这个标题。你认为自己为什么需要它? - πάντα ῥεῖ
2
@Omar 除非你想要明显增加编译时间,否则请使用与你所用函数/类必需的标准头文件,而不是一些内部的G++文件。 - deviantfan
2
@Omar:“我曾经包含它,而不是……”这是错误的。出现在“bits”目录中的头文件旨在将C++编译器实现与您的实际机器和操作系统环境绑定在一起。这些通常由C++标准库头文件的更高级别实现包含,有时仅在特定条件下(#ifdef)包含。 - πάντα ῥεῖ
10
推荐阅读:为什么不应该使用 #include <bits/stdc++.h>? - Lightness Races in Orbit
4
您的要求/需求是错误的,这就是为什么。不行。 - Lightness Races in Orbit
显示剩余8条评论
24个回答

38

8
这是一个糟糕的建议,将包括完整路径的哈希操作是极其可怕的行为。 - Alessandro Teruzzi
在使用任何从互联网(或其他不可信来源)下载的内容之前,一定要仔细检查。 - user4581301

31

由于bits/stdc++是GNU GCC扩展,而OSX使用clang编译器。

你需要在/usr/local/include目录下创建bits目录,然后在其中创建一个头文件stdc++.h,并将此链接里的内容复制到该文件中。

然后就可以按预期编译了。


由于在Mac OSX上,默认情况下/usr目录是隐藏的。

  1. 打开Finder。
  2. 点击菜单栏中的前往,然后点击前往文件夹或直接按Command+Shift+G。
  3. 输入路径/usr/local/include
  4. 现在按照上述提到的步骤进行操作。

(更新:对于最新的OS X,您需要在local目录下创建一个名为include的文件夹,再在其中创建一个bits文件夹,并将代码复制到bits文件夹中。)


5
使用以上路径对我来说不起作用。但是使用这个路径可以运行: "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/" - GvSharma
对于我来说,我首先需要运行“xcode-select -p”来验证命令行工具文件夹的位置(它是/Library/Developer/CommandLineTools)。然后我找到了其他文件夹/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include。 - undefined

22

Mac OS X 10.9+不再使用GCC/libstdc++,而是使用libc++和Clang。

XCode 6.0.1更新后,头文件现在位于此处:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1

因此,请从这里获取stdc++.h文件,然后在上述长地址中创建bits目录,并将stdc++.h文件复制到bits目录中。


2
至少不要称其为“bits/stdc++.h”。那是libstdc++中的一个实现头文件的名称。请将其命名为“myHeaderWithAllOfTheStdlib”或其他名称。 - Lightness Races in Orbit
1
不起作用。路径错误。请查看下面的答案。 - Manan Bordia

12

你无法这样做。X-Code使用带有Clang编译器的LLVM工具链,而<bits/stdc++>是专门针对GNU编译器工具链的。

其次,你本来就不应该使用那个头文件,正如其他人所述。


4
通过在"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/"路径下创建一个名为bits的文件夹,我可以添加stdc++.h文件,并且它可以正常工作。 - Shahriar Nasim Nafi

7

在将bits/stdc++.h添加到您的mac os平台之前,首先需要确定您的包含文件的位置。要找出在您的mac环境中使用哪个包含文件。

  • 在终端中运行以下命令:

    echo "" | gcc -xc - -v -E

这将提供您平台上gcc环境的详细信息。

Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
"/Library/Developer/......."
ignoring nonexistent directory 
"/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/local/include"
ignoring nonexistent directory 
"/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/include
 /Library/Developer/CommandLineTools/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include


/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks (framework directory)
End of search list.
# 1 "<stdin>"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 361 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "<stdin>" 2
  • 进入包含路径,例如:/usr/local/include 创建一个bits文件夹并添加stdc++.h文件。

1
谢谢。这很有帮助。在竞赛编程中,只需导入<bits/stdc++.h>就足够了。 - Kangkan Lahkar
也许在某些地方可以,但在其他地方使用它只会给你带来麻烦。不要在真正的代码中使用 <bits/stdc++.h> - user4581301

3

如果以上解决方案都不适用于您。

尝试在其中创建一个名为bits的文件夹。

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include

你可以使用终端,但如果你想使用Finder,请按下cmd + shift + G键(⌘ + ⇧ + G)进入路径菜单并粘贴上面的路径。

然后创建一个名为stdc++.h的文件,并将此处提供的stdc++.h文件代码粘贴到其中。


每次您升级Xcode,这个更改可能会丢失。 - HangarRash

1

只需在您的文件目录中创建一个类似于bitsStdcpp.hpp的头文件, 将以下代码添加到该文件中, 并使用#include "bitsStdcpp.hpp"代替#include <bits/stdc++.h>。

#include <stdio.h>

using namespace std;


#ifndef _GLIBCXX_NO_ASSERT
  #include <cassert>
  #endif
  #include <cctype>
  #include <cerrno>
  #include <cfloat>
  #include <ciso646>
  #include <climits>
  #include <clocale>
  #include <cmath>
  #include <csetjmp>
  #include <csignal>
  #include <cstdarg>
  #include <cstddef>
  #include <cstdio>
  #include <cstdlib>
  #include <cstring>
  #include <ctime>

  #if __cplusplus >= 201103L
  #include <ccomplex>
  #include <cfenv>
  #include <cinttypes>
  #include <cstdbool>
  #include <cstdint>
  #include <ctgmath>
  #include <cwchar>
  #include <cwctype>
  #endif

  // C++
  #include <algorithm>
  #include <bitset>
  #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 <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 /* bitsStdcpp_hpp */

1

正如其他人所说的那样,这是因为Mac OS X 10.9+不再使用GCC/libstdc++而是使用libc++和Clang。

因此,解决这个问题的另一种方法是将您的默认mac编译器从clang更改为gcc

正如您在this image中看到的那样,默认的mac编译器是clang

因此,在这里我们可以安装gcc

第一步

运行以下命令安装gcc(假设您的机器上已安装homebrew):

$ brew install gcc

第二步

安装完gcc后,运行以下命令查看版本号

$ gcc --version

第三步

现在当我们运行g++-version时,它将使用gcc编译器,但我们希望在运行g++时使用gcc编译器。为此,我们将创建一个g++到gcc的符号链接:

$ cd /usr/local/bin

现在通过运行以下命令创建符号链接:
$ ln -s g++-[version] g++

(将版本替换为您当前安装的版本。)

现在重新启动终端以使更改生效,并运行g ++,它将使用gcc编译器。 应该看起来像这样


0

对于新的Mac M1用户,只需进入应用程序,在Xcode中右键单击并转到“显示包内容”,

/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/

在MacOSX10.14.sdk中选择带有文件夹的项,不要点击别名,在其中创建一个名为“bits”的文件夹和一个名为“stdc++.h”的文件。然后它就会起作用。

0

在Mac上设置VS Code用于C++,并像Windows一样在Mac上使用#include <bits/stdc++.h>

  1. 安装Homebrew
  2. 使用brew install gcc命令安装GCC
  3. 然后在/usr/local/include位置创建一个文件夹
  4. 创建一个名为stdc++.h的文件,然后在******之间粘贴下面的代码


// C++ includes used for precompiling -*- C++ -*-
// Copyright (C) 2003-2015 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
// #ifndef _GLIBCXX_NO_ASSERT
// #include <cassert>
// #endif
// #include <cctype>
// #include <cerrno>
// #include <cfloat>
// #include <ciso646>
// #include <climits>
// #include <clocale>
// #include <cmath>
// #include <csetjmp>
// #include <csignal>
// #include <cstdarg>
// #include <cstddef>
// #include <cstdio>
// #include <cstdlib>
// #include <cstring>
// #include <ctime>

// #if __cplusplus >= 201103L
// #include <ccomplex>
// #include <cfenv>
// #include <cinttypes>
// #include <cstdalign>
// #include <cstdbool>
// #include <cstdint>
// #include <ctgmath>
// #include <cwchar>
// #include <cwctype>
// #endif

// // C++
// #include <algorithm>
// #include <bitset>
// #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>
// #include <unordered_map>
// #include <unordered_set>

// #if __cplusplus >= 201103L
// #include <array>
// #include <atomic>
// #include <chrono>
// #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>

// #endif

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
//#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#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>
#include <unordered_map>
#include <unordered_set>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#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>

#endif


然后按照以下步骤操作:
在VS Code中打开用户级别设置:
1. 按下 cmd + shift + p。 2. 在命令面板中输入并选择 "Preferences: Open Settings (JSON)"。这将打开用于用户级别设置的 settings.json 文件,或以某种方式在VS Code中打开 settings.json 文件。 3. 在代码中添加或修改这两行。

"C_Cpp.default.includePath": [
     "/usr/local/include" // Add global include paths here
 ],
"C_Cpp.default.compilerPath": "/usr/bin/g++" // Set the default compiler path

确保你的"C_Cpp.default.compilerPath"指向正确的GCC/G++编译器,并将"C_Cpp.default.includePath"设置为包含你想要的全局包含路径。
保存设置文件。
重新启动Visual Studio Code。
完成了,你可以运行cpp文件。
现在是额外的部分。只需下载一个名为Competitive Coding Helper (cph)的新扩展。然后进入设置,搜索cph。在Cph > Language > Cpp:Args部分添加 -std=c++17。你可以使用这个扩展,它是最好的C++扩展。完成。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接