0

I installed clang in my conda environment along with gcc. Their versions are

gcc     7.2.0
clang   7.0.0
libcxx  7.0.0

I then created an hello world src file a.cpp

  1. If I compile the file using clang++ a.cpp. The error reads

    a.cpp:1:10: fatal error: 'iostream' file not found
    #include <iostream>
             ^~~~~~~~~~
    1 error generated.
    
  2. Using clang++ a.cpp --stdlib=libstdc++, the error is the same

  3. Using clang++ a.cpp --stdlib=libc++, the error becomes

    ~/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. Using 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.
    

I use a shared computer so I cannot install system wide compilers and header files.

Questions:

  1. What should I do to have it work?
  2. If clang does not ship with its own header files and I need to use what are provided by gcc, should I consider the compatibility of clang version and the gcc version?
  3. Do I need to install libc++ in the same conda environment in order to use clang++?

After some test, I found the way to do it in conda, which is posted as the an answer. However, I still don't understand how clang works, especially its relation with gcc. I would appreciate it very much if any one could answer (and I will accept that as the answer to this post):

  1. Does clang forward all the jobs to gcc so we always need the gcc tool chain to be installed in order to use clang?
  2. I found an include folder for clang, which is $HOME/conda/envs/test/include/c++/v1 alongside with $HOME/conda/envs/test/include/c++/7.2.0 which is from gcc. But if the --gcc-toolchain has been specified, the v1 folder is not searched for headers, (which can be seen from the output by adding -v to the compiler. Then what is the usage of the v1 include files?
doraemon
  • 2,296
  • 1
  • 17
  • 36
  • Did you also install libc++? If you tell clang to use it, then yes, you need to install it! – Matthieu Brucher Dec 17 '18 at 09:39
  • 3
    `bits/c++config.h` is an implementation detail of libstdc++, don't expect to have it in libc++! – Matthieu Brucher Dec 17 '18 at 09:40
  • Using conda I didn't find libc++. But I tried `libcxx` which does not change the outcome – doraemon Dec 17 '18 at 09:40
  • Same. libc++ and libcxx are the same backend that clang can use. If conda doesn't provide it, then stick with libstdc++. – Matthieu Brucher Dec 17 '18 at 09:41
  • Then I installed it and I got the same error. How to "stick with libstdc++"? – doraemon Dec 17 '18 at 09:43
  • If you are on Linux, you can build clang or get it installed through packaging? – Matthieu Brucher Dec 17 '18 at 09:49
  • Your standard library and associated headers are not properly installed. You need to install them separately. – Peter Dec 17 '18 at 09:49
  • @Peter Are these "standard library and associated headers" shipped with clang or it is shared with other compilers as gcc. How to install them if I don't have root privilege? Originally, I just did `conda install gcc clang libcxx`. – doraemon Dec 17 '18 at 09:53
  • The compiler and libraries are often associated. So standard library for clang is not (necessarily) the one that comes with gcc. As for installing clang without root privileges, look at https://stackoverflow.com/questions/8681385/install-clang-as-user-no-root-privileges – Peter Dec 17 '18 at 10:20

1 Answers1

3

Finally I found the way, which is to do

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

This is not obvious at all.

doraemon
  • 2,296
  • 1
  • 17
  • 36