[clang] [Clang] [Docs] Add some CMake example code for linking against libclang (PR #166268)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 4 08:15:26 PST 2025
================
@@ -57,6 +58,22 @@ Code example
CXCursor cursor = clang_getTranslationUnitCursor(unit); //Obtain a cursor at the root of the translation unit
}
+.. code-block:: cmake
+
+ # CMakeLists.txt
+ cmake_minimum_required(VERSION 3.20)
+ project(my_clang_tool VERSION 0.1.0)
+
+ # This will find the default system installation of Clang; if you want to
+ # use a different build of clang, pass -DClang_DIR=/foobar/lib/cmake/clang
+ # to the CMake configure command, where /foobar is the build directory where
+ # you built Clang.
+ find_package(Clang CONFIG REQUIRED)
+
+ add_executable(my_clang_tool main.cpp)
+ target_include_directories(my_clang_tool PRIVATE ${CLANG_INCLUDE_DIRS})
----------------
Sirraide wrote:
Yeah, normally `target_link_libraries` should set the include directories properly, but I distinctly recall running into horrible crashes on several occasions whenever I forgot this step because it’d find my system clang headers but link against my more up-to-date local clang build, which of course didn’t end well, so I made sure to include that here.
https://github.com/llvm/llvm-project/pull/166268
More information about the cfe-commits
mailing list