Could I get one single bc file from multiple files?(which may have a real big amount of files including .c/.h/.hpp)

Hello everyone, i am a new one here.
Just like the question, I know it’s not a question about LLVM itself. But I need do some analysis job on code divided in different files.
It more like to be a question about preprocessing, that put the code not including the header files into a single file and then use clang command make them to one single file in bc format.
I hope to get some useful tools to finish it, since it’s not the main thing i working on.
I appreciate any word from you. Thanks again.

1 Like

I can think of two ways to do this. One is often called a “unity” build, where you have one file (maybe unity.cpp) that just includes all the .cpp files from your project. So if you had main.cpp, dep1,cpp, and dep1.cpp, you would create a file with:

#include "main.cpp"
#include "dep1.cpp"
#include "dep2.cpp"

so that clang -c -emit-llvm unity.cpp will process all your sources and emit a single .bc file.

The other way is to use the llvm-link tool; I have not done this myself so I’m not completely sure how it works, but this is a bitcode linker (not an object-file linker).

2 Likes

Thanks for your suggestion! : )
I will try them all to find if them work.
The first way seem like could be finished by a simple script. And i will do some research on the llvm-link.
If I find a good way to solve this question finally, i will post it here hoping to help someonelse.
Again, I would like to expreour thanks to you and your time.

Hi, sir.
I think i find a fine solution, which rely on the LLVMgold.so and “-flto -fuse-ld=gold -save-temps -Wl,-plugin-opt=save-temps”.
Find a example from stackoverflow which could help a lot.ld - Get llvm IR after lto linking - Stack Overflow
By the way, the situation that LLVMGold.so is lost is pretty common. So this web is all you need. Optimizations - The LLVM gold plugin - 《LLVM 10 User Guides》 - 书栈网 · BookStack

You can also use clang to compile each file to bitcode, and then use llvm-link to create a single bitcode file.

Yes, but after test, i do believe that there is a single bc beside the final elf file or .so file.
Thanks your book, which is simple but useful.
By the way. I have a small script for myself. build.sh (github.com)

1 Like

I used wllvm to compile a project to a single bc file.