Build your own clang example outside of the llvm source tree

Now it’s time to build our own clang standalone tool. The official LibTooling document provides an example code so that I was able to begin with it.


How to write CMakeLists.txt for the standalone clang tool?


Of course using the cmake is not required, but I wanted to go with it. At first I tried to use the CMakeLists which is included the rtags with few modifications, but it didn’t work well. Instead of digging it further, I tried another CMakeLists found and easily succeeded after adding more libs to link with.

However If you run the cmake, an error will be occurred indicating it failed to find the cmake file for llvm as below output:


— The C compiler identification is AppleClang
— The CXX compiler identification is AppleClang
— Check for working C compiler: /Applications/
— Check for working C compiler: /Applications/ — works
— Detecting C compiler ABI info
— Detecting C compiler ABI info – done
— Detecting C compile features
— Detecting C compile features – done
— Check for working CXX compiler: /Applications/
— Check for working CXX compiler: /Applications/ — works
— Detecting CXX compiler ABI info
— Detecting CXX compiler ABI info – done
— Detecting CXX compile features
— Detecting CXX compile features – done
CMake Error at CMakeLists.txt:26 (find_package):
Could not find a package configuration file provided by “LLVM” with any of
the following names:


Add the installation prefix of “LLVM” to CMAKE_PREFIX_PATH or set
“LLVM_DIR” to a directory containing one of the above files.  If “LLVM”
provides a separate development package or SDK, be sure it has been
— Configuring incomplete, errors occurred!
See also “/Users/hekim/Dropbox/clangtool/build/CMakeFiles/CMakeOutput.log”.


Screenshot 2016-08-12 08.27.34


In such case, you need to define CMAKE_PREFIX_PATH which contains LLVMConfig.cmake for its subdirectory. The files are located at:

➜  build$ find ~/Documents/sources/clang-build -name LLVMConfig.cmake

Screenshot 2016-08-12 08.30.50


So, I added the llvm&clang build output root directory(where the Makefile files are generated by cmake),


➜  build$ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_PREFIX_PATH=/Users/hekim/Documents/sources/clang-build ..
— Found LLVM 4.0.0svn
— Using LLVMConfig.cmake in: /Users/hekim/Documents/sources/clang-build/lib/cmake/llvm
— User selected librarys = clangFrontend;clangSerialization;clangDriver;clangParse;clangRewriteFrontend;clangStaticAnalyzerFrontend;clangSema;clangAnalysis;clangEdit;clangAST;clangLex;clangBasic;clangTooling
— User selected components = mcparser;bitreader;support;mc;option
—     =
— Configuring done
— Generating done
— Build files have been written to: /Users/hekim/Dropbox/clangtool/build

Screenshot 2016-08-12 08.35.05

➜  build$ make
Scanning dependencies of target example
[ 50%] Building CXX object CMakeFiles/example.dir/main.cpp.o
[100%] Linking CXX executable example
[100%] Built target example
➜  build$ ./example
example: Not enough positional command line arguments specified!
Must specify at least 1 positional argument: See: ./example – help

Screenshot 2016-08-12 08.35.36

Then it succeeded.

Where’s my ‘llvm/tools/clang/example’ binaries?

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.

  1. Check out LLVM:
  2. Check out Clang:

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
  • make
  • 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(

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
➜  clang-build$


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


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:

Now let’s see how to build your own clang example outside of clang-source tree as next.