代码之家  ›  专栏  ›  技术社区  ›  Daniel Mur

使用ffi在ruby中添加共享库

  •  1
  • Daniel Mur  · 技术社区  · 7 年前

    我有一个共享库libapi。所以我在C上编写函数,用于从libapi调用函数。所以

    web\u客户端。c :

    #include "client.h" //h-file in libapi.so
    
    char* send_from_web(const char *command, int len){
      //some code
      return answer;
    }
    

    生成文件 :

    all: cl
    
    INC_DIR = /path/to/.so_and_.h
    LIB=-L$(INC_DIR)
    INC=-I$(INC_DIR)
    
    CC = gcc
    
    cl: web_client.c 
        $(CC) $(INC) -c -fPIC web_client.c -o web_client.o $(LIB) -lapi
        $(CC) $(INC) -shared -o web_client.so web_client.o
    
    clean:
        -rm web_client.so 2>/dev/null
    

    客户rb型 :

    require 'ffi'
    
    module Client
       extend FFI::Library
       ffi_lib 'c'
       ffi_lib '/path/to/web_client.so'
       attach_function :send_from_web, [ :strptr , :int ], :strptr 
     end
    

    所以,当我打电话给客户时。send\u from\u web I get错误:

    符号查找错误:/path/to/web\u客户端。so:未定义符号:start\u client

    但是当我从C调用这个函数时,一切都很好。

    我怎样才能让ffi看到libapi。所以

    ldd/path/to/web\U客户端。所以 :

    linux-vdso.so.1 =>  (0x00007fff70fd5000)
    libapi.so => not found
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f51e76d9000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f51e7ca5000)
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Daniel Mur    7 年前

    抱歉吵闹了。通过以下方法解决了此问题

      ffi_lib_flags :now, :global