How to compile with clang and mingw-w64? - c++

I'm trying to use clang10 with mingw-w64's libstdc++, since the MSVC headers don't support clang 10. I don't mind not having the new parts of the standard library, I just want to use the new language features.
I have mingw-w64 version 8.1.0 for x86_64 with POSIX threads and SEH exceptions installed and I run clang with a command:
clang++ -target x86_64-pc-windows-gnu -std=c++20 ...
Everything works OK. All the language features that should be implemented in clang 10 work, but when I throw any exception, this happens when I run the program (the program compiles OK):
Mingw-w64 runtime failure:
Unknown pseudo relocation protocol version 65536.
I tried installing mingw with SJLJ exceptions and use the -fsjlj-exceptions flag in clang, but the program doesn't even compile:
C:\Users\egst\AppData\Local\Temp\test-f4a4de.o:test.cpp:(.text+0x82): undefined reference to `__gxx_personality_sj0'
C:\Users\egst\AppData\Local\Temp\test-f4a4de.o:test.cpp:(.text+0xd9): undefined reference to `_Unwind_SjLj_Register'
C:\Users\egst\AppData\Local\Temp\test-f4a4de.o:test.cpp:(.text+0x177): undefined reference to `_Unwind_SjLj_Resume'
clang++ --version shows Target: x86_64-pc-windows-msvc. Maybe there's a version for x86_64-pc-windows-gnu that I should use instead? Is there any way to make this work at this moment, or should I wait for support from MSVC? Is there maybe any alternative besides MSVC and MinGW?

Based on #HolyBlackCat 's sugestion, I tried this with MSYS2 and it works perfectly. I simply installed MSYS2, then from the MSYS bash I installed this package. It installs everything in in ...\msys2\mingw64. You get clang 10 (+ gcc 9.3) and all the needed stl headers (still no c++20 headers like <concepts> though).

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.

MinGW g++ 4.8.1-4 doesn't recognize -std=c++14

I installed MinGW by following their home page to their sourceforge and using mingw-get-setup.exe. This installed g++ 4.8.1-4. GCC 4.8 is supposed to support C++14 with a command-line switch, but I just get an "unrecognized option" error.
Is this a bug with MinGW? With GCC? What can I do about it? Since I know someone will ask, I want C++14 for its for-each loops. I'm using iterators for now, but for-each would improve both readability and writability.
EDIT: Found out my g++ build supports c++11, so I can use for-each. But still no luck on c++14 support.
g++ 4.8 simply does not support C++14, also MinGW is quite outdated when there are more new versions of gcc.
Alternatives you can use
If you want really to use C++11 or C++14 on windows with gcc you should be using one of the following options:
https://msys2.github.io/ (Uses MinGW-w64 internally).
http://mingw-w64.org/doku.php (it supports 32-bits too).
http://tdm-gcc.tdragon.net/.

Chilitags compilation on Linux

anyone has any experience with Chilitags? I tried to implemented it on my Virtual Linux, I followed all the steps the instructions give, but when I tried to make build it gave this error.
cc1plus: error: unrecgonized command line options `-std=c++11`
Can somebody tell me what this error means. My cmake version is 2.8.7 and gcc version is 4.6.3.
Thanks
GCC version 4.6 is far from the latest version, it's up to 4.9 now.
The problem is that the flag -std=c++11 didn't get into GCC until version 4.7, before that it was -std=c++0x.
You might also want to know that the support for C++11 (or C++0x as it was still called then) was far from complete in GCC 4.6.

getting c++11 - compliant compiler

This all seems like a colossal mess.
All I want is a compiler that implements C++11, so I can use <chrono>. But I'm so confused from the very beginning.
Currently, I build programs by invoking G++, but when I check the version via $ g++ -v, I get:
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build
2336.11.00)
What's going on? Am I using G++? GCC? LLVM? I don't even know. Are they the same thing?
So now I'm trying to build and download GCC 4.7 via gnu.org, but I have no idea what any of the guides are talking about. I've never seen so many acronyms for things I dont know.
Why is this so complicated? What's with all those versions, with some of them only implementing some parts of C++11 and not others?
Here's the situation on OS X.
There are two C++ compilers installed by default.
[5:49pm][wlynch#watermelon ~] g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
[5:49pm][wlynch#watermelon ~] clang++ --version
Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
g++ is running llvm-gcc, which is the gcc frontend, and then the llvm backend.
clang++ is running clang, which is the clang frontend and then the llvm backend.
If you want a C++11 compiler on OS X without installing other packages, your only option is to use the clang compiler.
The flags necessary are:
clang++ -stdlib=libc++ -std=gnu++11
To describe the two flags I'm passing:
-stdlib=libc++ uses the libc++ standard library, instead of the gnu libstdc++. On OS X, the libc++ version has c++11 support. The gnu libstdc++ one does not.
-std=gnu++11 tells the compiler to support c++11 code features, like lambdas and enum class. You can also pass -std=c++11, which is similar, but does not enable some commonly expected gnu extensions.
Update for OS X 10.9: As of OS X Mavericks, both g++ and clang++ are actually using clang. The only difference, is that g++ will imply -stdlib=libstdc++ and clang++ will imply -stdlib=libc++. So, on Mavericks, if you'd like to use C++11, you can follow the above advice, or just do:
clang++ -std=gnu++11
Update for OS X 10.10: As of OS X Yosemite, g++ is still clang in disguise. However, neither uses libstdc++ by default anymore. Both are now on libc++.
It sounds like you have Xcode 4.6 and the latest command line tools. This is from the release notes:
Important: The LLVM GCC compiler does not include the latest Objective-C and
C++11 features. Xcode 4.6 is the last major Xcode release which includes the
LLVM GCC compiler and the GDB debugger. Please migrate your projects to use the
LLVM compiler and LLDB debugger…
I think you want to use c++ instead:
$ c++ -v
Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix
#sharth: The situation changed on the Mac quite significantly since the release of XCode 5.0. clang/clang++ are the default C/C++ compilers. They correspond to the LLVM version 3.3 I believe, and this version of clang++ is fully C++11-compliant. Note that clang++ --version will return a version number like "5.0.x" on the Mac but that refers to the XCode version.
I have been using the Apple clang++ in a C++11 project for months now and so far I have not seen any problems. There is absolutely no reason to use any other C++ compiler on the Mac just now :-)
The situation with GCC/G++ is not so rosy. The latest version of G++ (4.8.2) does implement most of the C++11 standard, however the standard library is not compliant! For instance, std::regex is not implemented in libstdc++, but you find this out only when you run your code and the regex constructors throw std::regex_error -s. (I found this out the hard way when trying to port the aforementioned little project to Linux.) The community believes full compliance will be achieved with the 4.9 release of G++. Until then you should use the Clang compilers on Linux as well.
I have no access to the latest Intel C++ compiler suite so have no idea how compliant icpc is.

How to use std::thread of C++ 11 under Cygwin GCC 4.7.2

I've been trying to compile a multithread hello-world program under Cygwin using the newly introduced C++ 11 std::thread feature without success. I compiled and installed GCC 4.7.2 by myself, and the same code works without any problems under Linux with the same version of GCC. The first error I got was that the compiler did not recognize the -pthread flag. After researching on it for a while I noticed someone said on Cygwin this flag should be -lthread. I made the change and that error was gone, but another series of errors occur telling me thread is not member of std. I wonder if it's caused by the wrong configuration of the compiler during installation, or std::thread is simply not supported under Cygwin?
This looks like you did not compile the program with the appropriate standard library flag. If you want to compile for C++11 you should use:
g++ --std=c++0x -o ...
The --std flag sets the appropriate language compatibility level. If this does not help, please post the error messages you got as a source listing.