You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Conan包管理器时sol2查找失败,Lua无法正常链接

CMake无法找到sol2包配置文件问题及解决

原配置文件

CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(game)

set(SOURCES 
    ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
)


# Conan Requirements
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

add_executable(game ${SOURCES})

find_package(SDL2 REQUIRED)
find_package(sol2 REQUIRED)

target_compile_options( game PRIVATE 
    $<$<CONFIG:Debug>:
        -fsanitize=address,leak
    >
)

target_link_options( game PRIVATE 
    $<$<CONFIG:Debug>:
        -fsanitize=address,leak
    >
)

target_compile_features(game
    PRIVATE
        cxx_std_20
)

target_link_libraries(game PRIVATE 
    SDL2::SDL2
    sol2::sol
)

conanfile.txt

[requires]
lua/5.4.4
sdl/2.0.20

[generators]
cmake
cmake_find_package
CMakeDeps
CMakeToolchain

构建命令

conan install . --install-folder build --build=missing
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=build/conan_toolchain.cmake -S . -B build -GNinja
cmake --build build

报错信息

CMake Error at CMakeLists.txt:16 (find_package):
  By not providing "Findsol2.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "sol2", but
  CMake did not find one.

  Could not find a package configuration file provided by "sol2" with any of
  the following names:

    sol2Config.cmake
    sol2-config.cmake

  Add the installation prefix of "sol2" to CMAKE_PREFIX_PATH or set
  "sol2_DIR" to a directory containing one of the above files.  If "sol2"
  provides a separate development package or SDK, be sure it has been
  installed.

注:SDL2可正常识别,仅sol2出现上述报错。


解决方案

修改后的CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(game)

set(SOURCES 
    ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
)


# Conan Requirements
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

add_executable(game ${SOURCES})

find_package(SDL2 REQUIRED)
find_package(sol2 REQUIRED)

target_compile_options( game PRIVATE 
    $<$<CONFIG:Debug>:
        -fsanitize=address,leak
    >
)

target_link_options( game PRIVATE 
    $<$<CONFIG:Debug>:
        -fsanitize=address,leak
    >
)

target_compile_features(game
    PRIVATE
        cxx_std_20
)

target_link_libraries(game PRIVATE 
    SDL2::SDL2
    sol2::sol2
)

修改点:将target_link_libraries中的sol2::sol改为sol2::sol2,匹配sol2包导出的正确目标名称。

修改后的conanfile.txt

[requires]
sol2/3.3.0
sdl/2.0.20

[generators]
cmake
cmake_find_package
CMakeDeps
CMakeToolchain

修改点:添加sol2/3.3.0到依赖列表,移除原有的lua/5.4.4(sol2会自动关联对应版本的lua,无需单独声明)。

修改后的构建脚本

#!/usr/bin/sh

conan install . --install-folder build --build=missing
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=build/conan_toolchain.cmake -S . -B build -GNinja

内容的提问来源于stack exchange,提问作者Johnny Cash Cow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 19:24:21