Using Clang instead of GCC?
Using Clang instead of GCC?
Hi,
According to Clang and LLVM documentations, they are far better than GCC in compiling time, speed of generted code, expressive messages...and sound like a perfect compiler for C/C++. What I'm thinking is: Is it good to use Clang/LLVM for OS development? Can they replace GCC? How can I build a cross-compiler out of the source?
Looking forward for your opinions.
QA
According to Clang and LLVM documentations, they are far better than GCC in compiling time, speed of generted code, expressive messages...and sound like a perfect compiler for C/C++. What I'm thinking is: Is it good to use Clang/LLVM for OS development? Can they replace GCC? How can I build a cross-compiler out of the source?
Looking forward for your opinions.
QA
"Programmers are tools for converting caffeine into code."
Re: Using Clang instead of GCC?
Hi,
Clang and LLVM certainly are of good quality - the code generation for Intel isn't *quite* up to the standard of GCC yet, but it's certainly getting there at a fast rate and to be honest, you're unlikely to see the difference.
With Clang and LLVM you don't actually need a cross-compiler. Clang can generate LLVM bitcode for you to compile and link yourself ("-emit-llvm"), or can invoke LLVM itself and generate an object file ("-c -o myobj.o") - exactly the same as GCC.
Once you have your object files, you can link them with whatever tool you wish - llvm has its own linker, or you can use GNU LD as you currently do. LLVM isn't as hard-tied to the platform's glibc, so you don't need a cross compiler.
Hope this helps,
James
Clang and LLVM certainly are of good quality - the code generation for Intel isn't *quite* up to the standard of GCC yet, but it's certainly getting there at a fast rate and to be honest, you're unlikely to see the difference.
With Clang and LLVM you don't actually need a cross-compiler. Clang can generate LLVM bitcode for you to compile and link yourself ("-emit-llvm"), or can invoke LLVM itself and generate an object file ("-c -o myobj.o") - exactly the same as GCC.
Once you have your object files, you can link them with whatever tool you wish - llvm has its own linker, or you can use GNU LD as you currently do. LLVM isn't as hard-tied to the platform's glibc, so you don't need a cross compiler.
Hope this helps,
James
Re: Using Clang instead of GCC?
Thanks for the reply
Edit: I saw llvm-ld have an command line option named "-native", which instructs llvm-ld to generate native executable. Do we have to concern about it?
So that means machine code produced by llvm linker can run without glibc and/or standard libs? I prefer llvm linker because it can do cross-file optimizations.JamesM wrote:Once you have your object files, you can link them with whatever tool you wish - llvm has its own linker, or you can use GNU LD as you currently do. LLVM isn't as hard-tied to the platform's glibc, so you don't need a cross compiler.
Edit: I saw llvm-ld have an command line option named "-native", which instructs llvm-ld to generate native executable. Do we have to concern about it?
"Programmers are tools for converting caffeine into code."
Re: Using Clang instead of GCC?
Hi,
You can of course keep everything in LLVM bitcode form for the final link - this enabled LLVM to do its link-time optimisations.
You will at some point still need to generate machine code - this is done using "llc", which takes an LLVM bitcode file and spits out an assembly (GNU as format) file that you must then assemble.
Cheers,
James
You can of course keep everything in LLVM bitcode form for the final link - this enabled LLVM to do its link-time optimisations.
You will at some point still need to generate machine code - this is done using "llc", which takes an LLVM bitcode file and spits out an assembly (GNU as format) file that you must then assemble.
Cheers,
James
Re: Using Clang instead of GCC?
So you're using an outdated version of GCC. Upgrade it unless you have good reason to not do so.quanganht wrote:I prefer llvm linker because it can do cross-file optimizations.
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: Using Clang instead of GCC?
GCC's cross file optimizations require the GOLD linker, which is pretty rare. Additionally, GCC LTO is well known for being buggy and breaking lots of code.Fanael wrote:So you're using an outdated version of GCC. Upgrade it unless you have good reason to not do so.quanganht wrote:I prefer llvm linker because it can do cross-file optimizations.
Re: Using Clang instead of GCC?
It doesn't (actually, it does, but only for archives) - if it requires GOLD (ELF-only linker), how does it work under Windows (which uses PE/COFF) and Mac OS (Mach-O)?Owen wrote:GCC's cross file optimizations require the GOLD linker, which is pretty rare.
You can remove "LTO" from this sentence and it'll still be true (well, at least the part about being buggy)Owen wrote:Additionally, GCC LTO is well known for being buggy and breaking lots of code.
Re: Using Clang instead of GCC?
There's no way to emit an executable directly from llvm?JamesM wrote:You will at some point still need to generate machine code - this is done using "llc", which takes an LLVM bitcode file and spits out an assembly (GNU as format) file that you must then assemble.
"Programmers are tools for converting caffeine into code."
Re: Using Clang instead of GCC?
Thanks guys for your help. I will give LLVM a try
"Programmers are tools for converting caffeine into code."
Re: Using Clang instead of GCC?
Hi,quanganht wrote:Thanks guys for your help. I will give LLVM a try
You asked about keeping the intermediate files in LLVM bitcode format instead of ELF partially-linked .o. You can do that, but the front-end driver cannot then generate the executable using the same command line as GCC would - you have to use the "llc" tool (which comes with LLVM - it's just a driver front end!) to generate the executable.
So yes, it's still done "inside of LLVM".
James
Re: Using Clang instead of GCC?
Hmmm, clang man page says "Clang currently does not have C++ support..." while on its website it is said to support all iso c++ 98 standards
And i still find the building process quite confusing, because I have to put assembly (Intel syntax),C and C++ code together. Cannot get an executable yet.
And i still find the building process quite confusing, because I have to put assembly (Intel syntax),C and C++ code together. Cannot get an executable yet.
"Programmers are tools for converting caffeine into code."
Re: Using Clang instead of GCC?
OT: Definitely one language too many. If you're doing C++, there's no reason to have C files since there's always extern "C" if necessary. (Virtually any decent C can be compiled with a C++ compiler(*), with minimal changes if need be, reducing the number of languages used, which is A Good Thing.)quanganht wrote:...because I have to put assembly (Intel syntax),C and C++ code together.
(*) And because I cannot resist the temptation for my favourite flame angle, this goes to prove that the Linux kernel is not decent C. On purpose.
Every good solution is obvious once you've found it.
Re: Using Clang instead of GCC?
Why is that a problem? Compile everything to .o with clang straight away then.quanganht wrote:Hmmm, clang man page says "Clang currently does not have C++ support..." while on its website it is said to support all iso c++ 98 standards
And i still find the building process quite confusing, because I have to put assembly (Intel syntax),C and C++ code together. Cannot get an executable yet.
Or, if you still want LTO, compile everything with clang or llvm-gcc to LLVM bitcode except the assembly, link it, "llc" it to create a partially-linked object file (standard .o), then link that with GNU ld with your assembly.
Magic.
Re: Using Clang instead of GCC?
What's happening right now is output object files from clang is not recognized by gnu ld
Edit: Maybe something is wrong with my toolchain. Like JamesM said, I used llc to get an assembly file (Kernel.s) then compile it with gnu as
But when I try to link, the same error appears.
Edit2: found it. llc emitted 64-bit asm code. But adding "-march=x86" to llc makes it spit out broken code
Code: Select all
~clang -c -nostdlib -nodefaultlibs -mcmodel=large Kernel.cpp -o Kernel.o
~nasm -f elf loader.asm -o loader.o
~i586-elf-ld -T link.ld -o Kernel .bin loader.o Kernel.o
Kernel.o: file not recognized: File format not recognized
Code: Select all
as Kernel.s -o Kernel.o
Edit2: found it. llc emitted 64-bit asm code. But adding "-march=x86" to llc makes it spit out broken code
"Programmers are tools for converting caffeine into code."
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: Using Clang instead of GCC?
Hang on - "-mcmodel=large" implies 64-bit code. Additionally, since you're not passing an architecture specification to clang, it will produce native code for your platform (presumably, x86_64). You need to make clang emit 32-bit code too.
LLVM bitcode generated from C(++) is not portable; too much information has been discarded
LLVM bitcode generated from C(++) is not portable; too much information has been discarded