The answer here is stale.
In newer versions of LLVM, there is an included tool to include LLVM inside a CMake project.
Please see the documentation of how to embed LLVM in your project

You'll thus have this in your project CMakeLists.txt:

cmake_minimum_required(VERSION 3.4.3)
project(SimpleProject)

find_package(LLVM REQUIRED CONFIG)

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

# Set your project compile flags.
# E.g. if using the C++ header files
# you will need to enable C++11 support
# for your compiler.

include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})

# Now build our tools
add_executable(simple-tool tool.cpp)

# Find the libraries that correspond to the LLVM components
# that we wish to use
llvm_map_components_to_libnames(llvm_libs support core irreader)

# Link against LLVM libraries
target_link_libraries(simple-tool ${llvm_libs})

This should work.
If you are having problems on windows, install the source package and not the binary package as suggested in this thread

if you have this error:

CommandLine Error: Option 'help-list' registered more than once!
LLVM ERROR: inconsistency in registered CommandLine options

replace the ${llvm_libs} in target_link_libraries() by LLVM you can see this github thread

Answer from Narice on Stack Overflow
🌐
LLVM
llvm.org › docs › CMake.html
Building LLVM with CMake — LLVM 23.0.0git documentation
Download and install CMake. Version 3.20.0 is the minimum required. Open a shell. Your development tools must be reachable from this shell through the PATH environment variable. Create a build directory. Building LLVM in the source directory is not supported.
About
If you are using a released version of LLVM, see the download page to find your documentation · The LLVM compiler infrastructure supports a wide range of projects, from industrial strength compilers to specialized JIT applications to small research projects
LLVM Language Reference Manual
This document is a reference manual for the LLVM assembly language. LLVM is a Static Single Assignment (SSA) based representation that provides type safety, low-level operations, flexibility, and the capability of representing ‘all’ high-level languages cleanly.
Getting Started with the LLVM System
Most llvm developers use Ninja. Unix Makefiles — for generating make-compatible parallel makefiles. Visual Studio — for generating Visual Studio projects and solutions. Xcode — for generating Xcode projects. See the CMake docs for a more comprehensive list.
LLVM Coding Standards
Languages, Libraries, and Standards · C++ Standard Versions
🌐
LLVM
llvm.org › docs › GettingStarted.html
Getting Started with the LLVM System — LLVM 23.0.0git documentation
This is especially important for linking since linking can use lots of memory. If you run into memory issues building LLVM, try setting this to limit the maximum number of compile/link/tablegen jobs running at the same time. cmake --build build [--target <target>] or the build system specified above directly.
Top answer
1 of 2
25

The answer here is stale.
In newer versions of LLVM, there is an included tool to include LLVM inside a CMake project.
Please see the documentation of how to embed LLVM in your project

You'll thus have this in your project CMakeLists.txt:

cmake_minimum_required(VERSION 3.4.3)
project(SimpleProject)

find_package(LLVM REQUIRED CONFIG)

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

# Set your project compile flags.
# E.g. if using the C++ header files
# you will need to enable C++11 support
# for your compiler.

include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})

# Now build our tools
add_executable(simple-tool tool.cpp)

# Find the libraries that correspond to the LLVM components
# that we wish to use
llvm_map_components_to_libnames(llvm_libs support core irreader)

# Link against LLVM libraries
target_link_libraries(simple-tool ${llvm_libs})

This should work.
If you are having problems on windows, install the source package and not the binary package as suggested in this thread

if you have this error:

CommandLine Error: Option 'help-list' registered more than once!
LLVM ERROR: inconsistency in registered CommandLine options

replace the ${llvm_libs} in target_link_libraries() by LLVM you can see this github thread

2 of 2
5

As indicated by Marco A. in the comments, the problem were missing libraries. This link helped resolve the issue, and everything seems to be working normally now. https://stackoverflow.com/a/25783251/1938163

Thank you.

🌐
AMD ROCm
rocm.docs.amd.com › projects › llvm-project › en › latest › LLVM › llvm › html › CMake.html
Building LLVM with CMake — LLVM 22.0.0git documentation
Download and install CMake. Version 3.20.0 is the minimum required. Open a shell. Your development tools must be reachable from this shell through the PATH environment variable. Create a build directory. Building LLVM in the source directory is not supported.
🌐
Layle's Lair
layle.me › posts › using-llvm-with-cmake
LLVM with CMake: It's easier than you'd think! - Layle's Lair
We’ll be using version 12.0.1 of LLVM as we’ll need this version anyways in future posts ;) Paste the following code into CMake/HunterPackages.cmake:
🌐
GitHub
github.com › rui314 › llvm › blob › master › docs › CMake.rst
llvm/docs/CMake.rst at master · rui314/llvm
Download and install CMake. Version 2.8 is the minimum required. Open a shell. Your development tools must be reachable from this shell through the PATH environment variable. Create a directory for containing the build. It is not supported to build LLVM on the source directory.
Author   rui314
🌐
LLVM
releases.llvm.org › 16.0.0 › docs › CMake.html
Building LLVM with CMake — LLVM 16.0.0 documentation
March 18, 2023 - Download and install CMake. Version 3.13.4 is the minimum required. Open a shell. Your development tools must be reachable from this shell through the PATH environment variable. Create a build directory. Building LLVM in the source directory is not supported.
🌐
LLVM
releases.llvm.org › 20.1.0 › docs › CMake.html
Building LLVM with CMake — LLVM 20.1.0 documentation
Download and install CMake. Version 3.20.0 is the minimum required. Open a shell. Your development tools must be reachable from this shell through the PATH environment variable. Create a build directory. Building LLVM in the source directory is not supported.
Find elsewhere
🌐
LLVM
llvm.org › docs › CMakePrimer.html
CMake Primer — LLVM 23.0.0git documentation
In CMake, macros and functions are universally referred to as commands, and they are the primary method of defining code that can be called multiple times. In LLVM we have several CMake modules that are included as part of our distribution for developers who don’t build our project from source.
🌐
LLVM
releases.llvm.org › 11.0.0 › docs › CMake.html
Building LLVM with CMake — LLVM 11 documentation
Download and install CMake. Version 3.4.3 is the minimum required. Open a shell. Your development tools must be reachable from this shell through the PATH environment variable. Create a build directory. Building LLVM in the source directory is not supported.
🌐
GitHub
github.com › llvm › llvm-project › blob › main › llvm › CMakeLists.txt
llvm-project/llvm/CMakeLists.txt at main · llvm/llvm-project
option(LLVM_USE_FOLDERS "Enable solution folders in Visual Studio. Disable for Express versions." ON) ... This process created the file `CMakeCache.txt' and the directory `CMakeFiles'.
Author   llvm
🌐
LLVM
releases.llvm.org › 9.0.0 › docs › CMake.html
Building LLVM with CMake — LLVM 9 documentation
September 19, 2019 - Download and install CMake. Version 3.4.3 is the minimum required. Open a shell. Your development tools must be reachable from this shell through the PATH environment variable. Create a build directory. Building LLVM in the source directory is not supported.
🌐
AMD ROCm
rocm.docs.amd.com › projects › llvm-project › en › docs-6.3.3 › LLVM › llvm › html › CMake.html
Building LLVM with CMake — LLVM 18.0.0git documentation
February 10, 2025 - Download and install CMake. Version 3.20.0 is the minimum required. Open a shell. Your development tools must be reachable from this shell through the PATH environment variable. Create a build directory. Building LLVM in the source directory is not supported.
🌐
University of Texas
cs.utexas.edu › ~pingali › CS380C › 2019 › assignments › llvm-guide.html
Getting started with LLVM
You can choose whatever build system you prefer on your machine. On Ubuntu, I prefer Eclipse with Ninja as the build system. The steps to build LLVM are: a) generate the build system using CMake, b) use Ninja to build the huge LLVM codebase.
🌐
LLVM
releases.llvm.org › 14.0.0 › docs › CMake.html
Building LLVM with CMake — LLVM 14.0.0 documentation
Download and install CMake. Version 3.13.4 is the minimum required. Open a shell. Your development tools must be reachable from this shell through the PATH environment variable. Create a build directory. Building LLVM in the source directory is not supported.
🌐
Muxup
muxup.com › building-testing-and-distributing-llvm-clang-and-friends
Building, cross-building, testing, distributing LLVM and Clang and friends - Muxup
TODO LLVM_LIT_ARGS and LIT_ARGS_DEFAULT discussion. You can essentially combine the kinds of approaches discussed above to do a multi-stage build that first builds a fresh compiler for the host, then uses that to cross-build. Doing it as below gives you plenty of direct control over each point without having to trace the logic for the all-in-one boostrap builds. cmake -G Ninja \ -DCMAKE_BUILD_TYPE="Release" \ -DLLVM_ENABLE_PROJECTS="clang;lld" \ -DLLVM_ENABLE_ASSERTIONS=ON \ -DLLVM_CCACHE_BUILD=ON \ -DCMAKE_C_COMPILER=clang \ -DCMAKE_CXX_COMPILER=clang++ \ -DLLVM_ENABLE_LLD=ON \ -DLLVM_TARGETS_TO_BUILD="RISCV" \ -DLLVM_APPEND_VC_REV=OFF \ -B build/stage1cross \ -S llvm cmake --build build/stage1cross
🌐
LLVM
releases.llvm.org › 15.0.0 › docs › CMake.html
Building LLVM with CMake — LLVM 15.0.0 documentation
Download and install CMake. Version 3.13.4 is the minimum required. Open a shell. Your development tools must be reachable from this shell through the PATH environment variable. Create a build directory. Building LLVM in the source directory is not supported.
🌐
GitHub
github.com › LLVMParty › LLVMCMakeTemplate
GitHub - LLVMParty/LLVMCMakeTemplate: Collection of scripts and CMake files to easily link to LLVM into your project (Windows, Linux, macOS). · GitHub
# Checkout LLVM 15 git clone -b release/15.x --single-branch https://github.com/llvm/llvm-project.git --depth 1 cd llvm-project # Build LLVM cmake -G Ninja -B build -S llvm -DCMAKE_BUILD_TYPE=Release "-DLLVM_ENABLE_PROJECTS=clang;lld" -DLLVM_PARALLEL_LINK_JOBS=1 -DLLVM_BUILD_EXAMPLES=OFF -DLLVM_BUILD_TESTS=OFF cmake --build build cmake --install build --prefix build/install
Starred by 43 users
Forked by 5 users
Languages   CMake 75.3% | C++ 23.2% | Dockerfile 1.5%
🌐
LLVM
releases.llvm.org › 8.0.0 › docs › CMake.html
Building LLVM with CMake — LLVM 8 documentation
March 18, 2019 - Download and install CMake. Version 3.4.3 is the minimum required. Open a shell. Your development tools must be reachable from this shell through the PATH environment variable. Create a build directory. Building LLVM in the source directory is not supported.
🌐
LLVM
releases.llvm.org › 18.1.8 › docs › CMake.html
Building LLVM with CMake — LLVM 18.1.8 documentation
June 19, 2024 - Download and install CMake. Version 3.20.0 is the minimum required. Open a shell. Your development tools must be reachable from this shell through the PATH environment variable. Create a build directory. Building LLVM in the source directory is not supported.