2014-10-01 5 views
1

나는 서로를 참조하는 두 라이브러리의 함수를 사용하는 "test.so"라는 lib가있다. C 프로그램 내에서 test.so의 함수를 호출하면 잘 작동하므로 C 함수에 오류가 없다고 가정합니다.루아에서 호출 할 때 정의되지 않은 기호

그러나 Lua에서 호출 할 때 "정의되지 않은 기호"로 인해 Seg 오류가 발생합니다. 문제는 기호가 정의 된 것입니다. nm test.so를 실행하면 기호가 나타납니다.

Lua: C++ modules can't reference eachother, undefined symbol

아래 스레드 읽기 I는 레나하여 설명한 바와 같이, 사용 test.so dlopen을로드하는 새로운 모듈을 만들기 위해 노력했다.

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

가 그럼 난 주요 기능에 삽입 시도

luaopen_test(L); 

및 위

주요 기능

실제로 lib 디렉토리를 엽니 다,하지만 난이 오류 : 참조 된 질문

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

저자는 솔루션 개발의 세부 사항을 표시하지 않습니다. 누구나 작동하게 만들 수있는 단서가 있습니까?

미리 감사드립니다.

답변

2

분명히 잘못하고 있습니다. 실제로 라이브러리를 여는 함수를 얻으려면 ldsym 함수를 사용해야했습니다.

Lua shared object loading with C++ segfaults

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

이 답변이 스레드의 일부 코드에 기반했다입니다