How can I make use of llvm as clang backend to compile C++ files without using gcc as clang's backend?
I am pretty sure clang is using gcc because
$ clang++ --version
clang version 6.0.1 (tags/RELEASE_601/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
it uses gnu as target instead of llvm. My llvm-config output:
$ llvm-config --version --targets-built
6.0.1
X86
I built both clang and llvm from source using standard options for my build target(X86).
EDIT: I think it is using gcc as backend because this code produces error in online ide but works on my machine with clang++ and g++. Code relies on fact that gcc has implementation of policy based data structures which are not part of standard.
The problem is in the interpretation of the data. The target that clang refers to has to do with the platform for which you are generating code.
x86_64 This is a 64 bit processor compatible with Intel/and
unknown I'm uncertain about this one, though I believe it specifies more detail about the processor, which ain't available
linux You are using a Linux kernel/operation system
gnu The object structure should follow the gnu standards, I believe this directly maps on ELF
This will be different if you use BSD or windows as OS, or when your processor is an ARM, Intel 32 bit, Spark ...
The only moment you should be worrying about the target is when you are cross compiling. In other words, if the computer on which you are running the compiler has other requirements for the executable structure than the machine on which you will be running it.
PS: Clang always uses LLVM for it's IR. ignoring the deprecated Clang+C2, it always uses the LLVM optimizer and code generator.
Related
When using clang v8.0.0 on Windows (from llvm prebuilt binaries) with -g or -gline-tables-only source map tables are not being picked up by gdb or lldb debuggers.
Upon including -g flag file grows in size (which is to be expected) yet neither gdb nor lldb pickes the source up
When compiled with gcc though (with -g flag) source files are detected by debugger.
I have tried running the same command (clang -g <codefile>) on macOS High Sierra (clang -v says it is Apple LLVM version 10.0.0 (clang-1000/10.44.4)) where there source files are being picked up by lldb. So I guessed it is localized to my windows instance or llvm for windows build.
P.S. output of clang -v on windows:
clang version 8.0.0 (tags/RELEASE_800/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
On Windows, Clang is not self-sufficient (at least not the official binaries). You need to have either GCC or MSVC installed for it to function.
As Target: x86_64-pc-windows-msvc indicates, by default your Clang is operating in some kind of MSVC-compatible mode. From what I gathered, it means using the standard library and other libraries provided by your MSVC installation, and presumably generating debug info in some MSVC-specific format.
Add --target=x86_64-w64-windows-gnu to build in GCC-compatible mode. (If you're building for 32 bits rather than 64, replace x86_64 with i686). This will make Clang use headers & libraries provided by your GCC installation, and debug info should be generated in a GCC-compatible way. I'm able to debug resulting binaries with MSYS2's GDB (and that's also where my GCC installation comes from).
If you only have GCC installed and not MSVC, you still must use this flag.
How do I know this is the right --target? This is what MSYS2's Clang uses, and I assume they know what they're doing. If you don't want to type this flag every time, you can replace the official Clang with MSYS2's one, but I'm not sure if it's the best idea.
(I think they used to provide some patches to increase compatibility with MinGW, but now the official binaries work equally well, except for the need to specify the target. Also, last time I checked their binary distribution was several GB larger, due to their inability to get dynamic linking to work. Also some of the versions they provided were prone to crashing. All those problems come from them building their Clang with MinGW, which Clang doesn't seem to support very well out of the box. In their defence, they're actively maintaining their distribution, and I think they even ship libc++ for Windows, which the official distribution doesn't do.)
I use a command: g++ -v to know what's the version of GCC currently using. But I get following output:
$ g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
So I don't understand. Am I using LLVM or GCC when I compile my program with: g++ test.cpp? Why do I see the information about LLVM when I'm requesting the information about GCC? LLVM used with Clang as a frontend.
What have I missed there?
The short answer is you are using Apple's LLVM pretending to be gcc/g++. This is not a huge problem. One place where I was annoyed enough was the fact that gcc's -march=native optimizations just do not work with LLVM which resulted in about 50% worse performance in a rather CPU-intensive program I was running.
Getting a full gcc based toolchain installed involves a lot of work. See Compiling GCC 6 on macOS Sierra and Compiling GCC 6 on OS X.
If you get the real thing working, you might also want to build dedicated GNU binutils etc.
You're using LLVM. Unless you've specifically installed GCC (e.g. with Homebrew) you don't have GCC installed. References to GCC on your system are aliases to Clang intended to allow most code to compile without problems.
Is there a sort of official documentation about version compatibility between binutils, glibc and GCC? I found this matrix for binutils vs GCC version compatibility. It would be good to have something like this for GCC vs glibc as well.
The point I'm asking this for is that I need to know if I can build, say, cross GCC 4.9.2 with "embedded" glibc 2.2.4 to be able to support quite old targets like CentOS 5.
Thank you.
it's extremely unlikely you'll be able to build such an old version of glibc with such a new version of gcc. glibc documents the min required version of binutils & gcc in its INSTALL file.
glibc-2.23 states:
Recommended Tools for Compilation
GCC 4.7 or newer
GNU 'binutils' 2.22 or later
typically if you want to go newer than those, glibc will generally work with the version of gcc that was in development at the time of the release. e.g. glibc-2.23 was released 18 Feb 2016 and gcc-6 was under development at that time, so glibc-2.23 will work with gcc-4.7 through gcc-6.
so find the version of gcc you want, then find its release date, then look at the glibc releases from around the same time.
all that said, using an old version of glibc is a terrible idea. it will be full of known security vulnerabilities (include remotely exploitable ones). the latest glibc-2.23 release for example fixed CVE-2015-7547 which affects any application doing DNS network resolution and affects versions starting with glibc-2.9. remember: this is not the only bug lurking.
When building a cross-compiler there are at least two, and sometimes three, platform types to consider:
Platform A is used to BUILD a cross compiler HOSTED on Platform B which TARGETS binaries for embedded Platform C. I used the words BUILD, HOSTED, and TARGETS intentionally, as those are the options passed to configure when building a cross-GCC.
BUILD PLATFORM: Platform of machine which will create the cross-GCC
HOST PLATFORM: Platform of machine which will use the cross-GCC to create binaries
TARGET PLATFORM: Platform of machine which will
run the binaries created by the cross-GCC
Consider the following (Canadian Cross Config, BUILD != HOST platform):
A 32-bit x86 Windows PC running the mingw32 toolchain will be used to compile a cross-GCC. This cross-GCC will be used on 64-bit x86 Linux computers. The binaries created by the cross-GCC should run on a 32-bit PowerPC single-board-computer running LynxOS 178 RtOS (Realtime Operating System).
In the above scenario, our platforms are as follows:
BUILD: i686-w32-mingw32
HOST: x86_64-linux-gnu
TARGET: powerpc-lynx-lynxos178
However, this is not the typical configuration. Most often BUILD PLATFORM and HOST PLATFORM are the same.
A more typical scenario (Regular Cross Config, BUILD == HOST platform):
A 64-bit x86 Linux server will be used to compile a cross-GCC. This cross-GCC will also be used on 64-bit x86 Linux computers. The binaries created by the cross-GCC should run on a 32-bit PowerPC single-board-computer running LynxOS 178 RtOS (Realtime Operating System).
In the above scenario, our platforms are as follows:
BUILD: x86_64-linux-gnu
HOST: x86_64-linux-gnu
TARGET: powerpc-lynx-lynxos178
When building the cross-GCC (assuming we are building a Regular Cross Config, where BUILD == HOST Platform), native versions of GNU BinUtils, GCC, glibc, and libstdc++ (among other libraries) will be required to actually compile the cross-GCC. It is less about specific versions of each component, and more about whether each component supports the specific language features required to compile GCC-4.9.2 (note: just because GCC-4.9.2 implements language feature X, does not mean that language feature X must be supported by the version of GCC used to compile GCC-4.9.2. In the same way, just because glibc-X.X.X implements library feature Y, does not mean that the version of GCC used to compile glibc-X.X.X must have been linked against a glibc that implements feature Y.
In your case, you should simply build your cross-GCC 4.9.2 (or if you are not cross compiling, i.e. you are compiling for CentOS 5 on Linux, build native GCC 4.9.2), and then when you link your executable for CentOS 5, explicitly link glibc v2.2.4 using -l:libc.so.2.2.4. You also probably will need to define -std=c99 or -std=gnu99 when you compile, as I highly doubt glibc 2.2.4 supports the C 2011 standard.
So here's the situation: I need to be able to compile binaries from a Linux machine (on Ubuntu, for what it's worth) which are able to run from a SPARC server. The program I'm trying to compile is very simple:
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Testing the SPARC program...");
return EXIT_SUCCESS;
}
I've tried a number of different compile lines to get it to work, but unfortunately nothing appears to be working.
I tried the traditional:
clang -target sparc blah.c -o blahsparc
But this doesn't work, with a bunch of assembler failures:
/tmp/blah-519e77.s: Assembler messages:
/tmp/blah-519e77.s:7: Error: unknown pseudo-op: '.register'
/tmp/blah-519e77.s:8: Error: unknown pseudo-op: '.register'
/tmp/blah-519e77.s:9: Error: unknown pseudo-op: '.register'
/tmp/blah-519e77.s:10: Error: unknown pseudo-op: '.register'
/tmp/blah-519e77.s:11: Error: no such instruction: 'save %sp,-240,%sp'
/tmp/blah-519e77.s:12: Error: no such instruction: 'st %g0, [%fp+2043]'
...
clang: error: assembler (via gcc) command failed with exit code 1 (use -v to see invocation)
I've tried this also:
clang -cc1 -triple "sparc-unknown-Linux" blah.c -o blahsparc
which complains about the missing headers, so instead of using -cc1, I use -Xclang:
clang -Xclang -triple -Xclang "sparc-unknown-Linux" blah.c -o blahsparc
however, this also fails due to "error: unknown target CPU 'x86-64'".
I'm not sure where to proceed with this. I've tried using crosstool-ng as well with very little success.
As of the 3.4.2 release (June 2014), llvm is missing code necessary to be able to generate object files for sparc targets. Older releases (1.x & 2.x) had support for it, but llvm's framework for emitting object files was less mature back then. When the current framework was rolled out it looks like they didn't migrate all platforms.
The documentation seems to imply that a combination of llvm/gcc is known to work, but I think that table was tabulated based on a much earlier version of llvm when they had a less mature framework for emitting object files.
Support for emitting object files was added to their SVN trunk in revision r198533 (this thread discusses the commit), but as you can see in the 3.4.2 final release, files & changes added in r198533 aren't present.
As an aside, clang currently isn't functional in sparc solaris (not sure about sparc in general). The parser seems to have trouble parsing templates; I get coredumps & the like. I ran across a thread a week or so ago discussing alignment problems in sparc/solaris clang, and this may be one of the reasons clang isn't yet usable on this platform.
If you need a cross compiler for Sparc that runs on an Ubuntu machine, the simplest way I know of is to use Buildroot. Here's a small tutorial on how to obtain a cross compiler and test the generated executables on a Sparc emulator.
LLVM 3.6.2 has some support for sparc now... I was able to build llvm 3.6.2 and clang 3.6.2-r100 on my T2000. I haven't gotten C++ support working but I have built moderately complex C applications like htop.
I did compile LLVM using gcc 5.2 however I lower version should work as well although I'd suggested at least gcc 4.9 and no lower than gcc 4.7.
The LLVM emerge on gentoo crashed during the compile but I was able to resume it by moving to the portage directory with the llvm ebuilds and restarting the build manually:
cd /usr/portage/*/llvm/
ebuild llvm-3.6.2.ebuild merge
I had to override some of the default compiler:
CC="clang -target sparc-unknown-linux-gnu"
CXX="clang++ -target sparc-unknown-linux-gnu"
CFLAGS="-O2 -pipe"
CXXFLAGS="${CFLAGS}"
I don't know if you will be able to use this to build from an x86 machine... though clang is supposed to be able to do that. But worst case you might be able to get this going in qemu-system-sparc64 vm or on some real hardware that you can find cheap on ebay (T5xxx hardware is coming down in price and blades are dirt cheap)
I recently updated to clang 3.8 (which is as yet unreleased) and I was able to compile a c++ application by passing -lstdc++ in addition to the options above. I believe this is the same behavior as gcc when invoked as gcc rather than g++.
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.