2010-07-15 4 views
5

Ich versuche, das Beispiel lproc-Programm (beschrieben in Programmieren von Lua, Kapitel 30) in Lua zu laden und irgendwie zu beschmutzen. Ich folge diesem - http://www.lua.org/pil/26.2.html, um mein c-Modul in lua zu bekommen. Im Folgenden sind die Schritte, die ich getroffen habe:Laden eines C-Moduls in Lua

  1. Ich habe eine lproc.h und lproc.c (enthält genau die Funktionen gelegt in Kapitel 30 des Buches). Ich kompiliere lproc.c als --- gcc -c lproc.c -DLUA-USERCONFIG = \ "lproc.h \"

  2. Ich machte eine Bibliothek aus lproc.o, benannt das gleiche.

  3. Und dann kompiliert lua.c wie angewiesen. Meine Header-Dateien enthalten das Makro LUA_EXTRALIBS und die Methodendeklarationen.

  4. Ging zum Lua-Interpreter und es gab die folgenden Fehler:

 
> 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]: ? 

Es scheint, dass das Modul nicht, erhalten sie registriert hat, was ich von Lua tun müssen? Die Zeit ist kurz und ich mache etwas schrecklich falsch, jede Richtung wäre willkommen.

Danke,
Sayan

+0

Welche Version von Lua verwenden Sie? Die Online-PIL ist veraltet –

+0

Ich habe Lua 5.1.4 von Macports heruntergeladen. – Sayan

Antwort

1

ist die komplette und voll portable minimal Beispiel für den Aufbau einer C-Bibliothek für Lua (funktioniert in Lua 5,1-5,3 und luajit, für jede Plattform):

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

diese rockspec-Datei im selben Verzeichnis, example-1.0-1.rockspec genannt:

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

Führen Sie dann luarocks make. Es wird den C-Code mit den richtigen Flags für Ihre Plattform erstellen.

Ihr Modul ist jetzt einsatzbereit!

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