[英]Undefined symbol when calling from Lua
我有一個名為“ test.so”的庫,它使用兩個相互引用的庫中的函數。 如果我在C程序中調用test.so函數,那么它可以正常工作,因此我認為C函數中沒有錯誤。
但是,當我從Lua調用它時,由於“未定義符號”,它會引發Seg Fault錯誤。 問題是,該符號已定義,運行nm test.so時可以看到它。
閱讀下面的線程
如Rena所述,我嘗試創建一個使用dlopen加載test.so的新模塊。
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdio.h>
#include <dlfcn.h>
int luaopen_drmaa(lua_State *L){
printf("Chamou luaopen_test\n");
if( dlopen("/home/erica/Downloads/test.so", RTLD_NOW | RTLD_GLOBAL) == NULL)
printf("%s\n", dlerror());
else
printf("Chamou dlopen e teve retorno nao null\n");
return 0;
}
我編譯了它:
gcc -g drmaa_lib.c -shared -fpic -I /usr/include/lua5.1 -I /usr/local/include/ -L /usr/local/lib/m -llua5.1 -o drmaa.so
但是當我跑步時,我得到:
$ lua5.1
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> require "drmaa"
Chamou luaopen_test
Chamou dlopen e teve retorno nao null
> submitJob()
stdin:1: attempt to call global 'submitJob' (a nil value)
stack traceback:
stdin:1: in main chunk
[C]: ?
然后我嘗試在main函數中插入
luaopen_test(L);
及以上主要功能
extern int luaopen_test(lua_State *L);
實際打開庫,但出現此錯誤:
$ lua5.1
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> require "drmaa"
error loading module 'drmaa' from file './drmaa.so':
./drmaa.so: undefined symbol: luaopen_test
stack traceback:
[C]: ?
[C]: in function 'require'
stdin:1: in main chunk
[C]: ?
所引用問題的作者未顯示解決方案開發的詳細信息。 有誰知道它可能起作用的線索嗎?
提前致謝。
我顯然做錯了,要獲得實際上打開庫的函數,我應該使用ldsym函數。 drmaa lib的新代碼(加載test.so的代碼)是
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdio.h>
#include <dlfcn.h>
typedef void Register(lua_State*);
int luaopen_drmaa(lua_State *L){
void* lib = dlopen("/home/erica/Downloads/test.so", RTLD_NOW | RTLD_GLOBAL);
if(!lib)
{
printf("%s\n", dlerror());
return 1;
}
Register* loadFunc = (Register*)dlsym(lib, "luaopen_test");
if(!loadFunc)
{
printf("%s\n", dlerror());
return 1;
}
loadFunc(L);
return 0;
}
這個答案是基於該線程的一些代碼的:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.