Clang看不到基本标题

swe*_*gar 46 c++ clang llvm-clang c++11 clang++

我试图用Clang在Fedora 20上编译简单的hello world,我得到以下输出:

d.cpp:1:10:致命错误:找不到'iostream'文件

#include <iostream>

我不知道如何解决它.

小智 54

原因之一可能是:clang 没有自己的 c++ 头库,因此它指向 gcc 的库文件夹来访问头文件。您可以使用命令查看并确认

clang -v
Run Code Online (Sandbox Code Playgroud)

这将打印其版本和所选的 gcc 安装,这将帮助您了解它正在使用哪个版本的 gcc。对我来说,

Ubuntu clang version 14.0.0-1ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64
Run Code Online (Sandbox Code Playgroud)

现在可以看到它选择了我没有安装的 gcc 第 12 版本,因此出现错误,因为它找不到它(不知道它是如何选择该版本的,即使我还没有安装它) 。但解决办法是安装g++ nth版本。您可以通过以下命令来完成此操作,其中 n 是 gcc 选择的版本号,您就可以开始了。

sudo apt install g++-n
Run Code Online (Sandbox Code Playgroud)

经过大量挖掘后我得到了这个答案,并发现了这个博客clangmissingheaders

  • 完美,在 Ubuntu22.04 LTS 上为我解决了这个问题 (3认同)
  • 我为此挖了一天!就我而言,问题是在一周前没有问题的机器上编译 godot 时“找不到‘cstdint’文件”。非常感谢! (2认同)
  • 并非所有英雄都穿着斗篷,这件事已经困扰我好几天了,即使我从 `27.1` 升级到 `emacs-29.1`,它仍然没有为我做。已投赞成票 (2认同)
  • 谢谢,我的问题是没有为 gcc 版本 12 安装 libsdc++ 开发包... (2认同)

Aru*_*asR 33

这是因为没有安装g ++,所以libstdc ++不存在.

您可以安装g ++,或者如果首选LLVM,请安装LLVM libc ++并指定您要使用它,如下所示:

sudo apt-get install libc++-dev
clang++ -stdlib=libc++ <rest of arguments>
Run Code Online (Sandbox Code Playgroud)

您可能希望将/ usr/bin/c ++链接到默认编译器:

ln -s /usr/bin/c++ /usr/bin/clang++-libc++
Run Code Online (Sandbox Code Playgroud)

然后简单地使用编译

$ c++ <args_as_usual>
Run Code Online (Sandbox Code Playgroud)

  • @sweet_sugar对不起,我使用Mint,不知道Fedora是如何打包的.在Debian上,LLVM头文件位于**libc ++ - dev**中,它与**libc ++**(二进制运行时库)分开. (4认同)
  • 对于Fedora,软件包是**libcxx**和**libcxx-devel** (4认同)

小智 15

第3点为我解决了这个问题.

1.有同样的问题,fedora 21 :: clang 3.5.0:

clang++ -std=c++14 -pedantic -Wall test_01.cpp -o test_01 -v
Run Code Online (Sandbox Code Playgroud)

2.

ignoring nonexistent directory "/usr/lib/gcc/i686-redhat-linux/4.9.2/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/bin/../lib/clang/3.5.0/include
 /usr/include
End of search list.
test_01.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
Run Code Online (Sandbox Code Playgroud)

3.

sudo yum install gcc-c++
Run Code Online (Sandbox Code Playgroud)

4.

#include "..." search starts here:
#include <...> search starts here:
 /bin/../lib/gcc/i686-redhat-linux/4.9.2/../../../../include/c++/4.9.2
 /bin/../lib/gcc/i686-redhat-linux/4.9.2/../../../../include/c++/4.9.2/i686-redhat-linux
 /bin/../lib/gcc/i686-redhat-linux/4.9.2/../../../../include/c++/4.9.2/backward
 /usr/local/include
 /usr/bin/../lib/clang/3.5.0/include
 /usr/include
 /usr/lib/gcc/i686-redhat-linux/4.9.2/include
End of search list.
Run Code Online (Sandbox Code Playgroud)


Bog*_*ogi 15

就我而言,clang 使用的是 GCC 安装版本 12,但我缺少libstdc++该版本的:

$ clang -v
Ubuntu clang version 14.0.0-1ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Run Code Online (Sandbox Code Playgroud)

像这样修复它:

sudo apt install libstdc++-12-dev
Run Code Online (Sandbox Code Playgroud)


Ste*_*kiy 6

看起来您应该为您的 clang 构建提供-stdlib选项。其中之一-stdlib=libc++-stdlib=libstdc++可能会起作用。

有关您的主题的更多详细信息:

什么时候需要使用标志 -stdlib=libstdc++?