Loading C module in Lua

I am trying to load the lproc example (described in the Lua Program, chapter 30) into Lua and somehow bewitch. I follow this - http://www.lua.org/pil/26.2.html to get my c module in lua. Following are the steps I took:

  • I have lproc.h and lproc.c (containing exactly the functions outlined in chapter 30 of the book). I am compiling lproc.c as --- gcc -c lproc.c -DLUA-USERCONFIG = \ "lproc.h \"

  • I created a library from lproc.o named the same.

  • And then compiled lua.c according to the instructions. My header files contain the LUA_EXTRALIBS macro and method declarations.

  • Passed to the Lua interpreter and 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 has not registered, what do I need to do from Lua? Time is short, and I'm doing something terribly wrong, any direction is welcome.

Thanks,
Sayansk

+5
source share
2 answers

C Lua ( Lua 5.1-5.3 LuaJIT ):

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;
}

rockpec example-1.0-1.rockspec:

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

luarocks make. C .

!

Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> example = require("example")
> print(example.hello())
Hello, world!
> 
+1

All Articles