I have tried to generate C code from C++ code compiled by llvm-g++
, using the following commands:
llvm-g++ -emit-llvm -c ./example.cpp -o example.o
llc -march=c example.o
I tried these commands on a machine running Ubuntu (Linux 3.16.0-45-generic)
.
However instead of writing C code to standard output, the LLVM static linker reports that the compiled file is invalid: error: expected top-level entity
.
How can I generate C code using the LLVM linker?
The original C backend (llvm-cbe) was removed in 3.1 (release notes), but there is this Julia project, resurrected LLVM "C Backend", with improvements, which resurrected it.
Unfortunately emitting LLVM bitcode on Ubuntu systems can be a bit painful because they ship DragonEgg as the default frontend - see this question and this bugreport.
If you do a file example.o
on the file you generated above, you'll see that it's not actually LLVM IR bitcode (which explains the error):
$ file example.o
example.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
The easiest way to get LLVM IR bitcode on an Ubuntu system is to use clang:
$ clang -emit-llvm -c example.cpp -o example.o
$ file example.o
example.o: LLVM IR bitcode
That said, the C backend was removed in LLVM 3.1 (see the release notes and this question). As you can see from the output of llc -version
it's not listed and trying to use it gives the following error on my Ubuntu 14.04 system:
llc-3.4: error: invalid target 'c'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With