Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
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
"Programmers are tools for converting caffeine into code."
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.
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.
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.
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."
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.
quanganht wrote:I prefer llvm linker because it can do cross-file optimizations.
So you're using an outdated version of GCC. Upgrade it unless you have good reason to not do so.
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.
Owen wrote:GCC's cross file optimizations require the GOLD linker, which is pretty rare.
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:Additionally, GCC LTO is well known for being buggy and breaking lots of code.
You can remove "LTO" from this sentence and it'll still be true (well, at least the part about being buggy)
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.
There's no way to emit an executable directly from llvm?
"Programmers are tools for converting caffeine into code."
quanganht wrote:Thanks guys for your help. I will give LLVM a try
Hi,
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.
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.
"Programmers are tools for converting caffeine into code."
quanganht wrote:...because I have to put assembly (Intel syntax),C and C++ code together.
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.)
(*) 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.
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.
Why is that a problem? Compile everything to .o with clang straight away then.
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.
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