43

I have tried to include the header file bits/stdc++ in my C++ code, but it seems the compiler doesn't support it. Is there any way to make it work?

I use OS X Yosemite 10.10.2 and Xcode 6.1.1.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Omar
  • 801
  • 3
  • 10
  • 20
  • 10
    You aren't supposed to include this header directly. Why do you think you need it? – πάντα ῥεῖ Mar 11 '15 at 18:06
  • I used to include it instead of including many other libraries and save myself a time – Omar Mar 11 '15 at 18:09
  • 2
    @Omar Except you´ll probably get notably increased compile time. Use the standard headers necessary for your used functions/classes, and not some internal G++ file – deviantfan Mar 11 '15 at 18:13
  • 2
    @Omar _"I used to include it instead of ..."_ That's wrong. The headers appearing in the `bits` directory are meant to bind the c++ compiler implementation with your actual machine and OS environment. These are usually included by the higher level implementations of the c++ standard library headers, sometimes only under certain conditions (`#ifdef`'s) – πάντα ῥεῖ Mar 11 '15 at 18:13
  • this library suits my work environment because i need speed in writing, and the high level implementation won't cause any harm – Omar Mar 11 '15 at 19:11
  • the thing is, i used to include it on windows, i need a way to make it work on mac – Omar Mar 11 '15 at 19:12
  • just copy the file to your working directory and include it... – MartianMartian Sep 20 '18 at 04:02
  • 10
    **Recommended reading:** [Why should I not #include ?](https://stackoverflow.com/q/31816095/560648) – Lightness Races in Orbit Jun 15 '19 at 13:35
  • 4
    tl;dr your requirement/need is wrong and this is why. No. – Lightness Races in Orbit Jun 15 '19 at 13:36
  • 1
    Does this answer your question? [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Ulrich Eckhardt Feb 21 '22 at 18:19
  • @UlrichEckhardt I don't see how that's a duplicate. Whether one should, or should not, include a header is different from *how* to include that header. – cigien Feb 21 '22 at 18:22
  • 1
    I'd be tempted to close this, because the answer to this is a clear "Don't!". The question is based on a flawed assumption and the only accepted answer is an atrocity (yes, my opinion) that discourages understanding of the issue. I'm therefore convinced that SO is better off without this Q than with it. – Ulrich Eckhardt Feb 21 '22 at 18:32
  • The speed in writing argument is fallacious unless you use the header they way it was intended: to facilitate pre-compiled headers. If you include it without taking the proper steps, your build times increase dramatically, often taking an order of magnitude longer, and quickly eat up the time you saved by not typing the individual headers the program requires. – user4581301 Mar 21 '22 at 19:53

23 Answers23

36

You can do it by copying stdc++.h file from here: https://gist.github.com/reza-ryte-club/97c39f35dab0c45a5d924dd9e50c445f

Then you can include the file in your c++ file like this:

 //suppose the file is in your home folder, here my username is reza
 #include "./stdc++.h"
Reza
  • 2,896
  • 4
  • 26
  • 37
30

Since, bits/stdc++ is a GNU GCC extension, whereas OSX uses the clang compiler.

You have to create bits directory inside /usr/local/include and then make a header file stdc++.h inside bits and paste the contents of this gist inside it. Then, it should compile as expected.


Since, /usr directory is hidden by default on Mac OSX.

  1. Open Finder.
  2. Click Go on menu bar then click Go to folder or Press Command+Shift+G directly.
  3. Enter the path /usr/local/include
  4. Now proceed as mentioned above.

(UPDATE: For latest OS X you need to make folder include inside local and make bits folder inside include folder and then copy paste the code inside bits folder.)

starfry
  • 9,273
  • 7
  • 66
  • 96
Sachin Kumar
  • 470
  • 6
  • 7
  • 5
    with the above path , not working for me. But with this path it is working "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/" – GvSharma Jul 22 '19 at 12:46
22

Mac OS X 10.9+ no longer uses GCC/libstdc++ but uses libc++ and Clang.

After the XCode 6.0.1 update the headers are now located here:

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

so, get the stdc++.h file from here,then creat bits directory in the above long address, and copy the file stdc++.h to the bits directory.

frankchen0130
  • 559
  • 6
  • 7
12

You can't. X-Code uses LLVM Toolchain with Clang for the compiler, while <bits/stdc++> is specific to the GNU Compiler Toolchain.

Second, you shouldn't be using that header in the first place, as stated by everyone else.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
Brendan Lesniak
  • 2,271
  • 4
  • 24
  • 48
  • 4
    But by creating a folder in "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/" named bits, i can add the stdc++.h and it worked – Shahriar Nasim Nafi Jun 09 '19 at 11:47
7

Before Adding the bits/stdc++.h to your mac os platform, the First things are to figure out where your include files are present. To figure out which include file is getting utilized within you mac environment.

  • Run this command in terminal:

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

This will provide the details of gcc environment in your platform

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
  • Go to the include path. ex: /usr/local/include Create a bits folder and add stdc++.h file.
2

In case none of the above solutions work for you.

Try creating a folder named bits in

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

[ You can definitely use terminal but if you want to use finder press cmd + shift + G keys (⌘ + ⇧ + G) to goto path menu and paste the above path ]

and create a file named stdc++.h and paste the code for stdc++.h file given here

Manan Bordia
  • 394
  • 2
  • 4
  • 12
1

Simply create a header file like bitsStdcpp.hpp file in your file directory, add the following code in that file and use #include "bitsStdcpp.hpp" instead of #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

The reason for that as stated by others also is because Mac OS X 10.9+ no longer uses GCC/libstdc++ but uses libc++ and Clang.

So one alternative way to fix that is to make your default mac compiler from clang to gcc.

As you can see in this image, the default mac compiler is clang.

So, what we can do here is install gcc!

Step 1

Run the following command to install gcc (assuming homebrew is installed on your machine):

$ brew install gcc

Step 2

After gcc is installed note the version by running the following command

$ gcc --version

Step 3

So now when we run g++-version it will use the gcc compiler, but we want it to use gcc compiler when we run g++. For that we are going to create a symbolic link of g++ to gcc:

$ cd /usr/local/bin

Now create a symbolic link by running this command:

$ ln -s g++-[version] g++

(Replace version by your current installed.)

Now restart the terminal for the changes to take effect and run g++ , it will use gcc compiler. It should look like this.

Phortuin
  • 770
  • 4
  • 20
saketkr
  • 19
  • 2
0

for new mac M1 users, just go to applications, in xcode right click and go to Show Package contents,

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

in MacOSX10.14.sdk select the one with folder don't click on alias, make a folder bits and a file stdc++.h in it. Then it will work.

-1

The standard GNU compiler, G++ Does not directly support this header so we include it in the required location using the following steps :

  1. cd /usr/local/include

  2. mkdir bits

  3. nano stdc++.h

then copy the code of the header file from here .

Hope that helps ! :D

smriti
  • 105
  • 1
  • 5
-1

After creating a symbolic link to g++ at /usr/local/bin/ it should work.

Command for symbolic link:-

sudo ln -s $(which g++-11) /usr/local/bin/g++

If the bin directory is missing, then create the bin directory first.

lee-m
  • 2,269
  • 17
  • 29
-1

If the problem still persists after trying out all the aforementioned solutions

1] Go to folder

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

2] Create a new folder and name it bits

3] Copy the stdc++.h file into the newly created bits folder

Link for stdc++.h file https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/precompiled/stdc%2B%2B.h

Geetesh
  • 1
  • 1
  • 1
    Please don't post duplicate answers. – HangarRash Sep 01 '23 at 04:49
  • All the above answers have a different path and I posted a different path. Those previous answers didn't work for me and only this worked. There are some other answers with the same path as me (which I noticed just now), but all of them has negative votes. If you downvote this, people like me who suffering with a similar problem cannot find the correct solution – Geetesh Sep 02 '23 at 23:33
  • There is another answer with a positive upvote count that has the same path as your answer which makes yours a duplicate. The other answer is found at https://stackoverflow.com/a/75489214/20287183 – HangarRash Sep 02 '23 at 23:56
-2
  1. Open Finder.
  2. Click Go on menu bar then click Go to folder or Press Command+Shift+G directly.
  3. Enter the path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1

Now, get the stdc++.h file from here, then create bits directory in the above long address, and copy the file stdc++.h to the bits directory.

Sahil Rajput
  • 135
  • 1
  • 5
-2

1.Download the stdc++.h file from https://gist.github.com/eduarc/6....

2.In Finder CTRL + SHIFT +G and open /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/

3.Create the folder bits and copy the downloaded file here.

Shahriar Nasim Nafi
  • 1,160
  • 15
  • 19
-2
  1. Open Finder.
  2. Click Go on menu bar then click Go to folder or Press Command+Shift+G directly.
  3. Enter the path or c/p this path directly

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

  1. Then create bits directory in the above long address.
  2. Now, get the stdc++.h file from [here][1].
  3. and copy the file stdc++.h to the bits directory.
-2
  1. Copy the content of this header file to clipboard from Link: https://gist.github.com/reza-ryte-club/97c39f35dab0c45a5d924dd9e50c445f

  2. Run following in terminal :

    • mkdir /usr/local/include/bits
    • vim /usr/local/include/bits/stdc++.h
    • Switch to insert mode (press i) and Paste clipboard content
    • Save/Exit (Esc + : + w + q + Enter)
  3. Try compliation of your source code

chandan
  • 130
  • 7
-2

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

TRY THIS PATH to create the bits folder and add the stdc++.h file.

ouflak
  • 2,458
  • 10
  • 44
  • 49
-2

The basic solution is to create the bits folder and stdc++.h file where the compiler is looking for its libraries.

To find this for g++ or gcc go to the terminal and write

**g++ -print-search-dirs**

You'll get something like this:

programs: =/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
libraries: =/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.0.0

Go to the libraries location inside the include folder create a bits folder. Inside the bits folder create a stdc++.h file with these contents.

Note if you face some error like a particular header file not found please comment those lines in stdc++.h.

NPras
  • 3,135
  • 15
  • 29
-2

Header files may contain Function definitions, Data type definitions and Macros

You need to understand that in bits/stdc++.h, bits is the folder name that's useless to you right now, the header file is stdc++.h.

Step 1: Create a file named stdc++.h with following content.

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

Step 2: Add include "stdc++.h" on top of your file where you want to include this header file, Note: always put path with file name, as if your header file is in C:/Folder1 then you should write include "C:/Folder1/stdc++.h"

Harshit Jain
  • 885
  • 11
  • 15
  • if you find any errors while including any header file, that's inside `stdc++.h` header file, simply comment it out from `stdc++.h` file! Ofcourse if file is not important to you, if it's needed find other alternatives! – Harshit Jain Mar 13 '22 at 06:36
-2

If you update to MacOS 12.3, most of the existing answers on the Internet will fail. Open a folder with this:

open /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs

Or you can directly peel off Xcode layer by layer with the graphical interface. You will see two folders, one with an arrow and one without. like this

Obviously, this is caused by the change of folder path caused by MacOS update. Because the contents of the two folders are almost the same ,you can hardly find out the cause. Your Xcode can find the standard header file(like stdio.h) in the new path, but can't find the stdc++.h you added, because you added it to the old path.

Dr. Octopus
  • 161
  • 1
  • 5
-2

if you are adding the bits folder in the below path "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/" and still stuck,to fix it copy the all the c++(and comment out the c headers in stdc++.h file) headers from stdc++.h file and add it in to a simple c ++ code and run it in xcode,see the header which is creating problem and then comment it out in stdc++.h file and it will be fixed

Anuj Soni
  • 1
  • 1
-2

For latest Xcode version 13.4.x:
Goto this path

  1. Applications > Xcode > Contents > Developer > Platforms > MacOSX.platform > Developer > SDKs > MacOSX.sdk > usr > include > c++ > v1

OR

In finder press Cmd + Shift + G

and enter the following address /Applications/Xcode/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1

  1. Create a new folder with the name "bits"
  2. paste your stdc++.h file in this folder (you can get the stdc++.h file here)

In the code, you can simply add the following line to include the stdc++.h file.

#include<bits/stdc++.h>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ravi
  • 1
  • 1
-4

I have written a detailed article on Medium explaining how to resolve this issue elegantly. You may read it here.

TLDR:

Download the linked file. Go to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1 and copy the downloaded file to that directory. You can start using #include <stdcpp.h> in your projects instead of manually including all required header files.

vikram
  • 27
  • 4
  • 2
    The actual solution and explanation should be part of the stackoverflow answer, not just a reference to an external site. The code in the linked file should also be explained, not just given as is for people to include in their projects without knowledge of what it should do. – devil0150 Sep 10 '22 at 14:18
  • @devil0150 that’s ridiculous. The answer should be short and to the point, and if someone’s really interested then they may choose to read the article to understand the minute details. What’s there to explain in the code? Its literally just a bunch of #include statements that include every header file imaginable. Do you really expect me to explain the purpose of each and every header file? – vikram Sep 12 '22 at 16:03