doraemon
doraemon

Reputation: 2502

clang++ fails to compile hello world

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?

Upvotes: 0

Views: 1728

Answers (1)

doraemon
doraemon

Reputation: 2502

Finally I found the way, which is to do

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

This is not obvious at all.

Upvotes: 3

Related Questions