4

I'm trying to run a piece of code in Visual Studio Code, on macOS Catalina. The code:

#include <bits/stdc++.h>
using namespace std;

int main() 
{ 
    // Create an empty vector 
    vector<int> vect;  
     
    vect.push_back(10); 
    vect.push_back(20); 
    vect.push_back(30); 
  
    for (int x : vect) 
        cout << x << " "; 
  
    return 0; 
} 

When I try to run the code using the coderunner extension, I get the error:

[Running] cd "/Users/VSC_Files/" && g++ -std=c++17 helloworld.cpp -o helloworld && "/Users/VSC_Files/"helloworld
In file included from helloworld.cpp:1:
/usr/local/include/bits/stdc++.h:57:10: fatal error: 'cstdalign' file not found
#include <cstdalign>
         ^~~~~~~~~~~
1 error generated.

[Done] exited with code=1 in 1.465 seconds

Apparently this is an error only for C++11, then why am I getting this error? I have the latest updated Xcode version and the latest stable build of VSCode too.

EDITED AND ADDED LATER

Also, I would like to add that I manually added the bits/stdc++.h file, and that it wasn't there from before.

Also, when I change g++ -std=c++17 to just g++ when running, the program runs and shows the correct output. With a warning as shown below.
helloworld.cpp:13:15: warning: range-based for loop is a C++11 extension [-Wc++11-extensions]

Is there an issue with the default C++ version in mt laptop? Please help!

Shravan
  • 75
  • 1
  • 1
  • 10
  • 10
    dont ever use [#include ](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – yaodav Jul 13 '20 at 13:26
  • 1
    @yaodav Yes, I realise that does work. But I was wondering why the error I indicated when we use was coming. Any idea? – Shravan Jul 13 '20 at 13:36
  • I'm amazed that a search for `bits/stdc++.h` on SO does not give you an almost daily hit for a similar question – rioV8 Jul 13 '20 at 14:48
  • 2
    Reopened. This header is of course not portable, but if it's there, it should work... – HolyBlackCat Jul 13 '20 at 15:04
  • What does `g++ --version` print for you? – HolyBlackCat Jul 13 '20 at 15:06
  • @HolyBlackCat This is what comes. `Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 11.0.3 (clang-1103.0.32.59) Target: x86_64-apple-darwin19.4.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin` – Shravan Jul 13 '20 at 22:44
  • @HolyBlackCat Also, I would like to add that I added the `bits/stdc++.h` file, and that it wasn't there from before. – Shravan Jul 13 '20 at 22:47
  • @Shravan It should've been mentioned in the question. Where did you get the file from? – HolyBlackCat Jul 13 '20 at 22:55
  • In any case, this is not how things work. This file a GCC-specific feature. If you want to use it (you shouldn't), you'll need to install GCC. (The `g++` you have is not GCC, but an Apple Clang in disguise.) – HolyBlackCat Jul 13 '20 at 23:19
  • @HolyBlackCat So this isn't an issue of the C++ version installed in my laptop? Because, when I change `g++ -std=c++17` to just `g++` when running, the program runs. With a warning as shown below. `warning: range-based for loop is a C++11 extension [-Wc++11-extensions]` – Shravan Jul 14 '20 at 00:19
  • @Shravan I'm not sure why it works without `-std=c++17`, but it doesn't really matter. – HolyBlackCat Jul 14 '20 at 00:22
  • I guess the question is aimed at getting a competitive programming workspace running, not to create a full fledged software project. The comments about bad practices are not that useful in this respect. – Werner Altewischer Jan 02 '22 at 08:31

5 Answers5

4

#include<bits/stdc++.h> is an internal header for the GCC and you are not supposed to use it, it's not portable.

remvoe the #include<bits/stdc++.h> insted write #include<vector> and #include<iostream> also remove using namespace std it considered bad practice so you code shod look like this:

#include <vector>
#include <iostream>

int main() 
{ 
    // Create an empty vector 
    std::vector<int> vect;  
     
    vect.push_back(10); 
    vect.push_back(20); 
    vect.push_back(30); 
  
    for (int x : vect) 
        std::cout << x << " "; 
  
    return 0; 
} 
yaodav
  • 1,126
  • 12
  • 34
  • 2
    Yes, I realise that does work. But I was wondering why the error I indicated when we use was coming. Any idea? – Shravan Jul 13 '20 at 13:37
  • 1
    is internal header could be that part of the header files its including are not in the macOS c++lib is also indecate that from the error message - he cant find "#include " – yaodav Jul 13 '20 at 13:40
  • I've added some more info, if that helps! – Shravan Jul 14 '20 at 00:46
  • According to what version of g++ you have 4.2.1 I don't think it supports c++ 11 try updating your gcc version. – yaodav Jul 14 '20 at 05:59
  • solved the issue, and subsequent issue comes again, so I continue comment out the error line. It worked! – Jackson Feb 03 '22 at 10:37
3

I was having the same issue. First I installed gcc via homebrew

brew install gcc

To avoid conflict with the existing gcc (and g++) binaries, homebrew names the binary suffixed with version. At time of this comment, the latest was gcc-10.

You dont have to copy the bits/stdc++.h after this. Just compile using g++-<major-version-number> instead of g++, which would use the homebrew installed binary instead of the default osx one. For me it is

g++-10 -Wall -O2 -std=c++11 test.cpp -o test

To check the binary name that homebrew installed you can look in the /usr/local/bin directory because thats where homebrew installs packages.

Also, make sure that usr/local/bin is before /usr/bin in your $PATH

Himanshu Tanwar
  • 378
  • 2
  • 18
  • 1
    Also, I am not disagreeing with the other comments saying that we should not use `bits/stdc++.h` and `using namespace std;` in our code. I put this here, because its good to know how to make it work if we have to use it. – Himanshu Tanwar Sep 14 '20 at 17:47
2

I too got these error, and I solved these error by commenting out the <cstdalign> part.

After you comment out these line it will give 2 more errors - cuchar not found, and <memory_resources> not found, comment both of them using " //" . It will not harm you stdc++.h file . And it will definitely work.

enter image description here

Cristik
  • 30,989
  • 25
  • 91
  • 127
1

For me it worked to comment the following lines out in the file bits/stdc++.h:

// #include <cstdalign>

...

// #include <cuchar>

The file is located in /usr/local/include/bits/ as well as in /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/bits. I don't know if you have to do it in both files but the first one worked for me!


Update Dec 24: If you use the g++ with the command line, there is no need to move any file into any directory!

For example when I use the command: g++ custom_file.cpp it works fine! In addition you can add -std=c++11 to have the most needed functions. Also I don't have to move the bits/stdc++.h file after Xcode get's an update.

I hope this helps!

Chrissi
  • 189
  • 1
  • 8
  • Any answer that recommends either editing or explicitly using the `bits/stdc++.h` header is, IMHO, utterly misleading. – Adrian Mole Nov 22 '20 at 19:26
  • According to [this](https://stackoverflow.com/a/60671901/12165405) answer MacOSX does not have the file `uchar.h` so you cannot `#include `. Maybe you can download it [e.g. from here](https://github.com/lattera/glibc/blob/master/wcsmbs/uchar.h) and paste it to the right directory. I haven't tried this yet. – Chrissi Nov 22 '20 at 22:43
0

I am sharing steps to execute with sample code for array rotation which works with following commands

g++-10 -Wall -O2 -std=c++11 rotatearrayusingdeque.cpp 

Then a.out file gets generated.

./a.out

sample code:

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() 
{
    int n,r,i,j,temp=0,n1;
    deque<int> v;
    cin>>n>>r;  
    for(i=0;i<n;i++)
    {
        cin>>n1;
        v.push_back(n1);
        
    }
    for(j=0;j<r;j++)
    {
        temp = v.front();
        v.pop_front();
        v.push_back(temp);
    }
    for(auto x:v)
    {
        cout<<x<<" ";
    }
    cout<<endl;

    return 0;
}

Now, there will not be any error, Thanks

Reza Rahemtola
  • 1,182
  • 7
  • 16
  • 30