Problem with using clang as compiler for code::blocks - c++

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.

Related

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

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

OpenMP on Mac not working anymore

I had OpenMP compiling and executing in C/C++ on my Mac but then I formatted and reinstalled a fresh copy of OS X. I believe the only difference in the setup was that I had Xcode 5 before the format, and now I have Xcode 6.
Now I get fatal error: 'omp.h' file not found
I read this has something to do with clang and gcc, but, my confusion lies with why did it work before and now its not working?
After some research I seem to have found an answer.
GCC included in the latest version of Xcode (Xcode 6) is only a symbolic link to clang. Since clang does not support OpenMP at this time, you need to install a different version of GCC. The easiest way to do this would be to use Homebrew or MacPorts.
Keep in mind that even when you do this you will probably need to alter your $PATH to have /usr/local come before /usr/bin. This is because Homebrew will have placed your newly installed GCC in /usr/local. Also, some implementations may name the command gcc-49 instead of plain gcc.
Mac OSX uses clang.The gcc compiler in OS X does not support OpenMP. To use this feature a new gcc compiler needs to be installed.
Go to Terminal, if you have not installed Hombrew, install it:
/usr/bin/ruby -e "$(curl -fsSL https://`enter code here`raw.githubusercontent.com/Homebrew/install/master/install)"
then install new version of gcc
brew reinstall gcc --without-multilib
This will not make changes to the existing gcc compiler installed by Xcode as we are reinstalling it.
After running the command given below compile the files using the new version of gcc using the syntax : gcc-version -fopenmp filename.c
To find the version type gcc and then press tab. This will list out all the possible variants of gcc. The version number can be found out from this. For example : gcc-6, gcc-4.9,etc

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?

Easiest method to configure clang++ to use g++/gcc STL headers in Cygwin?

I'm running the most up to date versions of clang++ (v3.1) and g++/gcc (v4.7.3) in Cygwin (32-bit). Everything is using the installed default configuration. This is a fresh install of Cygwin in Windows 8.
My issue is that clang++ cannot find the installed g++ STL headers to compile my project.
#include <stdlib.h>
#include <mutex>
#include <thread>
int main() {
std::mutex myMutext;
return 0;
}
This sample code results in this error when compiled. Notice the libc stdlib.h header compiles without error. It's that isn't found. I've tried other STL headers as a test, same error.
clang++ -c -o test.o test.cpp
test.cpp:2:10: fatal error: 'mutex' file not found
#include <mutex>
^
1 error generated.
After some searching it seems the suggested options are recompiling the entire clang project and adding the header paths to its source or manually adding all the g++ STL header paths to my makefile both of which seem kind of hacky.
There has to be an easier option, right?
I don't know if you have solve it, but I have a simpler solution: just make s symlink of 4.7.3 to 4.5.3 and compile your code with -lstdc++ option, all will done.
clang++ will be build (compile-time) against specific versions of libstdc++ by default (IIRC). It will not check the system for newer versions at runtime.
The libstdc++ the clang-3.1 package of cygwin seems to use is gcc-4.5.3, as indicated by the output of clang++ -v test.cc. gcc-4.5.3 is not installed in your enviroment, however.
Your options, none of which are great:
Downgrade gcc to 4.5.3 via the setup. However your C++11 code will still not compile, due to lack of support in the libstc++ that comes with gcc-4.5.3.
Build clang yourself, against the newer gcc.
Build clang yourself, with libc++. However, it is highly unlikely Windows is (fully) supported by it.
Use gcc-4.7 instead. The Windows support is far more mature in gcc right now anyway (and don't get me started about cross-compilers).
(Aside: you'll need -std=c++11 to compile your code)

Using homebrew, gcc and llvm with C++ 11

Here's my problem: I want to use C++11 features provided by either gcc or clang. However, I have these requirements:
I'm using a mac
I'm dependent on a bunch of libraries provided by homebrew (and really don't want to compile them myself). Specifically OSG, which itself is dependent on a ton of other libraries. And boost, though I can always compile that myself.
Homebrew seems to only want to use gcc (please correct me if I'm wrong). I can't find any options to switch to LLVM instead. While I understand that this might be due to the fact that not all libraries are compatible with LLVM yet, this would still be a nice feature for those that are.
The version of gcc that comes pre-installed on a mac of gcc is 4.2. gcc 4.2 doesn't have the c++11 features required. I've installed 4.7 via homebrew, but searches for how to set homebrew to use it all say don't do it (gcc 4.2 on the mac is not the vanilla version, so the 4.7 version I got won't be able to compile some things).
My questions are: Does anyone have any suggestions or fixes they have implemented to get around this problem? Should I give up on Homebrew? Does anyone know if Homebrew has a plan to switch to LLVM in the future? Does anyone have any upgrade-plan for how to deal with these incompatibilities?
I don't see how homebrew can continue to depend on gcc 4.2 in the long run, but haven't found any real discussion on this matter.
The default GCC on Mac is not real GCC of GNU. It's LLVM-GCC in fact, which is a branch of GCC. Several years ago, LLVM-GCC was terminated, and replaced with DragonEgg, which is a GCC plugin to use LLVM as a GCC backend.
LLVM-GCC is just a compiler frontend, whose role is using GCC frontend to translate the source code into LLVM IR[Intro to LLVM 11.3]. Once IR generated, LLVM backend will use it to generate binary code. This step has nothing to do with GCC.
The above goal was fully achieved from 10.7, whose components were all compiled by clang, a frontend provided by LLVM.
But Apple still kept LLVM-GCC and GCC runtime libraries. I guess its purpose might be providing a opportunity to compile some code GCC ONLY.
Now let's answer your questions:
If you want to use C++11 features, use clang++ -stc=c++11 -stdlib=libc++ instead. And clang might have already supported all c++11 features.
If you want homebrew supporting LLVM, it has already supported, at least on backend.
If you want homebrew using clang as a compiler frontend, it depends on homebrew community schedule. For example, you can append --with-c++11 argument to use clang to compile boost.But you cannot use this argument when brew install autoconf. In fact, some components might not be compiled correctly by clang.
If you know it can be compiled by clang but homebrew hasn't supported yet, you have to hack the corresponding ruby script at $HOMEBREW_ROOT/Library/Formula directory. Fortunately, in most of cases, replacing ./configure blablabla with ./configure blablabla CXX=clang++ -stc=c++11 -stdlib=libc++ works well. And by the way, if your hack is successful, please make a pull request to homebrew.
So, try it and have a fun.
I have an OS X Mountain Lion environment and use C++11. In this answer I'll break your requirement for not compiling your own stuff.
I use Homebrew and, I must say, I advise you to give up on depending on it to provide you clang and libc++ and all its formulas built with them.
What I've done, and I like, is
clone llvm, clang and libc++ from repositories.
install to /opt/local and put /opt/local/bin at top on /etc/paths.
build my development stuff with my new clang.
let Homebrew for installing tools like git and things I'll not develop for, just use.
I've followed clang build instructions for installing it to /opt/local.
For libc++, one detail: after running the buildit script, I've symlinked the include directory to /opt/local/lib/c++/v1 (clang on /opt/local looks for this as default directory), and also symlinked the libs to /opt/local/lib/ (but look that binaries will not automatically link to libc++ on /opt/local/lib. You must use install_name_tool for that).
use
clang++ -std=c++11 -stdlib=libc++
you can also install latest gcc from homebrew-dups
brew install [flags] https://raw.github.com/Homebrew/homebrew-dupes/master/gcc.rb
For LLVM, brew install --HEAD llvm. To use clang or a brew-installed gcc, add --with-clang or --with-gcc=gcc-x.x where x.x is the version you want.