[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Advice request to build C functions in a library for Lua5.1
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Thu, 25 Oct 2007 15:54:57 -0400
Crystal Jewels wrote:
> I would be very grateful if anyone might advise how I can build a
> library of C functions so that I can call them from a lua script. I
> have searched and tried many different solutions but regret I cannot
> succeed.
>
> I am using Lua5.1 in Linux. I wish to create a wrapper to a library
> of C functions. I have created a trial script.
> I receive an error advisory when the 'standalone' lua script is run
> as follows:
> attempt to call global 'prin' (a nil value)
>
> My C code (mylib.c) and lua script (myrawlib.lua) is detailed below:
> Thank you very much for your time.
> C
>
> C code starts
> [...]
> C code ends
I think your C code is correct. You have to put it in a shared library
(with luaopen_mylib properly exported).
> Lua script starts
>
> #!/usr/bin/env lua
> --needs path in front of library and must explicitly state
> --filename.so
> --local p=package.loadlib("./mylib.so","prin")
> local p=package.loadlib("./mylib.so","luaopen_mylib")
> print(p)
> print "\nHello World\n"
> -- call a C function and send a number
> local indicator = prin(1)
> print("The result is ", indicator)
>
> Lua script ends
Instead of your loadlib stuff, you should use require. The
luaopen_<modname> functions (which are called loaders in the manual) are
to be used with require:
require("mylib")
local indicator = mylib.prin(1)
print("The result is ", indicator)