Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a C Module in Lua

Tags:

lua

I am trying to load the example lproc program (described on Programming Lua, Chapter 30) into Lua and fouling up somehow. I am following this - http://www.lua.org/pil/26.2.html to get my c module into lua. Following are the steps that I've taken:

  1. I have an lproc.h and lproc.c (containing exactly the functions laid out in Chapter 30 of the book). I am compiling lproc.c as --- gcc -c lproc.c -DLUA-USERCONFIG=\"lproc.h\"

  2. I made a library out of lproc.o, named the same.

  3. And then compiled lua.c as instructed. My header files contain the macro LUA_EXTRALIBS and the method declarations.

  4. Went to the Lua interpreter and it gave the following errors:

> require "lproc"
stdin:1: module 'lproc' not found:
    no field package.preload['lproc']
    no file './lproc.lua'
    no file '/opt/local/share/lua/5.1/lproc.lua'
    no file '/opt/local/share/lua/5.1/lproc/init.lua'
    no file '/opt/local/lib/lua/5.1/lproc.lua'
    no file '/opt/local/lib/lua/5.1/lproc/init.lua'
    no file './lproc.so'
    no file '/opt/local/lib/lua/5.1/lproc.so'
    no file '/opt/local/lib/lua/5.1/loadall.so'
stack traceback:
    [C]: in function 'require'
    stdin:1: in main chunk
    [C]: ?

It seems that the module did not get registered, what would I need to do from Lua? Time is short and I am doing something horrendously wrong, any direction would be welcome.

Thanks,
Sayan

like image 449
Sayan Avatar asked Jul 15 '10 23:07

Sayan


People also ask

Does Lua have libraries?

A Lua library is a chunk that defines several Lua functions and stores them in appropriate places, typically as entries in a table. A C library for Lua mimics this behavior. Besides the definition of its C functions, it must also define a special function that corresponds to the main chunk of a Lua library.

How do I create a Lua module?

The old way of creating modules lua : module("mymodule", package. seeall) function foo() -- create it as if it's a global function print("Hello World!") end.

What is Lua H?

The header file lua. h defines the basic functions provided by Lua. That includes functions to create a new Lua environment (such as lua_open ), to invoke Lua functions (such as lua_pcall ), to read and write global variables in the Lua environment, to register new functions to be called by Lua, and so on.

How does require work in Lua?

Lua offers a higher-level function to load and run libraries, called require . Roughly, require does the same job as dofile , but with two important differences. First, require searches for the file in a path; second, require controls whether a file has already been run to avoid duplicating the work.


2 Answers

Here is a complete and fully portable minimal example of building a C library for Lua (works in Lua 5.1-5.3 and LuaJIT, for any platform):

With this example.c:

#include <lua.h>

int example_hello(lua_State* L) {
   lua_pushliteral(L, "Hello, world!");
   return 1;
}

int luaopen_example(lua_State* L) {
   lua_newtable(L);
   lua_pushcfunction(L, example_hello);
   lua_setfield(L, -2, "hello");
   return 1;
}

Put this rockspec file in the same directory, named example-1.0-1.rockspec:

package = "example"
version = "1.0-1"
source = {
   url = "." -- not online yet!
}
build = {
   type = "builtin",
   modules = {
      example = "example.c"
   }
}

Then, run luarocks make. It will build the C code with the correct flags for your platform.

Your module is now ready to use!

Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> example = require("example")
> print(example.hello())
Hello, world!
> 
like image 184
Hisham H M Avatar answered Nov 01 '22 02:11

Hisham H M


The easiest way is to create a shared library and load your C module dynamically. This way avoids having to rebuild the Lua interpreter. There are several examples in http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/ and explanations in http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/install.html and http://lua-users.org/wiki/BuildingModules

like image 35
lhf Avatar answered Nov 01 '22 01:11

lhf