作者:糖果

这篇文章归纳lua5.1的C语言模库so的“标准”写法,代码都是编译通过的,可直接参考使用!

tangguo.h

#ifndef __tangguo_h__
#define __tangguo_h__
#include "lauxlib.h"
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

extern int add(lua_State* L);
extern int sub(lua_State* L);
extern int luaopen_libtangguo(lua_State* L);
static luaL_Reg libtangguo[] = {
        {"add", add},
        {"sub", sub},
        {NULL, NULL}
};

#endif

tangguo.c

#include "tangguo.h"

int sub(lua_State* L) {
    double op1 = luaL_checknumber(L, 1);
    double op2 = luaL_checknumber(L, 2);
    lua_pushnumber(L, op1 - op2);
    return 1;
}

int add(lua_State* L) {
    double op1 = luaL_checknumber(L, 1);
    double op2 = luaL_checknumber(L, 2);
    lua_pushnumber(L, op1 + op2);
    return 1;
}


int luaopen_libtangguo(lua_State* L)
{
    luaL_openlibs(L);
    const char* libName = "libtangguo";
    luaL_register(L, libName, libtangguo);
    return 0;
}

Makefile

#LUALIB=-I/usr/include/lua5.1 -L/usr/local/lib -llua -ldl -lm
LUALIB=-I/usr/include/lua5.1 -L/usr/local/lib -ldl -lm


.PHONY: all win linux

all:
        @echo Please do \'make PLATFORM\' where PLATFORM is one of these:
        @echo win linux

win:

linux: libtangguo.so

libtangguo.so : tangguo.c
        #gcc --shared -Wall -fPIC -O2 $^ -o$@
        gcc --shared -Wall -fPIC -O2 $^ -o$@

clean:
        rm -f libtangguo.so

2016.6.7日的时候,重新看了一下这篇文章,发现gcc的一个有开关没加,lib库又多加了一个。追加了$(LUALIB), 去掉了 -llua 编译选项。

/*
** Try to find a load function for module 'modname' at file 'filename'.
** First, change '.' to '_' in 'modname'; then, if 'modname' has
** the form X-Y (that is, it has an "ignore mark"), build a function
** name "luaopen_X" and look for it. (For compatibility, if that
** fails, it also tries "luaopen_Y".) If there is no ignore mark,
** look for a function named "luaopen_modname".
*/
static int loadfunc (lua_State *L, const char *filename, const char *modname) {
  const char *openfunc;
  const char *mark;
  modname = luaL_gsub(L, modname, ".", LUA_OFSEP);
  mark = strchr(modname, *LUA_IGMARK);
  if (mark) {
    int stat;
    openfunc = lua_pushlstring(L, modname, mark - modname);
    openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc);
    stat = lookforfunc(L, filename, openfunc);
    if (stat != ERRFUNC) return stat;
    modname = mark + 1;  /* else go ahead and try old-style name */
  }
  openfunc = lua_pushfstring(L, LUA_POF"%s", modname);
  return lookforfunc(L, filename, openfunc);
}

static int ll_loadlib (lua_State *L) {
  const char *path = luaL_checkstring(L, 1);
  const char *init = luaL_checkstring(L, 2);
  int stat = lookforfunc(L, path, init);
  if (stat == 0)  /* no errors? */
    return 1;  /* return the loaded function */
  else {  /* error; error message is on stack top */
    lua_pushnil(L);
    lua_insert(L, -2);
    lua_pushstring(L, (stat == ERRLIB) ?  LIB_FAIL : "init");
    return 3;  /* return nil, error message, and where */
  }
}

https://github.com/lua/lua/blob/e354c6355e7f48e087678ec49e340ca0696725b1/loadlib.c

作者:糖果
PS:转载到其它平台请注明作者姓名及原文链接,请勿用于商业用途。

点击查看Lua FAQ