lua-users home
lua-l archive

Re: building module on OSX

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Thanks Ryan,
I did get Lua to load the code you posted. To link the lib, I had to
add -mmacosx-version-min=10.4 to the command. Now, I have to figure
out why my other project wont load libs. It has the Lua source
compiled directly into the binary, so the usual make macosx strategy
doesn't work. I need to figure out what the difference between the 2
are which is why I was asking about luaconf.h. What setting controls
whether or not nyamic libs are enabled?
best,
wes
On 1/21/07, Ryan Raaum <ryan@raaum.org> wrote:
>
> 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

AltStyle によって変換されたページ (->オリジナル) /