2015-06-29 23 views
1

我編譯使用C。所以C模塊中的「本地名=需要 「力量」 錯誤

.so文件製備的樣品 「USR/local/lib目錄/ LUA/5.2」 我把目錄「power.so」

,但這樣的工作類型

require("power") 
print(square(1.414213598)) 
print(cube(5)) 

從而使錯誤

local power = require "power" 

a = power.square(1.414213598) 
b = power.cube(5) 

print(a) 
print(b) 

錯誤的類型

lua: /Users/batuhangoksu/Desktop/test.lua:3: attempt to index a boolean value (local 'power') 
stack traceback: 
    /Users/batuhangoksu/Desktop/test.lua:3: in main chunk 
    [C]: in ? 

C示例代碼

// hellofunc.c (C) 2011 by Steve Litt 
// * gcc -Wall -shared -fPIC -o power.so -I/usr/include/lua5.1 -llua5.1 hellofunc.c 
// * Note the word "power" matches the string after the underscore in 
// * function luaopen_power(). This is a must. 
// * The -shared arg lets it compile to .so format. 
// * The -fPIC is for certain situations and harmless in others. 
// * On your computer, the -I and -l args will probably be different. 
// * gcc -Wall -shared -fPIC -o power.so -I/usr/include -llua test.c 

#include <lua.h> 
#include <lauxlib.h> 
#include <lualib.h> 

static int isquare(lua_State *L){    /* Internal name of func */ 
    float rtrn = lua_tonumber(L, -1);  /* Get the single number arg */ 
    printf("Top of square(), nbr=%f\n",rtrn); 
    lua_pushnumber(L,rtrn*rtrn);   /* Push the return */ 
    return 1;        /* One return value */ 
} 

static int icube(lua_State *L){    /* Internal name of func */ 
    float rtrn = lua_tonumber(L, -1);  /* Get the single number arg */ 
    printf("Top of cube(), number=%f\n",rtrn); 
    lua_pushnumber(L,rtrn*rtrn*rtrn);  /* Push the return */ 
    return 1;        /* One return value */ 
} 

// Register this file's functions with the 
// * luaopen_libraryname() function, where libraryname 
// * is the name of the compiled .so output. In other words 
// * it's the filename (but not extension) after the -o 
// * in the cc command. 
// * 
// * So for instance, if your cc command has -o power.so then 
// * this function would be called luaopen_power(). 
// * 
// * This function should contain lua_register() commands for 
// * each function you want available from Lua. 
// * 

int luaopen_power(lua_State *L){ 
    lua_register(L, "square", isquare); 
    lua_register(L, "cube", icube); 
    return 0; 
} 

當我犯錯,我如果你能幫我

回答

0

通常的協議是圖書館應該在一個表中返回它們的功能很高興。你的庫設置全局函數,這是正常的,但使power = require "power"設置powertrue,這是什麼require當庫不返回任何東西。

+0

謝謝, 我編譯使用MongoDB「.so」擴展名「local mongo = require('mongo')」可以使用表格。它的「.so」擴展我的「本地權力=需要」權力「不能使用這種形式爲什麼?不是解決方案 – batuhangoksu

相關問題