gpt4 book ai didi

c++ - 在 Clang++ : fatal error: 'iostream' file not found 中定位 iostream

转载 作者:太空狗 更新时间:2023-10-29 11:48:36 36 4
gpt4 key购买 nike

我编写了以下简单的 C++ 程序:

#include <iostream>

using namespace std;

int main() {
cout << "Hello, World" << endl;
return 0;
}

当我用 g++ 编译它时,它工作得很好。当我尝试使用 Clang++ 进行编译时,出现以下错误:

main.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^~~~~~~~~~
1 error generated.

使用 -v 参数运行,我看到以下内容:

ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/backward"
ignoring nonexistent directory "/include"
ignoring duplicate directory "/usr/include/clang/6.0.0/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++
/usr/include/clang/6.0.0/include
/usr/local/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.

分别查看这些文件夹,我发现在 /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++ (或者,更简洁地说,在 /usr/include/c++ 中)我有以下目录:

drwxr-xr-x   5 root root 4.0K Feb  4 09:38 .
drwxr-xr-x 101 root root 20K Feb 4 12:22 ..
drwxr-xr-x 12 root root 12K May 24 2018 5
drwxr-xr-x 12 root root 12K Oct 9 14:53 7
drwxr-xr-x 5 root root 4.0K Feb 4 09:38 v1
lrwxrwxrwx 1 root root 1 Apr 11 2018 5.5.0 -> 5
lrwxrwxrwx 1 root root 1 Apr 15 2018 7.3.0 -> 7

57v1 目录中,每个目录中都有一个名为 iostream 的文件>/p>

同样在 /usr/include/x86_64-linux-gnu 中有一个 c++ 目录,它看起来和这个完全一样(575.5.07.3.0 目录)。

同样在/usr/include 中有一个c++ 目录,它看起来和上面两个一模一样

我不确定我的开发环境是如何变得如此困惑的,但此时我只想知道如何修复它,以便 Clang++ 能够成功找到这 9 个 iostream 实例之一> 而不是抛出它不存在的错误。我是否需要添加一个环境变量来告诉 Clang 去哪里找?我是否需要传递一个命令行参数来告诉 Clang 进行递归搜索?

更新(一)

当我尝试使用 libc++ 构建时,出现以下错误:

$> clang++ -stdlib=libc++ main.cpp
/usr/bin/ld: cannot find -lc++abi
clang: error: linker command failed with exit code 1 (use -v to see invocation)

当我尝试使用手动覆盖的包含路径构建时,出现以下错误:

$> clang++ -isystem /usr/include/c++/7 -isystem /usr/include/x86_64-linux-gnu/c++/7 main.cpp
/usr/bin/ld: cannot find -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)

当我同时尝试这两种方法时,出现以下(非常大的)错误:

$> clang++ -isystem /usr/include/c++/7 -isystem /usr/include/x86_64-linux-gnu/c++/7 -stdlib=libc++ main.cpp
In file included from main.cpp:1:
In file included from /usr/include/c++/7/iostream:39:
In file included from /usr/include/c++/7/ostream:38:
In file included from /usr/include/c++/7/ios:42:
In file included from /usr/include/c++/7/bits/ios_base.h:41:
In file included from /usr/include/c++/7/bits/locale_classes.h:40:
In file included from /usr/include/c++/7/string:52:
In file included from /usr/include/c++/7/bits/basic_string.h:6352:
In file included from /usr/include/c++/7/ext/string_conversions.h:41:
In file included from /usr/include/c++/7/cstdlib:77:
/usr/include/c++/7/bits/std_abs.h:56:3: error: declaration conflicts with target of using declaration already in scope
abs(long __i) { return __builtin_labs(__i); }
^
/usr/include/c++/v1/stdlib.h:111:44: note: target of using declaration
inline _LIBCPP_INLINE_VISIBILITY long abs( long __x) _NOEXCEPT {return labs(__x);}
^
/usr/include/c++/7/bits/std_abs.h:52:11: note: using declaration
using ::abs;
^
/usr/include/c++/7/bits/std_abs.h:61:3: error: declaration conflicts with target of using declaration already in scope
abs(long long __x) { return __builtin_llabs (__x); }
^
/usr/include/c++/v1/stdlib.h:113:44: note: target of using declaration
inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT {return llabs(__x);}
^
/usr/include/c++/7/bits/std_abs.h:52:11: note: using declaration
using ::abs;
^
In file included from main.cpp:1:
In file included from /usr/include/c++/7/iostream:39:
In file included from /usr/include/c++/7/ostream:38:
In file included from /usr/include/c++/7/ios:42:
In file included from /usr/include/c++/7/bits/ios_base.h:41:
In file included from /usr/include/c++/7/bits/locale_classes.h:40:
In file included from /usr/include/c++/7/string:52:
In file included from /usr/include/c++/7/bits/basic_string.h:6352:
In file included from /usr/include/c++/7/ext/string_conversions.h:41:
/usr/include/c++/7/cstdlib:177:3: error: declaration conflicts with target of using declaration already in scope
div(long __i, long __j) { return ldiv(__i, __j); }
^
/usr/include/c++/v1/stdlib.h:116:42: note: target of using declaration
inline _LIBCPP_INLINE_VISIBILITY ldiv_t div( long __x, long __y) _NOEXCEPT {return ldiv(__x, __y);}
^
/usr/include/c++/7/cstdlib:145:11: note: using declaration
using ::div;
^

提醒一下,我实际上只是在尝试编译 Hello, World

我还尝试使用以下命令卸载并重新安装 Clang:

$> sudo apt-get purge --auto-remove clang
$> sudo apt-get update
$> sudo apt-get install clang

这没有效果。我正在运行 Ubuntu 18.04,我不知道出了什么问题或从哪里开始修复它。我的构建环境一团糟。

如果可能的话,我想让 Clang 工作而不是退回到使用 G++,因为我的 IDE 似乎会自动检测 Clang 并将其用于语法检查。这意味着我编写的每个 C++ 程序在第一行都有一个 fatal error (“未找到 iostream”)并且文件的其余部分未经检查,因为第一个是 fatal error 。

更新(二)

我已经尝试从 Ubuntu apt 存储库安装更多的软件包,但没有成功:

$> sudo apt-get install libc++1 libc++1-9 libc++abi1 libc++abi1-9 llvm-9 llvm-9-dev
$> clang++ -isystem /usr/include/c++/7 -isystem /usr/include/x86_64-linux-gnu/c++/7 -stdlib=libc++ main.cpp
/usr/bin/ld: cannot find -lc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我也试过 sudo apt-get install lc++1 却发现这是一个完全不相关的包。

更新(三)

我又花了几个小时试图解决这个问题,从 apt 和源代码安装多个包,尝试各种工具的不同版本,从其他源手动复制库,甚至跳到 Clang IRC 和几个非常知识渊博的开发人员直接。

没有人能够弄清楚我的笔记本电脑出了什么问题,而且我所做的一切都无法让它正常工作。

不幸的是,再过两周我就没有这台笔记本电脑了,所以我可能需要以“无法重现”为由关闭这个问题——因为一旦笔记本电脑不见了,我将无法复制损坏的开发环境.

最佳答案

我也被这个问题困扰了很久,你应该试试删除文件夹(cd/usr/lib/gcc/aarch64-linux-gnu/8)。 clang++ 不能工作的原因是这个文件夹不包含 libstdc++.a。

clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/5
Found candidate GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/5.5.0
Found candidate GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/6
Found candidate GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/6.5.0
Found candidate GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/7
Found candidate GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/7.3.0
Found candidate GCC installation: /usr/lib/gcc/aarch64-linux-gnu/5
Found candidate GCC installation: /usr/lib/gcc/aarch64-linux-gnu/5.5.0
Found candidate GCC installation: /usr/lib/gcc/aarch64-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/aarch64-linux-gnu/6.5.0
Found candidate GCC installation: /usr/lib/gcc/aarch64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/aarch64-linux-gnu/7.3.0
Selected GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/7.3.0

检查/usr/lib/gcc/aarch64-linux-gnu/中的所有文件夹clang++会选择最后一个,确保最后一个有libstdc++.a

关于c++ - 在 Clang++ : fatal error: 'iostream' file not found 中定位 iostream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54521402/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com