Search code examples
cmakeclangllvmllvm-clang

How to get the bitcode when I use the cmake to compile the cppfile?


I have searched the tutorial that I can use the command line to try : clang -emit-llvm -o file.bc but how to write it in the cmakelists. I have to get all of bitcode file from the source code files.


Solution

  • Several options possible:

    • You can add particular flag to all your sources: set ( CMAKE_C_FLAGS "-emit-llvm")
    • Or if you still want to have .bc file and .o file at same time, you perhaps should introduce custom compile command, which is in fact two commands: first compiles .bc file, and second either just compiles .o file or converts .bc file into .o file by means of llc.
    • If you compile with -flto flag, then all .o files will be in fact .bc files (you can check it with llvm-bcanalyzer for example). But in this case you have to link it with ld.lld