>
> Hi,
Hi,
> I'm trying to build a dynamic library module on OSX that I can then
> load with require(). Lua finds the dylib, but when it tries to load I
> get the error "hello.dylib:1 unexpected symbol near ',' ". Why does
> it try to give me a line number for a compiled binary? Anyone have
> this problem before?
>
> thanks,
> wes
>
Assuming you have built Lua 5.1.1 from the standard tarball via "make
macosx", the simplest possible binary extension build process goes
something like this (modify for your directory setup). Create a file
"mylib.c" in the distribution src/ directory with:
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "math.h"
static int l_cos (lua_State *L);
static const struct luaL_reg mylib [] = {
{"cos", l_cos},
{NULL, NULL} /* sentinel */
};
static int l_cos (lua_State *L) {
double d = lua_tonumber(L, 1); /* get argument */
lua_pushnumber(L, cos(d)); /* push result */
return 1; /* number of results */
}
int luaopen_mylib (lua_State *L) {
luaL_openlib(L, "mylib", mylib, 0);
return 1;
}
then build this with:
gcc -O2 -fno-common -c -o mylib.o mylib.c
gcc -bundle -undefined dynamic_lookup -o mylib.so mylib.o
test by starting the lua interpreter and:
require("mylib")
=mylib.cos(1)
Best,
-r