aboutsummaryrefslogtreecommitdiffhomepage
path: root/content/2022-06-18-libclang-c-to-llvm-ir/index.md
blob: 7d3ee63cb690e5b8eec8126eb685a9dc8b0d1b2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
+++
title = "C to LLVM IR in memory using libclang"

[taxonomies]
tags = ["llvm", "clang", "c++"]
+++

For some experiments with the LLVM just in time (JIT) APIs, I was looking for a
way to compile in memory from `C -> LLVM IR` and without invoking Clang as a
child process.

I created a minimal example for my purpose based on the [Clang
source][src-clang] code and the example given in the blog post [Compiling C++
Code In Memory With Clang][blog-clang-in-memory].

The code listing below shows the example with detailed comments inlined, hence
I am not further describing any details here.

> The example was build & tested with LLVM & Clang 13.

```cpp
{{ include(path="content/2022-06-18-libclang-c-to-llvm-ir/gen-ir.cc") }}
```

The following Makefile can be used to compile and run the example.

```make
{{ include(path="content/2022-06-18-libclang-c-to-llvm-ir/Makefile") }}
```

[src-clang]: https://github.com/llvm/llvm-project/tree/main/clang
[blog-clang-in-memory]: https://blog.audio-tk.com/2018/09/18/compiling-c-code-in-memory-with-clang/