Lua liblua5.1.so无法打开共享对象文件

发布于 2024-12-27 07:27:10 字数 1530 浏览 0 评论 0原文

所以我有一个 .so 格式的已编译 C 文件,并尝试在 Lua 中使用它。这两个文件的代码是:

-- 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 64 位上遇到此错误,但这个确切的代码在 OSX 上运行良好。

任何对此的见解都会很棒,我似乎在任何地方都找不到其他人遇到这个问题。

So I have a compiled C file in .so format, and am trying to use it from within Lua. The code for the 2 files is:

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

The C code compiles fine. But when I try to do:

cd <directory>
lua luatest.lua

I get the following error:

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

I'm not sure what's wrong as ./power.so should exist.

I'm getting this error on openSUSE 64bit, but this exact code works fine on OSX.

Any insight into this would be great, I can't seem to find anyone else with this issue anywhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的往事 2025-01-03 07:27:10

我在 lua5.1 中使用带有 -l[1] 标志的 gcc 来编译它。删除这个就可以了!

-l 库
链接时搜索名为library的库。

gcc -Wall -fPIC -shared -o <output file name> -I<path to lua include directory> <input file name>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文