[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: OSX loadlib problems
- From: Mike Pall <mikelu-0502@...>
- Date: Sat, 12 Feb 2005 17:28:28 +0100
Hi,
jym@Cs.Nott.AC.UK wrote:
> loadlib("./comp1.cpp.comp","create");
> symbol `create' not found
Mac OS X libraries have an underscore prepended to every C symbol. Thus
you need to search for '_create' when you use loadlib() directly.
The old patch you are referring to fixes that internally to loadlib().
Lua 5.1-work4 leaves loadlib() alone, but fixes the underscore problem
in require().
Anyway, it is strongly suggested that you use the require() mechanism
for module loading. loadlib() is intended to be used by low-level stuff
whereas require() is part of the new module system. This way you gain
better portability and don't have to do module loading 'by hand'.
To get you started, here is a trivial module (hello.c):
#include "lua.h"
#include "lauxlib.h"
static int hello_world(lua_State *L)
{
lua_pushstring(L, "Hello world!");
return 1;
}
static luaL_reg hello_funcs[] = {
{"world", hello_world},
{NULL, NULL}
};
LUA_API int luaopen_hello(lua_State *L)
{
luaL_openlib(L, "hello", hello_funcs, 0);
return 1;
}
Compile this to "hello.so" (as a bundle library on Mac OS X and a shared
library elsewhere) and store it in the C module path of Lua (see the start
of luaconf.h, the current directory will do for testing).
Now you can use it from Lua with (example.lua):
local hello = require("hello")
print(hello.world())
You can use require() in any number of your Lua modules and it will load
the underlying C (or Lua) modules only once and pass back the same handle
to the module table. You also get an entry in the globals if you prefer
that. But it's much cleaner to place a 'local <name> = require("<name>")'
in every Lua file that depends on a specific module and let the module
mechanism load it dynamically.
BTW: Here is the same trivial module, written in Lua (hello.lua):
module(...)
function world()
return "Hello world!"
end
You have to place it in the Lua module path of course (for testing you can
use the current directory). The interesting part is that you can use the
same require() line to load it and don't have to know whether the underlying
module is a C or a Lua module.
Be careful when testing this: When two modules have the same name, the
C module takes precedence.
To rename a C module you have to change both the luaopen_* line and the
luaL_openlib line _and_ the filename.
You don't need to change anything for Lua modules when you use the
module(...) idiom (yes, that's literally three dots here) -- just rename
the file.
Bye,
Mike