Lua liblua5.1.so无法打开共享对象文件
问题描述:
因此,我有一个.so格式的编译C文件,并试图在Lua中使用它。这两个文件的代码是:Lua liblua5.1.so无法打开共享对象文件
-- luatest.lua:
require("power")
print("Enter a number: ")
local num = tonumber(io.read())
local n = create(num)
square(n)
cube(n)
nprint(n)
// luatest.c compiled to power.so
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdlib.h>
static int createStruct(lua_State *L);
static int isquare(lua_State *L);
static int icube(lua_State *L);
static int nprint(lua_State *L);
typedef struct numbers {
float number;
float square;
float cube;
} numbers;
int luaopen_power(lua_State *L){
lua_register(L, "create", createStruct);
lua_register(L, "square", isquare);
lua_register(L,"cube",icube);
lua_register(L, "nprint", nprint);
return 0;
}
static int createStruct(lua_State *L){
// Code here
}
static int isquare(lua_State *L){
// Code here
}
static int icube(lua_State *L){
// Code here
}
static int nprint(lua_State *L){
// Code here
}
C代码编译得很好。但是,当我尝试做:
cd <directory>
lua luatest.lua
我得到以下错误:
lua: error loading module 'power' from file './power.so':
liblua5.1.so: cannot open shared object file: No such file or directory
stack traceback:
[C]: ?
[C]: in function 'require'
luatest.lua:3: in main chunk
[C]: ?
我不知道作为./power.so应该存在什么是错的。
我在openSUSE 64bit上遇到这个错误,但是这个确切的代码在OSX上正常工作。
任何对此的见解都会很棒,我似乎无法在任何地方找到其他人。
答
I was compiling it using gcc with the -l[1] flag at lua5.1. Remove this and it will work!
-l library Search the library named library when linking.
gcc -Wall -fPIC -shared -o <output file name> -I<path to lua include directory> <input file name>
对于这个是有用的,你应该添加删除为什么'-llua5.1'解决您的问题。否则它就没有任何意义 - 有人会怀疑,你正在编译C lua模块,为什么你不想链接到lua库? – greatwolf 2012-01-16 05:17:10
您应该将解决方案作为答案发布并接受它。它会将问题标记为已回答,并且可以让人们更轻松地解决相同的问题以找到解决方案。 – jpjacobs 2012-01-16 07:55:49