Lua Module Example |
|
see also BuildingModules
// myhello.cpp -> myhello.dll (msvc6 : dll example project, select c++:codegen:"multithreaded dll" and add luabin include&lib paths)
#include <stdio.h>
extern "C" {
#include "lua.h"
#include "lauxlib.h"
}
#define PROJECT_TABLENAME "myhello"
#ifdef WIN32
#define LUA_API __declspec(dllexport)
#else
#define LUA_API
#endif
extern "C" {
int LUA_API luaopen_myhello (lua_State *L);
}
static int helloworld (lua_State *L) {
printf("hello world!\n");
return 0;
}
int LUA_API luaopen_myhello (lua_State *L) {
struct luaL_reg driver[] = {
{"helloworld", helloworld},
{NULL, NULL},
};
luaL_openlib (L, "myhello", driver, 0);
return 1;
}
/* test.lua :
require("myhello")
myhello.helloworld()
*/
LUA_INC= /usr/include/lua5.1 #~ WARN= -Wall -Wmissing-prototypes -Wmissing-declarations -ansi -pedantic WARN= INCS= -I$(LUA_INC) CFLAGS= -O2 $(WARN) $(INCS) $(DEFS) CXXFLAGS= -O2 $(WARN) $(INCS) $(DEFS) CC= g++ # OS dependent LIB_OPTION= -shared #for Linux #LIB_OPTION= -bundle -undefined dynamic_lookup #for MacOS X LIBNAME= myhello.so OBJS= myhello.o SRCS= myhello.cpp AR= ar rcu RANLIB= ranlib lib: $(LIBNAME) $(LIBNAME): $(OBJS) export MACOSX_DEPLOYMENT_TARGET="10.3"; $(CC) $(CFLAGS) -o $@ $(LIB_OPTION) $(OBJS) #~ $(COMPAT_DIR)/compat-5.1.o: $(COMPAT_DIR)/compat-5.1.c #~ $(CC) -c $(CFLAGS) -o $@ $(COMPAT_DIR)/compat-5.1.c #~ install: #~ mkdir -p $(LUA_LIBDIR)/myhello #~ cp src/$(LIBNAME) $(LUA_LIBDIR)/myhello clean: rm -f $(LIBNAME) *.o