building module on OSX

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

building module on OSX

Wesley Smith
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

Reply | Threaded
Open this post in threaded view
|

Re: building module on OSX

Wesley Smith
Sorry for all the noise,
I'm now getting the error "dynamic libraries not enabled, check you
lua installation".  I've searched the archive in this and there are
some hints to look in luaconf.h, but nothing explicit.  What exactly
in luaconf.h do I have to enable to allow for the loading of dyamic
libraries.  On a hunch, I tried

//#undef LUA_COMPAT_LOADLIB

commenting out the above line, but this didn't help.  Any ideas?

thanks,
wes

Reply | Threaded
Open this post in threaded view
|

Re: building module on OSX

Ryan Raaum
In reply to this post by Wesley Smith


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
Reply | Threaded
Open this post in threaded view
|

Re: building module on OSX

Wesley Smith
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 <[hidden email]> 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



Reply | Threaded
Open this post in threaded view
|

Re: building module on OSX

Wesley Smith
I finally realized that the answer was not in lua conf, but in adding
-DLUA_USE_MACOSX to my "other C flags setting" (I'm doing the
compilation in Xcode).  When I tried to load a lib, I get this crash
log:

Link (dyld) error:

Symbol not found: _luaL_openlib
 Referenced from: /usr/local/lib/lua/5.1/mylib.so
 Expected in: dynamic lookup

Doing nm on the lib, I see

00000eb0 t __dyld_func_lookup
00000000 t __mh_bundle_header
        U _cos
00000ed0 t _l_cos
        U _luaL_openlib
        U _lua_pushnumber
        U _lua_tonumber
00000f10 T _luaopen_mylib
0000100c s _mylib
00001000 d dyld__mh_bundle_header
00001008 s dyld_func_lookup_pointer
00001004 s dyld_lazy_symbol_binding_entry_point
00000e80 t dyld_stub_binding_helper

The Lua library symbols are U (undefined) which I'd expect because the
module was compiled dynamically against liblua.a.  To give some
context, I'm building a Lua plugin to a software environment.  The
plugin has the Lua source compiled directly into it, so it contains
all of the symbols like "0001428c T _luaL_openlib".  Is there a way to
direct the lookup of this symbol into the binary of my external as
opposed to dyld crash because it can't find it in the mylib.so file?

thanks,
wes

Reply | Threaded
Open this post in threaded view
|

Re: building module on OSX

Gé Weijers
Make sure that the main program (/usr/local/bin/lua?) has not been stripped of its global symbols, they're need to link the dynamic library into the interpreter. Use 'strip -x' if you want to reduce the size of the lua binary. Do not use 'install -s' or 'strip' without flags.

I've appended a Makefile I use to build a dynamic library. The magic incantations are stolen from luasocket Makefiles.

Hope this helps.



CFLAGS=-pedantic -fno-common -O2 -Wall
LDFLAGS=-bundle -undefined dynamic_lookup
CC=gcc
LD=export MACOSX_DEPLOYMENT_TARGET="10.4"; gcc

tlv.so: tlv.o
        $(LD) $(LDFLAGS) $< -o $@

tlv.o:  tlv.c

%.o:    %.c
        $(CC) $(CFLAGS) -c $< -o $@

On Jan 21, 2007, at 3:13 PM, Wesley Smith wrote:

I finally realized that the answer was not in lua conf, but in adding
-DLUA_USE_MACOSX to my "other C flags setting" (I'm doing the
compilation in Xcode).  When I tried to load a lib, I get this crash
log:

Link (dyld) error:

Symbol not found: _luaL_openlib
 Referenced from: /usr/local/lib/lua/5.1/mylib.so
 Expected in: dynamic lookup

Doing nm on the lib, I see

00000eb0 t __dyld_func_lookup
00000000 t __mh_bundle_header
        U _cos
00000ed0 t _l_cos
        U _luaL_openlib
        U _lua_pushnumber
        U _lua_tonumber
00000f10 T _luaopen_mylib
0000100c s _mylib
00001000 d dyld__mh_bundle_header
00001008 s dyld_func_lookup_pointer
00001004 s dyld_lazy_symbol_binding_entry_point
00000e80 t dyld_stub_binding_helper

The Lua library symbols are U (undefined) which I'd expect because the
module was compiled dynamically against liblua.a.  To give some
context, I'm building a Lua plugin to a software environment.  The
plugin has the Lua source compiled directly into it, so it contains
all of the symbols like "0001428c T _luaL_openlib".  Is there a way to
direct the lookup of this symbol into the binary of my external as
opposed to dyld crash because it can't find it in the mylib.so file?

thanks,
wes



--
Gé Weijers



Reply | Threaded
Open this post in threaded view
|

Re: building module on OSX

Wesley Smith
Hi Gé,

Thanks for the info.  These 2 switches, -bundle and -undefined
dynamic_lookup, are exactly the ones that I used to compile the module
without the Lua symbols.

Make sure that the main program (/usr/local/bin/lua?) has not been stripped
of its global symbols, they're need to link the dynamic library into the
interpreter. Use 'strip -x' if you want to reduce the size of the lua
binary. Do not use 'install -s' or 'strip' without flags.

In doing this, I'm not using the lua binary.  I have a completely
different situation where I'm compiling a plugin with Lua in it within
a greater software environment.  I believe the symbols are global in
my plugin within which Lua is embedded.  Here's one of the lua symbols

0001428c T _luaL_openlib

T means text section symbol and that it's external as well.  I'm not
sure how to get dyld to recognize that these symbols are already in
memory when it tries to load the module.

wes


Reply | Threaded
Open this post in threaded view
|

Re: building module on OSX

Gé Weijers
The only thing I can think of it that you could sidestep the issue and put the basic lua interpreter in a library as well. Sorry I can't be of more help.

One thing you could try is to use "-bundle_loader <your-plugin>" in stead of "-undefined dynamic_lookup". I don't know if it would change anything.


On Jan 21, 2007, at 8:00 PM, Wesley Smith wrote:

Hi Gé,

Thanks for the info.  These 2 switches, -bundle and -undefined
dynamic_lookup, are exactly the ones that I used to compile the module
without the Lua symbols.

Make sure that the main program (/usr/local/bin/lua?) has not been stripped
of its global symbols, they're need to link the dynamic library into the
interpreter. Use 'strip -x' if you want to reduce the size of the lua
binary. Do not use 'install -s' or 'strip' without flags.

In doing this, I'm not using the lua binary.  I have a completely
different situation where I'm compiling a plugin with Lua in it within
a greater software environment.  I believe the symbols are global in
my plugin within which Lua is embedded.  Here's one of the lua symbols

0001428c T _luaL_openlib

T means text section symbol and that it's external as well.  I'm not
sure how to get dyld to recognize that these symbols are already in
memory when it tries to load the module.

wes



--
Gé Weijers