Files
LuaCangjia_api/lib/CMakeLists.txt

36 lines
1.2 KiB
CMake
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

cmake_minimum_required(VERSION 3.12)
set(CMAKE_CXX_STANDARD 17)
project(LuaCJAPILib)
list(APPEND EXTRA_LIBS dl)
set(ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${ROOT_PATH} ${ROOT_PATH}/lua)
add_subdirectory(lua)
# ==================== 核心修改 ====================
# 方案:使用 OBJECT 库
# 1. OBJECT 库不会生成 .a 文件,而是生成中间对象文件 (.o)。
# 2. 当 luacjapi (SHARED) 引用 luarunner (OBJECT) 时CMake 会自动开启 -fPIC 编译选项。
# 3. 这解决了 luarunner 缺少 PIC 导致的运行时崩溃问题。
add_library(luarunner OBJECT lua_runner.cpp)
# 显式声明依赖,虽然 OBJECT 库不链接,但这建立了依赖关系
target_include_directories(luarunner PRIVATE ${ROOT_PATH}/lua)
add_library(luacjapi SHARED lua_cj_api.cpp)
# 链接阶段
# 1. luarunner (OBJECT): 代码直接嵌入 luacjapi.so
# 2. lua (STATIC): 必须用 -Wl,--whole-archive 包裹。
# 原因lua_cj_api.cpp 只调用了少数 Lua 函数,如果不强制全链接,
# 链接器会丢弃 print, io 等标准库函数,导致 Lua 运行时功能缺失。
target_link_libraries(luacjapi PRIVATE
luarunner
"-Wl,--whole-archive"
lua
"-Wl,--no-whole-archive"
m
${EXTRA_LIBS}
)