# need help with cmake code to use Lua

**URL:** https://discourse.cmake.org/t/need-help-with-cmake-code-to-use-lua/15320
**Category:** Usage
**Created:** [November 18, 2025, 8:03am UTC](https://discourse.cmake.org/t/need-help-with-cmake-code-to-use-lua/15320 "2025-11-18T08:03:27Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![solomoncyj](https://discourse.cmake.org/user_avatar/discourse.cmake.org/solomoncyj/32/4765_2.png) [@solomoncyj](https://discourse.cmake.org/u/solomoncyj)
#### Post date: [November 18, 2025, 8:03am UTC](https://discourse.cmake.org/t/need-help-with-cmake-code-to-use-lua/15320/1 "2025-11-18T08:03:27Z")

</div>

code:

```auto
if (CHATTERINO_PLUGINS)

    find_package(Sol2 REQUIRED)
    find_package(Lua 5.4)

    if (NOT Lua_FOUND)
        set(LUA_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/lib/lua/src")
        add_subdirectory(lib/lua)
    else()
        # When Lua is found via find_package, use its include directories
        if (Lua_INCLUDE_DIRS)
            set(LUA_INCLUDE_DIRS "${Lua_INCLUDE_DIRS}")
        elseif (Lua_INCLUDE_DIR)
            set(LUA_INCLUDE_DIRS "${Lua_INCLUDE_DIR}")
        else()
            # If include directories aren't provided, fall back to building from source
            message(WARNING "Lua found but include directories not available, building from source")
            set(LUA_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/lib/lua/src")
            add_subdirectory(lib/lua)
        endif()
        # If we didn't build from source and find_package didn't create a target, create an imported target
        if (NOT TARGET lua AND LUA_INCLUDE_DIRS)
            if (Lua_LIBRARIES)
                add_library(lua UNKNOWN IMPORTED)
                set_target_properties(lua PROPERTIES
                    IMPORTED_LOCATION "${Lua_LIBRARIES}"
                    INTERFACE_INCLUDE_DIRECTORIES "${LUA_INCLUDE_DIRS}"
                )
            else()
                # No library found either, build from source
                message(WARNING "Lua found but library not available, building from source")
                set(LUA_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/lib/lua/src")
                add_subdirectory(lib/lua)
            endif()
        elseif (TARGET lua AND LUA_INCLUDE_DIRS)
            # Ensure include directories are set on existing target
            get_target_property(lua_inc_dirs lua INTERFACE_INCLUDE_DIRECTORIES)
            if (NOT lua_inc_dirs)
                set_target_properties(lua PROPERTIES
                    INTERFACE_INCLUDE_DIRECTORIES "${LUA_INCLUDE_DIRS}"
                )
            endif()
        endif()
    endif()
endif()

```

pr for contex: [use system lua by solomoncyj · Pull Request #6495 · Chatterino/chatterino2 · GitHub](https://github.com/Chatterino/chatterino2/pull/6495)

the problem i’m facing is even when lua’s develpment files are installed, I always get `Lua found but include directories not available, building from source`

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [November 20, 2025, 3:58pm UTC](https://discourse.cmake.org/t/need-help-with-cmake-code-to-use-lua/15320/2 "2025-11-20T15:58:39Z")

</div>

If you look at the [documentation of CMake’s `FindLua` module](https://cmake.org/cmake/help/latest/module/FindLua.html), you will see that the only variable related to include directories which it may set is `LUA_INCLUDE_DIR` (notice the case difference from what your code is testing). The example in the docs even shows how to create an imported target for Lua.
