+
+<div class="question">
+<p><a name="translatecxx">Can I use LLVM to convert C++ code to C code?</a></p>
+</div>
+
+<div class="answer">
+<p>Yes, you can use LLVM to convert code from any language LLVM supports to C.
+ Note that the generated C code will be very low level (all loops are lowered
+ to gotos, etc) and not very pretty (comments are stripped, original source
+ formatting is totally lost, variables are renamed, expressions are
+ regrouped), so this may not be what you're looking for. Also, there are
+ several limitations noted below.<p>
+
+<p>Use commands like this:</p>
+
+<ol>
+ <li><p>Compile your program with llvm-g++:</p>
+
+<pre class="doc_code">
+% llvm-g++ -emit-llvm x.cpp -o program.bc -c
+</pre>
+
+ <p>or:</p>
+
+<pre class="doc_code">
+% llvm-g++ a.cpp -c -emit-llvm
+% llvm-g++ b.cpp -c -emit-llvm
+% llvm-ld a.o b.o -o program
+</pre>
+
+ <p>This will generate program and program.bc. The .bc
+ file is the LLVM version of the program all linked together.</p></li>
+
+ <li><p>Convert the LLVM code to C code, using the LLC tool with the C
+ backend:</p>
+
+<pre class="doc_code">
+% llc -march=c program.bc -o program.c
+</pre></li>
+
+ <li><p>Finally, compile the C file:</p>
+
+<pre class="doc_code">
+% cc x.c -lstdc++
+</pre></li>
+
+</ol>
+
+<p>Using LLVM does not eliminate the need for C++ library support. If you use
+ the llvm-g++ front-end, the generated code will depend on g++'s C++ support
+ libraries in the same way that code generated from g++ would. If you use
+ another C++ front-end, the generated code will depend on whatever library
+ that front-end would normally require.</p>
+
+<p>If you are working on a platform that does not provide any C++ libraries, you
+ may be able to manually compile libstdc++ to LLVM bitcode, statically link it
+ into your program, then use the commands above to convert the whole result
+ into C code. Alternatively, you might compile the libraries and your
+ application into two different chunks of C code and link them.</p>
+
+<p>Note that, by default, the C back end does not support exception handling.
+ If you want/need it for a certain program, you can enable it by passing
+ "-enable-correct-eh-support" to the llc program. The resultant code will use
+ setjmp/longjmp to implement exception support that is relatively slow, and
+ not C++-ABI-conforming on most platforms, but otherwise correct.</p>
+
+<p>Also, there are a number of other limitations of the C backend that cause it
+ to produce code that does not fully conform to the C++ ABI on most
+ platforms. Some of the C++ programs in LLVM's test suite are known to fail
+ when compiled with the C back end because of ABI incompatibilities with
+ standard C++ libraries.</p>