clang++ 无法编译 hello world

clang++ fails to compile hello world

我在 conda 环境中安装了 clang 和 gcc。他们的版本是

gcc     7.2.0
clang   7.0.0
libcxx  7.0.0

然后我创建了一个 hello world src 文件 a.cpp

  1. 如果我使用clang++ a.cpp编译文件。错误显示

    a.cpp:1:10: fatal error: 'iostream' file not found
    #include <iostream>
             ^~~~~~~~~~
    1 error generated.
    
  2. clang++ a.cpp --stdlib=libstdc++,错误一样

  3. 使用clang++ a.cpp --stdlib=libc++,错误变为

    ~/conda/envs/test/bin/ld: cannot find crtbegin.o: No such file or directory
    ~/conda/envs/test/bin/ld: cannot find -lgcc
    clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
    
  4. 使用clang++ a.cpp -I$HOME/conda/envs/test/include/c++/7.2.0

    In file included from a.cpp:1:
    /site/home/shliu/conda/envs/test/include/c++/7.2.0/iostream:38:10: fatal error: 'bits/c++config.h' file not found
    #include <bits/c++config.h>
             ^~~~~~~~~~~~~~~~~~
    1 error generated.
    

我使用共享计算机,因此无法安装系统范围的编译器和头文件。

问题:

  1. 我应该怎么做才能让它发挥作用?
  2. 如果 clang 没有附带自己的头文件,我需要使用 gcc 提供的头文件,我是否应该考虑 clang version 和 [=22] 的兼容性=]?
  3. 是否需要在相同的 conda 环境中安装 libc++ 才能使用 clang++

经过一些测试,我找到了在 conda 中执行此操作的方法,posted 作为答案。但是,我还是不明白clang是如何工作的,尤其是它与gcc的关系。如果有人能回答我将不胜感激(我会接受它作为这个 post 的答案):

  1. 是否 clang 将所有作业转发给 gcc,所以我们总是需要安装 gcc 工具链才能使用 clang
  2. 我找到了 clang 的包含文件夹,它是 $HOME/conda/envs/test/include/c++/v1 和来自 gcc$HOME/conda/envs/test/include/c++/7.2.0。但是如果指定了--gcc-toolchain,则不会在v1文件夹中搜索headers,(在编译器中加上-v从输出中可以看出。那有什么用v1 包含文件?

终于找到方法了,就是做

clang++ --gcc-toolchain=$HOME/conda/envs/test a.cpp

这一点都不明显。