Recently I got interests in clang & its library(Libtooling, more specifically) so that I decided to start digging in. However, the first frustration came from while building the clang examples.
Download and build the clang source tree
Building the clang executable was easy. The Clang website provides instructions and you’ll get the result by just following it. The clang source code is located as a form of subproject of the llvm. So you need to download the llvm first and clang in order.
- Check out LLVM:
- Change directory to where you want the llvm directory placed.
- svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
- Check out Clang:
- cd llvm/tools
- svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
- cd ../..
Then the overall source tree will look like this:
The llvm & clang use the CMake, so as a next stage, you’ll need to generate Makefile or any solution files for your favorite build tools with it.
Build LLVM and Clang:
- mkdir build (in-tree build is not supported)
- cd build
- cmake -G "Unix Makefiles" ../llvm
- This builds both LLVM and Clang for debug mode.
- Note: For subsequent Clang development, you can just run make clang.
- CMake allows you to generate project files for several IDEs: Xcode, Eclipse CDT4, CodeBlocks, Qt-Creator (use the CodeBlocks generator), KDevelop3. For more details see Building LLVM with CMake page.
I created a ‘clang-build’ directory instead of ‘build’ with a same level as ‘llvm’ then followed the steps above. You’ll see the llvm & clang executables after finished the whole building process usually takes >= 20 mins.
Where’s the my clang example binaries?
If you look into llvm/tools/clang/examples directory, there’re four examples – AnnotateFunctions, PrintFunctionNames, analyzer-plugin, and clang-interpreter. However, I couldn’t find the executables of those four, later I found out that I should add another flag to build the examples at here(http://stackoverflow.com/questions/6265624/how-to-build-clang-examples-printfunctionnames)
So if you followed the exact steps described above, then no example files are generated:
➜ clang-build$ pwd
➜ clang-build$ find . -name ‘PrintFunctionNames*’ -type f
The linked Stackoverflow’s post suggests run
but it didn’t work either with previously generated Makefile. So, I went back to the llvm source directory and had to re-run the following command to generate Makefile again(as the link also suggested).
cmake –DBUILD_EXAMPLES=1 ../llvm
But it still didn’t succeed. So, lastly, I tried
cmake -DLLVM_BUILD_EXAMPLES=1 -DCLANG_BUILD_EXAMPLES=1 ../llvm
make (*without* parameter)
Then finally I was able to see the example result.
➜ clang-build find . -name ‘PrintFunctionNames*’ -type f
You can also read another CMake options at here: http://llvm.org/releases/3.8.1/docs/CMake.html
Now let’s see how to build your own clang example outside of clang-source tree as next.