简体   繁体   English

从Lua呼叫时未定义符号

[英]Undefined symbol when calling from Lua

I have a lib, called "test.so" that uses functions from two libraries that reference each other. 我有一个名为“ test.so”的库,它使用两个相互引用的库中的函数。 If I call the functions of test.so within a C program, it works just fine, so I assume there's no error in C functions. 如果我在C程序中调用test.so函数,那么它可以正常工作,因此我认为C函数中没有错误。

However, when I call it from Lua, it throws a Seg Fault error due to an "undefined symbol". 但是,当我从Lua调用它时,由于“未定义符号”,它会引发Seg Fault错误。 The problem is, the symbol is defined, I can see it when I run nm test.so. 问题是,该符号已定义,运行nm test.so时可以看到它。

Reading the thread below 阅读下面的线程

Lua: C++ modules can't reference eachother, undefined symbol Lua:C ++模块无法互相引用未定义的符号

I tried to creating a new module that would load test.so using dlopen, as described by Rena. 如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;
}

I compiled it: 我编译了它:

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

But when I run, I get: 但是当我跑步时,我得到:

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

Then I tried inserting in main function 然后我尝试在main函数中插入

luaopen_test(L);  

and above the main function 及以上主要功能

extern int luaopen_test(lua_State *L);

to actually open the lib, but I get this error: 实际打开库,但出现此错误:

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

The author of the referenced question doesn't show details of the development of the solution. 所引用问题的作者未显示解决方案开发的详细信息。 Does anyone has a clue of what may make it work? 有谁知道它可能起作用的线索吗?

Thanks in advance. 提前致谢。

I was obviously doing it wrong, to get the function that actually opens the library I should have used ldsym function. 我显然做错了,要获得实际上打开库的函数,我应该使用ldsym函数。 The new code for drmaa lib (the one that loads test.so) is 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;
}

This answer was base in some code of this thread: 这个答案是基于该线程的一些代码的:

Lua shared object loading with C++ segfaults Lua使用C ++ segfaults共享对象加载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM