clang++ fails to compile hello world - c++

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
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.
Using clang++ a.cpp --stdlib=libstdc++, the error is the same
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)
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:
What should I do to have it work?
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?
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):
Does clang forward all the jobs to gcc so we always need the gcc tool chain to be installed in order to use clang?
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?

Finally I found the way, which is to do
clang++ --gcc-toolchain=$HOME/conda/envs/test a.cpp
This is not obvious at all.

Related

Clang MacOs over command line does not work

I am fairly new to both MacOs and C++ and have a problem which is similar to the one described here but also no solution I find in the Internet works.
'fatal error: 'wchar.h' file not found' error with the new macos 11.3 update
If I try to compile the most simple c++ program on my machine via command line it does not work.
#include <iostream>
int main()
{
std::cout << "Hello, World!" << std::endl;
return 0;
}
Since it used to work I probably broke something but don't know what
The simple test command I use is the following
clang -v --target=arm64 helloworld.cpp
Which results in a iostream not found error
if I now include the xcode include directory via
clang -v --target=arm64 -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/ helloworld.cpp
The iostream error disappears and I get a wchar.h not found error
I removed xcode completely and reinstalled it but this seems to not help also does it not make a difference if I use clang or clang++.
With Clion and cmake it works but I do not know why
xcode-select version 2392
/usr/bin/clang
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin21.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
locate iostream.h
`/Library/Frameworks/Mono.framework/Versions/6.12.0/include/glib-2.0/gio/gfileiostream.h
/Library/Frameworks/Mono.framework/Versions/6.12.0/include/glib-2.0/gio/giostream.h
/opt/homebrew/Cellar/boost/1.76.0/include/boost/asio/basic_socket_iostream.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/iostreams/detail/iostream.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/math/cstdfloat/cstdfloat_iostream.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/nowide/iostream.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/typeof/std/iostream.hpp
/opt/homebrew/Cellar/glib/2.70.2/include/glib-2.0/gio/gfileiostream.h
/opt/homebrew/Cellar/glib/2.70.2/include/glib-2.0/gio/giostream.h
/opt/homebrew/Cellar/glib/2.70.2/include/glib-2.0/gio/gsimpleiostream.h
I had similar troubles building clang + llvm, then trying to use the newly built clang via
./build/bin/clang -std=c++20 <my_file>
gave me a
fatal error: 'iostream' file not found
What fixed it was if I compiled with the following instead:
./build/bin/clang -std=c++20 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk <my_file>
The fix largely comes from https://github.com/MaskRay/ccls/issues/191#issuecomment-556983460 and the discussion there

can not build cpp using clang pre-built binary: file wchar.h not found

I am using MacOS 10.15. Since the clang shipped with MacOS does not include clang-format. I installed another pre-built clang binary from here. I have added the binary file path to my PATH variable.
export PATH="$HOME/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin:$PATH"
I tried to compile a simple program:
#include <iostream>
int main(int argc, char *argv[]) {
std::cout << "Hello world!\n";
return 0;
}
using the following command:
clang++ hello.cpp -o hello
I got the following error:
In file included from hello.cpp:1:
In file included from /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/iostream:37:
In file included from /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/ios:214:
In file included from /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/iosfwd:95:
/Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/wchar.h:118:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>
^~~~~~~~~
1 error generated.
I found that wchar.h bundled with this pre-built package is in the following directory:
/Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/include/c++/v1/
So I added the -I flag:
clang++ -I /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/include/c++/v1 hello.cpp -o hello
The error still persists.
If I use clang++ shipped with MacOS, I have no problem compiling the source code:
# the following works without any error
/usr/bin/clang++ hello.cpp -o hello
I have seen post here, here, and here, but the solutions do not apply.
You got clang-format improperly. Reset the system to the state before you installed another pre-built clang binary. Then use Homebrew to install clang-format
brew install clang-format
clang+llvm-10.0.0-x86_64-apple-darwin is not suitable to your Mac. It depends on system frameworks that are not available, so you get the error finding wchar.h in a system framework. When you install clang+llvm-10.0.0-x86_64-apple-darwin you ignore framework dependencies. Homebrew will care about dependencies.

Compiling with g++, using vector and array libraries

When compiling my cpp file on linux with the following line:
$ g++ -o blabla blabla.cpp
I get the following message on stdout:
In file included from
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/array:35,
from blabla.cpp:5: /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../
include/c++/4.4.7/c++0x_warning.h:31:2:
error: #error This file requires compiler and library support for the
upcoming ISO C++ standard, C++0x. This support is currently
experimental, and must be enabled with the -std=c++0x or -std=gnu++0x
compiler options.
The script does #includes the <vector> and <array> libraries, so I don't know why it fails.
What causes this error?
Above error are coming because you are using the latest feature of C++, and you default version is older than required.
Flags (or compiler options) are nothing but ordinary command line arguments passed to the compiler executable.
g++ -std=c++0x -o blabla blabla.cpp

installing gfortran in cygwin: gfortran: cyglto_plugin.dll not found

I'm trying to get the gfortran compiler with cygwin. When attempting to compile a hello world program, I get the following error:
gfortran: fatal error: -fuse-linker-plugin, but cyglto_plugin.dll not found
compilation terminated.
To install the compiler, I used the cygwin setup and selected
gcc-fortran: GNU compiler collection
libgfortran3: GCC fortran runtime library
I also mistakenly selected the toolchains for mingw, even though I don't have the mingw compiler (as far as I'm aware). g++, which I installed awhile ago, works fine.
What do I need to do to compile/find the library?
It is because of the gcc and gfortran versions are not same, try checking that. If so, do the following
$ apt-cyg remove gcc-fortran
$ apt-cyg install gcc-fortran

why self built g++ compiler fails to compile my code

I wanted to use latest g++ compiler(4.9.1) on suse linux, but suse only supports an older g++ version. So, I took a latest source code from one of the gnu mirror sites and compiled it myself. Everything went fine. But when I tried to compile my test code using the built g++, the compilation fails with error,
"/root/home/include/c++/4.9.1/x86_64-unknown-linux-gnu/bits/os_defines.h:39:22: fatal error: features.h: No such file or directory".
I can find a "features.h" in "/root/home/include/c++/4.9.1/parallel", but I feel that it should be there in "/root/home/include/c++/4.9.1/" itself.
I copied "/root/home/include/c++/4.9.1/parallel/features.h" to "/root/home/include/c++/4.9.1/" just to see what happens. Now it complains with error "whcar.h" not found.
Have I missed something.
Here are the steps I followed to build g++.
1. /root/home/gcc_build/objdir# ../gcc-4.9.1/configure --prefix=/root/home/ --disable-multilib
2. /root/home/gcc_build/objdir# make -j16
3. /root/home/gcc_build/objdir# make install
4. /root/home/gcc_build/test# /root/home/bin/g++ --sysroot /root/home -m64 test.cpp
I resolved the issue by removing sysroot option and pointing c++ include and library path to my home directory. One thing I noticed was that the g++ source does not come with libc and c header files, and libc has to be installed seperately. But with sysroot option, g++ was trying to look all the header files in my home directory.
Below is the command I used to successfully compile the code.
/root/home/bin/g++ -I /root/home/include/c++/4.9.1 -L /root/home/lib64 -Wl,--rpath=/root/home/lib64 --std=c++0x -m64 test.cpp
Take a look at the GCC Directory Options. It is important to use the correct "specifier" as well (-isystem, -L, -B, -I etc)