Missing symbols when using Clang with libc++ on CentOS 7 (no C++ ABI lib?) - c++

The following packages were installed from repositories on CentOS 7.6:
clang-3.4.2-9.el7.x86_64
libcxx-3.8.0-3.el7.x86_64
libcxx-devel-3.8.0-3.el7.x86_64
Trying to build a simple C++ program:
clang++ -std=c++11 -stdlib=libc++ junk.cpp
This results in a bunch of linker errors, an example is:
/bin/../lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/libc++.so: undefined reference to `__cxa_throw'
AFAIK, this seems to be a problem of missing C++ ABI library, but I can't figure out how to install one that would provide the needed symbols. I could probably build one from source, but would like to stick with what's available from trustworthy repositories.
Any ideas on how to fix this?
I can build the program just fine if -stdlib=libc++ is omitted, in which case libstdc++ is used.

Try passing -lc++abi manually. This problem is fixed in modern installations, but the Linux packages aren't modern. Consider using apt.llvm.org

Related

Problem with using clang as compiler for code::blocks

I installed llvm for clang, because I wanted to use clang for code::blocks as compiler, since I need compiler that supports c++20, so I installed llvm, the bin was added in environmental variables, even the code::blocks detected llvm as compiler, however I get error when i want to compile my code:
-------------- Build file: "no target" in "no project" (compiler: unknown)---------------
clang++.exe -c C:\Users\Temirlan\labs\lab4\rpn.cpp -o C:\Users\Temirlan\labs\lab4\rpn.o
clang++.exe -o C:\Users\Temirlan\labs\lab4\rpn.exe C:\Users\Temirlan\labs\lab4\rpn.o
C:\Users\Temirlan\labs\lab4\rpn.cpp:180:10: fatal error: 'iostream' file not found
#include <iostream>
^~~~~~~~~~
1 error generated.
Do you know what is the problem or maybe the picture will help?
photo of compiler executables in code::blocks
I got error of "fatal error: 'iostream' file not found"
All modern compilers support C++20 (to slightly varying extent): both Clang, GCC, and MSVC. So this shouldn't affect your choice (but I do think that Clang is the best option).
Clang can be set up in different ways: (in order of personal preference)
With GCC's standard C++ library, libstdc++. Install MSYS2, then use it to install both Clang and GCC: pacman -S mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-clang. Then use C:/msys64/ucrt64/bin/clang++.exe as the compiler. (There's also MINGW64 variant instead of UCRT64, read about the difference here).
With its own standard C++ library, libc++. Install MSYS2, then use it to install libc++-flavored Clang: pacman -S mingw-w64-clang-x86_64-clang. Then use C:/msys64/clang64/bin/clang++.exe as the compiler.
With MSVC's standard C++ library, aka MSVC STL. Install the official Clang build, and install Visual Studio.
Note that the first two options don't involve downloading the official Clang build. The official build wants the MSVC STL by default, which you don't have, since you didn't install VS. (And if you do install it, you might as well use it instead of CodeBlocks.)
The official Clang build can be made to work with other standard libraries, but they need to be installed separately, and you need to persuade it with some compiler flags. It's easier to install the MSYS2's version, which already uses the correct flags by default.

On Linux, what's the use of installing clang++ if it uses g++ libs only?

For C++ development on Linux, if I install clang and use it; it actually uses libstdc++(the g++ lib). What's the use of installing the frontend compiler clang on linux then?
I should be good with gcc/g++ only on a linux machine as that's a complete toolchain!
Note: I'm not an expert in C++.
libstdc++ is a default runtime on your Linux. libc++ is not installed by default. If you link your app to libc++, you have to add it as a runtime dependency.
You are right, gcc/g++ is good for Linux, moreover its diagnostic messages are more clear, thus the compiler is better for beginners. By using clang++, you need to know the C++ standard deeper, otherwise it's difficult to get an error reason.

Runtime error [abi:cxx11] when compile with g++-4.9 on Ubuntu 15.10

I recently updated Ubuntu from 15.04 to 15.10. One of the major differences between these versions is the update of the default gcc version from gcc-4.9 -> gcc-5. The library I'm developing has been written and compiled for gcc-4.9, and relies on other libraries which only work in gcc-4.9.
I have installed gcc-4.9 onto my computer, and I can successfully compile both the library and my source file. However, when I tried to run the resultant program I get this error:
terminate called after throwing an instance of 'std::ios_base::failure[abi:cxx11]'
what(): basic_ios::clear: iostream error
Aborted (core dumped)`
The source code, and the file I'm trying to read here used to work before the upgrade. I have tried using the -D_GLIBCXX_USE_CXX11_ABI=0 flag, but I'm not sure this is the right thing to do, also it doesn't work.
This is an example of the flags I'm currently including in my makefile:
CPPFLAGS = -O0 -g3 -Wall -c -fpermissive -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++0x -fPIC -MMD -MP
Any ideas that might help me out?
You "simply" need to recompile everything your program needs that is C++.
See eg the Debian wiki on the transision which has (inter alia)
The good news is, that GCC 5 now provides a stable libcxx11 ABI, and stable support for C++11 (GCC version before 5 called this supported experimental). This required some changes in the libstdc++ ABI, and now libstdc++6 provides a dual ABI, the classic libcxx98 ABI, and the new libcxx11 (GCC 5 (<< 5.1.1-20) only provides the classic libcxx98 ABI). The bad news is that the (experimental) C++11 support in the classic libcxx98 ABI and the new stable libcxx11 ABIs are not compatible, and upstream doesn't provide an upgrade path except for rebuilding
There is no shortcut.

Clang, LLVM, and g++

I installed clang to compare its use with g++ (gcc) for some C++ programs; as in, I compared compilation time, error feedback, etc,. for the same programs. I did this mainly for the official LLVM tutorial (implementing a compiler using LLVM).
At one point, I needed to install the LLVM libraries (v. 2.9.), while before I had only downloaded clang. Subsequent to this, compilation with clang++ doesn't seem to work for the following tutorial code; while g++ appears to work with LLVM directives (e.g., 'llvm-config --libs`, etc.). Concurrent to installation of the libraries, I had to also go from the old tutorial I was reading (llvm 2.2., suggesting clang++ syntax), to one for 3.0. (suggesting g++ syntax), as the library include locations had changed.
My questions to explain the changes after downloading the llvm libs:
(1) When I now type 'g++', am I still actually working with gcc, or has llvm/clang set itself as a new default mimicking the gcc syntax? If yes, how can I make sure that I actually use gcc, not clang, when I want to?
(2) If g++ still is gcc, any opinions/guesses why the official clang/llvm tutorial switches from suggesting the use of its own competing compiler back to gcc? (c., eg, here)
My System:
Ubuntu 12.04.
llvm 2.9.
gcc 4.6.3.
clang 3.0.
In order for typing g++ to execute a clang compiler, you'd probably be looking at either a symbolic link called g++ to clang, earlier in your $PATH than the path to the real g++, or else an alias named g++, along the lines of alias g++='clang++'.
You can check for the alias (although I imagine it's unlikely) by typing alias g++, which will let you know if you have any alias set up (and if so, what to)
In order to change the $PATH issue, if it occurs, you'd have to either remove the symbolic link (seems sensible, given if you wanted clang, you could just type clang++ instead of g++) or change the position of the symbolic link in the PATH variable, but since they're likely to exist in /usr/local/bin or something similar, that would render a symbolic link unlikely too!
Given this, probably g++ still calls the gcc g++ compiler, and someone was just a little careless when typing the tutorial - I see only the one mention of g++ on the page you linked, near the bottom?

What is the sanctioned way to build libc++ for clang on Linux?

Edit/Update/Note: Just let clang use libstdc++. Has been working really well for me so far.
===============================
In the past I have been able to succeed by doing something with cmake, but just now I discovered a buildit script inside the lib directory of the http://llvm.org/svn/llvm-project/libcxx/trunk project tree.
This buildit script appears to not make use of libsupc++ which is what the cmake approach that I took earlier used. For instance, this guide shows one cmake incantation to produce a makefile for libc++, which will be able to take care of compiling and installation.
My question is what is the difference between these different ways to produce the LLVM-libc++ and which one should be used? Will they behave differently?
The buildit script does not appear to provide any help for installation. Are there directions anywhere for how to properly install the library? With my previous libc++ built with cmake, I had to always add -lc++ to the linker flags (and the path with -L), which is not necessary in my OS X makefiles.
The libc++ website has a nice overview of the possible ways to build libc++.
I suggest using CMake + libc++abi.
Also see the Arch Linux User Repository build script, which uses the buildit script. I installed libc++ from that and used it with the Arch Linux Clang package succesfully by using
clang++ -std=c++11 -stdlib=libc++ -lc++abi