lua-cmake编译lua


title: lua-cmake编译lua
categories: Lua
tags: [lua, cmake]
date: 2020-09-01 15:59:03
comments: false
mathjax: true
toc: true

lua-cmake 编译 lua 可执行程序, 这里在 windows 环境下, 使用 clion 工具编译 lua 5.1

已上传到 GitHub: https://github.com/yangxuan0261/lua-5.1


前篇

  • 别人写好的 cmake 文件, 可以直接拿来用
    • lua 5.1 cmake 文件 - https://gist.github.com/squeek502/76fb065848897138a95d11f9aa0eedd4
    • lua 5.3 cmake 文件 - https://gist.github.com/squeek502/0706332efb18edd0601a4074762b0b9a

之前有写过用 vs 编译 lua lua-Visual_Studio编译Lua库.md, 必过 vs 太重了, 用 cmake 还可以跨平台.


流程

  1. 下载 lua 5.1 源码. https://www.lua.org/ftp/lua-5.1.tar.gz

  2. 解压, 用 clion 工具打开工程

  3. 创建 CMakeLists.txt 文件, 把 lua 5.1 cmake 文件 - https://gist.github.com/squeek502/76fb065848897138a95d11f9aa0eedd4 粘贴进去 (别人制作好的 cmake 文件)

    lua-cmake编译lua_第1张图片

  4. 构建, build -> build project, 显示是构建出 四个 目标文件, 在 cmake-build-debug 目录下

    ====================[ Build | all | Debug ]=====================================
    "D:\JetBrains\CLion 2019.3.3\bin\cmake\win\bin\cmake.exe" --build D:\lua-5.1\cmake-build-debug --target all -- -j 4
    [ 46%] Built target libluadll
    [ 92%] Built target liblua
    [ 95%] Built target lua
    [100%] Built target luac
    
    Build finished
    
    • 测试一下构建出的 lua 可执行程序

      lua-cmake编译lua_第2张图片


CMakeLists.txt

project(lua C)
cmake_minimum_required(VERSION 2.8)

include_directories(src ${CMAKE_CURRENT_BINARY_DIR})
set(SRC_CORE src/lapi.c src/lcode.c src/ldebug.c src/ldo.c src/ldump.c src/lfunc.c src/lgc.c src/llex.c
        src/lmem.c src/lobject.c src/lopcodes.c src/lparser.c src/lstate.c src/lstring.c src/ltable.c
        src/ltm.c src/lundump.c src/lvm.c src/lzio.c)
set(SRC_LIB src/lauxlib.c src/lbaselib.c src/ldblib.c src/liolib.c
        src/lmathlib.c src/loslib.c src/lstrlib.c src/ltablib.c src/loadlib.c src/linit.c)

set(SRC_LUA src/lua.c)
set(SRC_LUAC src/luac.c src/print.c)

add_library(liblua ${SRC_CORE} ${SRC_LIB})
set_target_properties(liblua PROPERTIES OUTPUT_NAME lua)

add_library(libluadll SHARED ${SRC_CORE} ${SRC_LIB})
target_compile_definitions(libluadll PRIVATE _CRT_SECURE_NO_WARNINGS LUA_BUILD_AS_DLL)
set_target_properties(libluadll PROPERTIES OUTPUT_NAME lua51)

add_executable(lua ${SRC_LUA})
target_link_libraries(lua libluadll)

add_executable(luac ${SRC_LUAC})
target_link_libraries(luac liblua)

你可能感兴趣的:(Lua,lua,cmake)