This tutorial outlines how to use Clang as a library to enable basic C/C++ incremental compilation infrastructure by building a C++ interpreter that connects to the Python interpreter. We:
- Learn how to use Clang as a library
- Build a simple C++ REPL with
clang::Interpreter - See how we can bridge compiled and interpreted code
- Develop building blocks for a basic Compiler-as-a-Service
- Use the CaaS layer to bridge Python and C++
Requires LLVM/Clang 21, CMake ≥ 3.13, a C++17 compiler, and Python3 for exercises 4 and 5.
git clone https://github.com/compiler-research/eurollvm26-tutorial.git
cd eurollvm26-tutorial && mkdir build && cd build
cmake -DLLVM_DIR=/usr/lib/llvm-21/lib/cmake/llvm \
-DClang_DIR=/usr/lib/llvm-21/lib/cmake/clang ..
makeSubstitute whichever /usr/lib/llvm-21 with your install:
- mac:
$(brew --prefix llvm@21)/lib/cmake/... - source build:
path/to/build/lib/cmake/....
- ex1 — Use Clang components as a library to build an AST, walk the translation unit, and extract a class template's Doxygen comment.
- ex2 — Build a simple C++ REPL built leveraging
IncrementalCompilerBuilderandclang::Interpreter. - ex3 — Use
clang::Valueto capture expression results, andgetSymbolAddressto call JIT'd functions from compiled code. - ex4 — Builds a lightweight library containing Compiler-as-a-Service building blocks using
InterpreterandSema, exposing template instantiation driven from both C and Python (expressMyClass<int>in Python). - ex5 — extends ex4 with
clang::Valuecapture, allowing the Python template wrapper to perform expression evaluation and read the typed primitive results back.