Skip to content

Use libxmake to build binary with xmake lua apis #6138

@waruqi

Description

@waruqi

xmake provides a lot of practical module interfaces, we can use them to build a binary executable program independent of xmake, which is completely independent of xmake.

It can be executed independently without using xmake to load it.

Example

https://github.com/xmake-io/xmake/tree/dev/tests/projects/xmake_cli/hello

Create project

$ xmake create -t xmake.cli -l c test
$ create test ...
  [+]: xmake.lua
  [+]: src/lni/main.c
  [+]: src/lua/main.lua
  [+]: .gitignore
create ok!

Build project

please run xrepo update-repo to update repositories first. It will install latest libxmake package.

$ cd test
$ xmake
[ 40%]: archiving.luafiles test
[ 60%]: cache compiling.release src/lni/main.c
[ 80%]: linking.release test
[100%]: build ok, spent 1.548s
$ xmake run
hello xmake!
$ ./build/macosx/x86_64/release/test
hello xmake!

main.c

#include <xmake/xmake.h>

static tb_byte_t const g_luafiles_data[] = {
    #include "luafiles.xmz.h"
};

static tb_int_t lni_test_hello(lua_State* lua) {
    lua_pushliteral(lua, "hello xmake!");
    return 1;
}

static tb_void_t lni_initalizer(xm_engine_ref_t engine, lua_State* lua) {
    static luaL_Reg const lni_test_funcs[] = {
        {"hello", lni_test_hello}
    ,   {tb_null, tb_null}
    };
    xm_engine_register(engine, "test", lni_test_funcs);
    xm_engine_add_embedfiles(engine, g_luafiles_data, sizeof(g_luafiles_data));
}

tb_int_t main(tb_int_t argc, tb_char_t** argv) {
    tb_char_t* taskargv[] = {"lua", "-D", "lua.main", tb_null};
    return xm_engine_run("hello", argc, argv, taskargv, lni_initalizer);
}

main.lua

import("lib.lni.test")

function main ()
    print(test.hello())
end

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions