【问题标题】:ERROR: <bits/stdc++.h>, 'cstdalign' file not found, running C++17错误:<bits/stdc++.h>,未找到“cstdalign”文件,正在运行 C++17
【发布时间】:2020-11-02 16:55:18
【问题描述】:

我正在尝试在 macOS Catalina 上的 Visual Studio 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; 
} 

当我尝试使用 coderunner 扩展 运行代码时,我收到错误:

[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

显然这只是 C++11 的错误,那我为什么会收到这个错误呢?我也有最新的 Xcode 更新版本和 VSCode 的最新稳定版本。

稍后编辑和添加

另外,我想补充一点,我手动添加了 bits/stdc++.h 文件,而它以前不存在。

另外,当我在运行时将 g++ -std=c++17 更改为 g++ 时,程序运行并显示正确的输出。带有如下所示的警告。
helloworld.cpp:13:15: warning: range-based for loop is a C++11 extension [-Wc++11-extensions]

mt 笔记本电脑的默认 C++ 版本有问题吗?请帮忙!

【问题讨论】:

  • 永远不要使用#include <bits/stdc++.h>
  • @yaodav 是的,我意识到这确实有效。但我想知道为什么我在使用 时指出的错误即将到来。有什么想法吗?
  • 我很惊讶在 SO 上搜索 bits/stdc++.h 并没有让您几乎每天都遇到类似问题
  • 重新打开。这个头当然是不可移植的,但如果它在那里,它应该可以工作......
  • g++ --version 为您打印什么?

标签: c++ xcode c++11 visual-studio-code c++17


【解决方案1】:

我遇到了同样的问题。首先我通过自制软件安装了 gcc

brew install gcc

为避免与现有 gcc(和 g++)二进制文件发生冲突,自制软件以版本为后缀命名二进制文件。在发表此评论时,最新的是 gcc-10。

您不必在此之后复制bits/stdc++.h。只需使用g++-&lt;major-version-number&gt; 而不是g++ 进行编译,这将使用自制软件安装的二进制文件而不是默认的osx 二进制文件。对我来说是

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

要检查 homebrew 安装的二进制名称,您可以查看 /usr/local/bin 目录,因为那是 homebrew 安装软件包的地方。

另外,请确保 usr/local/bin 在您的 $PATH 中位于 /usr/bin 之前

【讨论】:

  • 另外,我不同意其他 cmets 说我们不应该在代码中使用 bits/stdc++.husing namespace std;。我把它放在这里,因为如果我们必须使用它,知道如何使它工作是件好事。
【解决方案2】:

#include&lt;bits/stdc++.h&gt; 是 GCC 的内部标头,您不应该使用它,它不可移植。

删除#include&lt;bits/stdc++.h&gt; 插入写#include&lt;vector&gt;#include&lt;iostream&gt; 同时删除 using namespace std 它被认为是不好的做法 所以你的代码看起来像这样:

#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; 
} 

【讨论】:

  • 是的,我意识到这确实有效。但我想知道为什么我在使用 时指出的错误即将到来。有什么想法吗?
  • 是内部头文件可能是它所包含的部分头文件不在 macOS c++lib 中,从错误消息中也无法找到 - 他找不到“ #include "
  • 我已经添加了更多信息,如果有帮助的话!
  • 根据你的 g++ 版本 4.2.1 我不认为它支持 c++ 11 尝试更新你的 gcc 版本。
【解决方案3】:

对我来说,它可以在文件 bits/stdc++.h 中注释以下行:

// #include <cstdalign>

...

// #include <cuchar>

该文件位于/usr/local/include/bits//Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/bits。 我不知道您是否必须在两个文件中都这样做,但第一个对我有用!


12 月 24 日更新: 如果你在命令行中使用g++,则无需将任何文件移动到任何目录!

例如,当我使用命令时:g++ custom_file.cpp 它工作正常! 此外,您可以添加-std=c++11 以获得最需要的功能。 此外,我不必在 Xcode 获得更新后移动 bits/stdc++.h 文件。

我希望这会有所帮助!

【讨论】:

  • 任何建议编辑或明确使用 bits/stdc++.h 标头的答案,恕我直言,完全具有误导性。
  • 根据this的回答MacOSX没有uchar.h文件所以你不能#include &lt;cuchar&gt;。也许您可以下载它e.g. from here并将其粘贴到正确的目录中。我还没试过。
【解决方案4】:

我也遇到了这些错误,我通过注释掉 &lt;cstdalign&gt; 部分解决了这些错误。

注释掉这些行之后,它会再给出 2 个错误 - cuchar not found&lt;memory_resources&gt; not found,使用 "//" 注释它们。它不会伤害您的 stdc++.h 文件。而且肯定会奏效。

【讨论】:

    【解决方案5】:

    我正在分享使用以下命令执行数组旋转示例代码的步骤

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

    然后生成一个.out文件。

    ./a.out
    

    示例代码:

    #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;
    }
    

    现在,不会有任何错误,谢谢

    【讨论】:

      猜你喜欢
      • 2014-10-08
      • 1970-01-01
      • 2020-08-29
      • 2022-01-11
      相关资源
      最近更新 更多