Are llvm-gcc and clang binary compatible with gcc? - particularly mingw gcc on Windows
Asked Answered
A

5

28

If I build a static library with llvm-gcc, then link it with a program compiled using mingw gcc, will the result work?

The same for other combinations of llvm-gcc, clang and normal gcc. I'm interested in how this works out on Linux (using normal non-mingw gcc, of course) and other platforms as well, but the emphasis is on Windows.

I'm also interested in all languages, but with a strong emphasis on C and C++ - obviously clang doesn't support Fortran etc, but I believe llvm-gcc does.

I assume they all use the ELF file format, but what about call conventions, virtual table layouts etc?

Anthropomorphosis answered 10/7, 2010 at 1:55 Comment(0)
B
24

Yes, for C code Clang and GCC are compatible (they both use the GNU Toolchain for linking, in fact.) You just have to make sure that you tell clang to create compiled objects and not intermediate bitcode objects. C ABI is well-defined, so the only issue is storage format.

C++ is not portable between compilers in the slightest; different compilers use different virtual table calls, constructors, destruction, name mangling, template implementations, etc. As a rule you should assume objects from one C++ compiler will not work with another.

However yes, at the time of writing Clang++ is able to use GCC/C++ compiled libraries as well; I recently set up a rig to compile C++ programs with clang using G++'s standard runtime library and it compiles+links just fine.

Bellini answered 30/6, 2012 at 14:4 Comment(4)
"...at the time of writing Clang++ is able to use GCC/C++ compiled libraries as well": this must have changed in the meantime, I had to recompile Boost (V1.54) with Clang++ (V3.3) before being able to link Clang++ - compiled code against it. See also: #11082318Secateurs
Forgot to say that you have to do this if you want to use Clang++ in C++11 mode. You must specify the clang/LLVM C++11-compatible standard library, gcc's standard lib is not good.Secateurs
Accept switched based on popular opinion. As far as I recall, I didn't really get an answer I was fully happy with, so just accepted anything that wasn't my own non-answer. As this answer seems most useful to others, though, it should probably be accepted.Anthropomorphosis
@user465139, are you sure you weren't trying to link a program linked against libc++ with a version of Boost that is linked against libstdc++?Virtual
B
2

I don't know the answer, but slide 10 in this presentation seems to imply that the ".o" files produced by llvmgcc contain LLVM bytecode (.bc) instead of the usual target-specific object code, so that link-time optimization is possible. However, the LLVM linker should be able to link LLVM code with code produced by "normal" GCC, as the next slide says "link in native .o files and libraries here".

LLVM is a Linux tool, I have sometimes found that Linux compilers don't work quite right on Windows. I would be curious whether you get it to work or not.

Backward answered 16/7, 2010 at 21:44 Comment(2)
I believe they have been pushing to get as much done as they can and maybe neglecting quality.. It would explain how I'm on linux and am only now getting a working compiler out of the llvm project.Ferrara
LLVM is not "a Linux tool". It is also the preferred compiler toolchain on Apple's OS X which is Unix.Secateurs
S
2

I use -m i386pep when linking clang's .o files by ld. llvm's devotion to integrating with gcc is seen openly at http://dragonegg.llvm.org/ so its very intuitive to guess llvm family will greatly be cross-compatible with gcc tool-chain.

Selfimprovement answered 4/3, 2013 at 12:31 Comment(0)
A
1

Sorry - I was coming back to llvm after a break, and have never done much more than the tutorial. First time around, I kind of burned out after the struggle getting LLVM 2.6 to build on MinGW GCC - thankfully not a problem with LLVM 2.7.

Going through the tutorial again today I noticed in Chapter 5 of the tutorial not only a clear statement that LLVM uses the ABI (Application Binary Interface) of the platform, but also that the tutorial compiler depends on this to allow access to external functions such as sin and cos.

I still don't know whether the compatible ABI extends to C++, though. That's not an issue of call conventions so much as name mangling, struct layout and vtable layout.

Being able to make C function calls is enough for most things, there's still a few issues where I care about C++.

Anthropomorphosis answered 10/7, 2010 at 21:47 Comment(0)
H
-2

Hopefully they fixed it but I avoid llvm-gcc because I (also) use llvm as a cross compiler and when you use llvm-gcc -m32 on a 64 bit machine the -m32 is ignored and you get 64 bit ints which have to be faked on your 32 bit target machine. Clang does not have that bug nor does gcc. Also the more I use clang the more I like. As to your direct question, dont know, in theory these days targets have well known or used calling conventions. And you would hope both gcc and llvm conform to the same but you never know. the simplest way to find this out is to write a couple of simple functions, compile and disassemble using both tool sets and see how they pass operands to the functions.

Hypersensitize answered 11/7, 2010 at 4:22 Comment(1)
Sorry, had to downvote this because it doesn't actually answer the question, and the advice given (to examine and compare the code output) does not strike me as good advice. There's more to an ABI than how parameters are passed to functions, especially if C++ and not just C is involved, and I'd only trust an expert to assess the compatibility of two compilers.Villada

© 2022 - 2024 — McMap. All rights reserved.