Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions clang/docs/LibClang.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Code example

.. code-block:: cpp

// main.cpp
#include <clang-c/Index.h>
#include <iostream>

Expand All @@ -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})
Copy link
Contributor

@Endilll Endilll Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole target_include_directories invocation should not have been needed if we had our CMake code in shape, but that's not your fault anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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``.
Expand Down Expand Up @@ -283,6 +300,7 @@ Complete example code

.. code-block:: cpp

// main.cpp
#include <clang-c/Index.h>
#include <iostream>

Expand Down Expand Up @@ -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 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})
target_link_libraries(my_clang_tool PRIVATE libclang)

.. _Index.h: https://github.com/llvm/llvm-project/blob/main/clang/include/clang-c/Index.h

Expand Down
Loading