Omar
Omar

Reputation: 861

How can I include <bits/stdc++> in Xcode

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.

Upvotes: 49

Views: 129291

Answers (24)

Aditya Suman
Aditya Suman

Reputation: 1

Solution for vscode on mac

  • install the gcc compiler with homebrew
  • change the compilerPath in settings.json file to the installed homebrew gcc
{
    "workbench.startupEditor": "none",
    "files.autoSave": "afterDelay",
    "window.newWindowProfile": "React",
    "redhat.telemetry.enabled": true,
    "workbench.colorTheme": "Quiet Light",
    "remote.SSH.remotePlatform": {
        "sandbox.4ym8ml.csb": "linux"
    },
    "C_Cpp.default.compilerPath": "/opt/homebrew/bin/g++"
}

simple solution for vscode on macos with gcc installed with homebrew

  • install the gcc compiler with homebrew
  • put below file in the .vscode folder -> it is basically changing the compilerPath from clang to g++ -> since gcc have this bits/stdc++.h header so, will work as expected
// .vscode/c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/opt/homebrew/bin/g++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm64"
        }
    ],
    "version": 4
}

Upvotes: 0

Meyank Garg
Meyank Garg

Reputation: 1

set up vs code for C++ mac and use #include <bits/stdc++.h> in mac like windows

  1. install homebrew
  2. brew install gcc
  3. then create folder in /usr/local/include location
  4. create a file name stdc++.h and then paste the below code between *******


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

then follow these instructions :

Open User-Level Settings in VS Code:

1.press cmd + shift + p . 2. Type and select "Preferences: Open Settings (JSON)" from the command palette. This will open the settings.json file for your user-level settings. or somehow open settings.json file in vs code. 3. add or modify these two lines in code


"C_Cpp.default.includePath": [
     "/usr/local/include" // Add global include paths here
 ],
"C_Cpp.default.compilerPath": "/usr/bin/g++" // Set the default compiler path
  1. Ensure that you have the correct "C_Cpp.default.compilerPath" pointing to your GCC/G++ compiler and "C_Cpp.default.includePath" set to include the global include paths you want.

  2. Save the Settings File:

  3. Restart Visual Studio Code:

done..you can run cpp file

now bonus part. just download a new extension called Competitive Coding Helper (cph). then go to settings and search cph . then in Cph>Language > Cpp:Args section add -std=c++17. you can use this extention. it's best extention for c++. done

Upvotes: -1

Reza
Reza

Reputation: 3038

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"

Upvotes: 40

Manan Bordia
Manan Bordia

Reputation: 492

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

Upvotes: 3

Ransh Anand
Ransh Anand

Reputation: 39

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.

Upvotes: -1

vikram
vikram

Reputation: 25

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.

Upvotes: -5

Raja Ram Sharma
Raja Ram Sharma

Reputation: 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.

Upvotes: -2

Ravi
Ravi

Reputation: 1

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>

Upvotes: -3

Anuj Soni
Anuj Soni

Reputation: 1

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

Upvotes: -3

Dr. Octopus
Dr. Octopus

Reputation: 189

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.

Upvotes: -3

Harshit Jain
Harshit Jain

Reputation: 968

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"

Upvotes: -3

manal arora
manal arora

Reputation: 1

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.

Upvotes: -3

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

Upvotes: -3

saketkr
saketkr

Reputation: 19

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.

Upvotes: 1

Sachin Kumar
Sachin Kumar

Reputation: 508

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.)

Upvotes: 33

sasikumar yalamaddi
sasikumar yalamaddi

Reputation: 27

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

Upvotes: 1

smriti
smriti

Reputation: 135

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

Upvotes: -1

chandan
chandan

Reputation: 148

  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

Upvotes: -3

Ravibhushan Kumar
Ravibhushan Kumar

Reputation: 395

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.

Upvotes: 7

Akash Srinivasan
Akash Srinivasan

Reputation: 119

  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.

Upvotes: -3

Shahriar Nasim Nafi
Shahriar Nasim Nafi

Reputation: 1354

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.

Upvotes: -3

Sahil Rajput
Sahil Rajput

Reputation: 153

  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.

Upvotes: -3

frankchen0130
frankchen0130

Reputation: 547

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.

Upvotes: 20

Brendan Lesniak
Brendan Lesniak

Reputation: 2321

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.

Upvotes: 15

Related Questions