从C运行luajit对象文件

Mat*_*hew 5 c lua luajit

来自文档:http://luajit.org/running.html

luajit -b test.lua test.obj                 # Generate object file
# Link test.obj with your application and load it with require("test")
Run Code Online (Sandbox Code Playgroud)

但是没有解释如何做这些事情.我猜他们假设任何使用Lua的人也是C程序员,而不是我的情况!我可以得到一些帮助吗?以GCC为例.

除了C字节数组头之外,我还想做同样的事情.我也找不到相关文档.

luajit -bt h -n test test.lua test.h
Run Code Online (Sandbox Code Playgroud)

这会创建头文件,但我不知道如何从C运行它.谢谢.

小智 14

main.lua

print("Hello from main.lua")
Run Code Online (Sandbox Code Playgroud)

app.c

#include <stdio.h>

#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

int main(int argc, char **argv)
{
  int status;
  lua_State *L = luaL_newstate();
  luaL_openlibs(L);
  lua_getglobal(L, "require");
  lua_pushliteral(L, "main");
  status = lua_pcall(L, 1, 0, 0);
  if (status) {
    fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
    return 1;
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

Shell命令:

luajit -b main.lua main.o
gcc -O2 -Wall -Wl,-E -o app app.c main.o -Ixx -Lxx -lluajit-5.1 -lm -ldl
Run Code Online (Sandbox Code Playgroud)

替换-Ixx-LxxLuaJIT包含和库目录.如果你已经安装了/usr/local(默认),那么大多数GCC安装都会在没有这两个选项的情况下找到它.

第一个命令将Lua源代码编译为字节码并将其嵌入到目标文件中main.o.

第二个命令编译并链接最小的C应用程序代码.请注意,它也链接在嵌入字节码中.该-Wl,-E是强制性的(在Linux上)为所有的符号从可执行出口.

现在移动原始main.lua(以确保它真正运行嵌入的字节码而不是Lua源代码文件),然后运行您的应用程序:

mv main.lua main.lua.orig
./app
# Output: Hello from main.lua
Run Code Online (Sandbox Code Playgroud)


gre*_*olf 8

基本用法如下:

  • 使用luajit生成头文件
  • #include 源文件中将要引用其符号的标头
  • 根据您的用例,将源编译为lua的可运行可执行文件或共享二进制模块.

这是一个最小的例子来说明:

test.lua

return
{
  fooprint = function (s) return print("from foo: "..s) end,
  barprint = function (s) return print("from bar: "..s) end
}
Run Code Online (Sandbox Code Playgroud)

test.h

// luajit -b test.lua test.h
#define luaJIT_BC_test_SIZE 155
static const char luaJIT_BC_test[] = {
27,76,74,1,2,44,0,1,4,0,2,0,5,52,1,0,0,37,2,1,0,16,3,0,0,36,2,3,2,64,1,2,0,15,
102,114,111,109,32,102,111,111,58,32,10,112,114,105,110,116,44,0,1,4,0,2,0,5,
52,1,0,0,37,2,1,0,16,3,0,0,36,2,3,2,64,1,2,0,15,102,114,111,109,32,98,97,114,
58,32,10,112,114,105,110,116,58,3,0,2,0,5,0,7,51,0,1,0,49,1,0,0,58,1,2,0,49,1,
3,0,58,1,4,0,48,0,0,128,72,0,2,0,13,98,97,114,112,114,105,110,116,0,13,102,
111,111,112,114,105,110,116,1,0,0,0,0
};
Run Code Online (Sandbox Code Playgroud)

runtest.cpp

// g++ -Wall -pedantic -g runtest.cpp -o runtest.exe -llua51
#include <stdio.h>
#include <assert.h>

#include "lua.hpp"
#include "test.h"

static const char *runtest = 
"test = require 'test'\n"
"test.fooprint('it works!')\n"
"test.barprint('it works!')\n";


int main()
{
  lua_State *L = luaL_newstate();
  luaL_openlibs(L);

  lua_getglobal(L, "package");
  lua_getfield(L, -1, "preload");
  // package, preload, luaJIT_BC_test
  bool err = luaL_loadbuffer(L, luaJIT_BC_test, luaJIT_BC_test_SIZE, NULL);
  assert(!err);

  // package.preload.test = luaJIT_BC_test
  lua_setfield(L, -2, "test");

  // check that 'test' lib is now available; run the embedded test script 
  lua_settop(L, 0);
  err = luaL_dostring(L, runtest);
  assert(!err);

  lua_close(L);
}
Run Code Online (Sandbox Code Playgroud)

这非常简单.此示例获取字节代码并将其放入package.preload此程序的lua环境的表中.然后其他lua脚本可以通过这样做来使用它require 'test'.嵌入式lua源runtest正是这样做并输出:

from foo: it works!
from bar: it works!
Run Code Online (Sandbox Code Playgroud)