Giter Site home page Giter Site logo

lua-compiled's Introduction

lua-compiled

This example demonstrates how to create executable that runs embedded Lua bytecode

Why

Sometimes there is a need to decouple the application core from some logic that depends on external factors. For example, you need to generate a segment of application that works on that particular machine only, which is often used, for example, in software licensing.

In the current codebase, we demonstrate how to compile the Lua app into bytecode, wrap that bytecode as something we can embed in C app, and then compile that app into executable, which is more, rather, a loader for our bytecode.

But we could go even further; instead of having that bytecode compiled all together with our app, we could have it somewhere on disk or, even better, remotely.

Build tools

To build it you need Xmake, A cross-platform build utility based on Lua, you can grab it from here https://xmake.io

Xmake is super easy to use and allows to make insane build scenarios impossible to achieve in Xmake and others.

Configuring Xmake.lua

Basically the following config is all you need:

set_languages('cxx14')

target('demo')
set_kind('binary')

add_links('lua')
add_linkdirs("/usr/local/Cellar/lua/5.4.3/lib")

--add lua headers
add_includedirs(os.getenv('LUA_HEADERS'))

add_files('$(projectdir)/src/main.cpp')

before_build(function(target)
    -- do something here before building it
end)

after_build(function(target)
    print("hello: %s", target:name())
    os.exec("echo %s", target:targetfile())
end)

Notice add_linkdirs, that's where you need to provide a path to your libluathat will me linked with your app.

LUA_HEADERS that was set as environment variable points to Lua headers required for your app.

Working with Clion

If you are coding in Clion, you might need to have CmakeLists.txt as well, just to allow CLion to 'see' your Lua headers, for this just add minimal Cmake config:

cmake_minimum_required(VERSION 3.5)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(lua_hello CXX)

message("Including Lua headers from $ENV{LUA_HEADERS}")
include_directories($ENV{LUA_HEADERS})

add_executable(${PROJECT_NAME} ${SOURCE_FILES})

lua-compiled's People

Contributors

skitsanos avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.