[clang] 71022d1 - [Clang] [Docs] Add some CMake example code for linking against libclang (#166268)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 4 08:13:27 PST 2025
Author: Sirraide
Date: 2025-11-04T17:13:23+01:00
New Revision: 71022d1ed6f1446fde4ca13f21259c5e550af0f7
URL: https://github.com/llvm/llvm-project/commit/71022d1ed6f1446fde4ca13f21259c5e550af0f7
DIFF: https://github.com/llvm/llvm-project/commit/71022d1ed6f1446fde4ca13f21259c5e550af0f7.diff
LOG: [Clang] [Docs] Add some CMake example code for linking against libclang (#166268)
Though we have a few code examples in our documentation that show how to
*use* libclang, we never actually show how to *link* against it. I
myself mostly figured this out through trial and error some time ago,
and I’ve since had to explain it to others on several occasions, so I
thought adding some very minimal CMake example code might be helpful.
Added:
Modified:
clang/docs/LibClang.rst
Removed:
################################################################################
diff --git a/clang/docs/LibClang.rst b/clang/docs/LibClang.rst
index e747022b9c173..6c62bcb5f8c29 100644
--- a/clang/docs/LibClang.rst
+++ b/clang/docs/LibClang.rst
@@ -38,6 +38,7 @@ Code example
.. code-block:: cpp
+ // main.cpp
#include <clang-c/Index.h>
#include <iostream>
@@ -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
diff erent 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})
+ target_link_libraries(my_clang_tool PRIVATE libclang)
+
Visiting elements of an AST
~~~~~~~~~~~~~~~~~~~~~~~~~~~
The elements of an AST can be recursively visited with pre-order traversal with ``clang_visitChildren``.
@@ -283,6 +300,7 @@ Complete example code
.. code-block:: cpp
+ // main.cpp
#include <clang-c/Index.h>
#include <iostream>
@@ -356,6 +374,21 @@ Complete example code
);
}
+.. 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
diff erent 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})
+ target_link_libraries(my_clang_tool PRIVATE libclang)
.. _Index.h: https://github.com/llvm/llvm-project/blob/main/clang/include/clang-c/Index.h
More information about the cfe-commits
mailing list