首页 > 编程语言 >ERROR: <bits/stdc++.h>, 'cstdalign' file not found, running C++17

ERROR: <bits/stdc++.h>, 'cstdalign' file not found, running C++17

时间:2022-08-29 07:55:12浏览次数:125  
标签:std 13 sheet 17 share C++ running data se


Modified 1 year, 1 month ago Viewed 9k times 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!

Share Improve this question   edited Jul 14, 2020 at 0:44     asked Jul 13, 2020 at 13:23 user avatar Shravan 7511 silver badge77 bronze badges Show 7 more comments

5 Answers

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; 
} 
Share Improve this answer   edited May 19, 2021 at 13:14     answered Jul 13, 2020 at 13:31 user avatar yaodav 1,05688 silver badges2626 bronze badges
  • 2 Yes, I realise that does work. But I was wondering why the error I indicated when we use <bits/stdc++.h> was coming. Any idea?  – Shravan  Jul 13, 2020 at 13:37
  • 1 <bits/stdc++.h> 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 <cstdalign>"  – yaodav  Jul 13, 2020 at 13:40
  •   I've added some more info, if that helps!  – Shravan  Jul 14, 2020 at 0: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, 2020 at 5:59
  •   solved the issue, and subsequent issue comes again, so I continue comment out the error line. It worked!  – Jackson  Feb 3 at 10:37
Add a comment 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

Share Improve this answer   answered Sep 14, 2020 at 17:45 user avatar Himanshu Tanwar 36822 silver badges1616 bronze badges
  • 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, 2020 at 17:47
Add a comment 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!

Share Improve this answer   edited Dec 24, 2020 at 0:03     answered Nov 22, 2020 at 18:50 user avatar Chrissi 16111 silver badge66 bronze badges
  •   Any answer that recommends either editing or explicitly using the bits/stdc++.h header is, IMHO, utterly misleading.  – Adrian Mole  Nov 22, 2020 at 19:26
  •   According to this answer MacOSX does not have the file uchar.h so you cannot #include <cuchar>. Maybe you can download it e.g. from here and paste it to the right directory. I haven't tried this yet.  – Chrissi  Nov 22, 2020 at 22:43 
Add a comment 1

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

Share Improve this answer   edited Jul 13, 2021 at 13:17 user avatar Cristik 29.2k2424 gold badges8686 silver badges122122 bronze badges answered Jul 13, 2021 at 8:52 user avatar Archies Singh 1111 bronze badge Add a comment 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

标签:std,13,sheet,17,share,C++,running,data,se
From: https://www.cnblogs.com/flyingsir/p/16634686.html

相关文章

  • LeetCode 1779. Find Nearest Point That Has the Same X or Y Coordinate
    原题链接在这里:https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/题目:Youaregiventwointegers, x and y,whichrepresen......
  • c++学习案例:猜数字游戏
    最近在学习C++,遇到了一个案例:猜数字游戏案例要求:系统生成一个范围在1-100的随机整数,用户有5次猜数字的机会,当用户猜的数字大于或小于生成的值时进行提示,5次没猜对则失败,......
  • C++中 sort()和priority_queue()中的自定义比较
    C++sort/priority_queue自定义比较sort/priority_queue的自定义比较是有区别的:sort是自定义函数;priority_queue则是自定义结构体,结构体里面重载()实现自定义比较......
  • C++ 性能小测 1 二维数组的遍历效率
    C++性能小测1二维数组的遍历效率遍历二维数组时,常规思路是使用一个嵌套循环。一方面,由于CPU使用了分支预测技术,因此通常将循环次数最多循环的放在最内层。另一方面,由......
  • C++一些新的特性的理解(二)
    1C++11多线程thread重点:join和detach的使用场景thread构造函数参数绑定c函数绑定类函数线程封装基础类互斥锁mutexconditionnotify、waitlock_guard/unique_l......
  • C++【多线程编程】之【初识线程】
    1.用c++11的thread库还是用pthread库?至于选择哪种多线程编程方案,需要根据你的实际项目、运行平台、团队协作等因素来考虑。一般而言,如果使用的是Linux操作系统,那么可以......
  • Google C++ Style Guide 学习
    目录参考参考http://home.ustc.edu.cn/~hqp/RootClass/AddFiles2/GoogleC++StyleGuide.pdfhttps://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styl......
  • P3955 [NOIP2017 普及组] 图书管理员
    P3955[NOIP2017普及组]图书管理员-洛谷|计算机科学教育新生态(luogu.com.cn)  #include<iostream>#include<cstdio>#include<cstring>#include<algorithm......
  • 【C++-笔记】override与final说明符
    在effectiveC++中提到C++没有Java那样的finalclasses的禁止派生的机制,遂想到在C++Primer中好像提到过final说明符,正好就连带着override说明符一起复习一下了。简介首......
  • C++ 用函数打印员工的平均工资
    #include<iostream>#include<windows.h>#include<string>usingnamespacestd;floataverageSalary(intn[],inti){floatsum=0;for(intx=0;x......