Skip to content

使用CMake将Lua嵌入到其他应用中

Lua

Repo: https://github.com/lua/lua

官网:https://www.lua.org/lua-l.html

版本: v5.4.4

使用CMake嵌入Lua

  • lua.cmake
    cmake
    set(LUA_LIB_SRCS 
    	"${LuaDir}/lapi.c"
    	"${LuaDir}/lcode.c"
    	"${LuaDir}/lctype.c"
    	"${LuaDir}/ldebug.c"
    	"${LuaDir}/ldo.c"
    	"${LuaDir}/ldump.c"
    	"${LuaDir}/lfunc.c"
    	"${LuaDir}/lgc.c"
    	"${LuaDir}/llex.c"
    	"${LuaDir}/lmem.c"
    	"${LuaDir}/lobject.c"
    	"${LuaDir}/lopcodes.c"
    	"${LuaDir}/lparser.c"
    	"${LuaDir}/lstate.c"
    	"${LuaDir}/lstring.c"
    	"${LuaDir}/ltable.c"
    	"${LuaDir}/ltm.c"
    	"${LuaDir}/lundump.c"
    	"${LuaDir}/lvm.c"
    	"${LuaDir}/lzio.c"
    	"${LuaDir}/lauxlib.c"
    	"${LuaDir}/lbaselib.c"
    	"${LuaDir}/lcorolib.c"
    	"${LuaDir}/ldblib.c"
    	"${LuaDir}/liolib.c"
    	"${LuaDir}/lmathlib.c"
    	"${LuaDir}/loadlib.c"
    	"${LuaDir}/loslib.c"
    	"${LuaDir}/lstrlib.c"
    	"${LuaDir}/ltablib.c"
    	"${LuaDir}/lutf8lib.c"
    	"${LuaDir}/linit.c"
    )
    
    set(LUA_LIB_HEADERS 
    	"${LuaDir}/lua.h"
    	"${LuaDir}/lualib.h"
    	"${LuaDir}/luaconf.h"
    	"${LuaDir}/lauxlib.h"
    )
    
    add_library(lua_static STATIC ${LUA_LIB_SRCS} ${LUA_LIB_HEADERS})
    set_target_properties(lua_static PROPERTIES OUTPUT_NAME "lua")
    target_include_directories(lua_static PUBLIC ${LUA_LIB_HEADERS})
    
    if(UNIX)
    	set(LUA_DEFINITIONS)
    
    	if(NOT EMSCRIPTEN)
    		find_library(LIBM m)
    		#TODO: Redo this with find_package
    		if(NOT LIBM)
    			message(FATAL_ERROR "libm not found and is required by lua")
    		endif()
    		target_link_libraries(lua_static INTERFACE ${LIBM})
    
    		list(APPEND LUA_DEFINITIONS LUA_USE_POSIX)
    		if(LUA_SUPPORT_DL)
    			target_compile_definitions(lua_static PRIVATE "LUA_USE_DLOPEN")
    			target_link_libraries(lua_static INTERFACE dl)
    		endif()
    	endif()
    
    	target_compile_definitions(lua_static 
    		PUBLIC ${LUA_DEFINITIONS}
    	)
    	target_compile_options(lua_static
    		PRIVATE "-Wall" "-Wextra"
    	)
    endif()
    
    if(LUA_BUILD_BINARY)
    	include(CheckIncludeFile)
    	CHECK_INCLUDE_FILE("readline/readline.h" HAVE_READLINE_READLINE_H)
    
    	add_executable(lua "${LuaDir}/lua.c")
    	target_link_libraries(lua PUBLIC lua_static)
    	set_target_properties(lua PROPERTIES 
    		RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}
    	)
    	if (HAVE_READLINE_READLINE_H)
    		target_compile_definitions(lua PUBLIC "LUA_USE_READLINE")
    		target_link_libraries(lua PUBLIC readline)
    	endif()
    endif()
    
    if(LUA_BUILD_COMPILER)
    	add_executable(luac "${LuaDir}/luac.c")
    	target_link_libraries(luac PUBLIC lua_static)
    	set_target_properties(luac PROPERTIES 
    		RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}
    	)
    endif()
    
    include_directories(${LuaDir})

测试

  • main.cpp
    cpp
    sol::state lua;
    int x = 0;
    lua.set_function("beep", [&x]{ ++x; printf("Welcome to use lua.\n\n\n"); });
    lua.script("beep()");
    assert(x == 1);