【问题标题】:adding shared library in ruby with ffi使用 ffi 在 ruby​​ 中添加共享库
【发布时间】:2018-02-15 17:51:16
【问题描述】:

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

web_client.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

Client.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

所以,当我调用 Client.send_from_web 时出现错误:

符号查找错误:/path/to/web_client.so:未定义符号:start_client

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

如何让 ffi 看到 libapi.so?

ldd /path/to/web_client.so

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)

【问题讨论】:

  • start_client 在哪里? //some code中的内容是什么
  • @TormundGiantsbane int start_client(); in client.h 在 /path/to/.so_and_.h 中,在 //some_code 我从 libapi.so 调用函数,它在 client.h 中定义

标签: c ruby shared-libraries ffi


【解决方案1】:

抱歉打扰了。这是通过以下方式解决的

  ffi_lib_flags :now, :global

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 2019-01-21
    • 2019-02-10
    相关资源
    最近更新 更多