Search code examples
crubyshared-librariesffi

adding shared library in ruby with ffi


I have a shared library libapi.so. I write function on C for calling function from 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;
}

Makefile:

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

So, when I call Client.send_from_web I get error:

symbol lookup error: /path/to/web_client.so: undefined symbol: start_client

But when I call this function from C, everything is fine.

How can I make ffi see 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)

Solution

  • Sorry for the noise. This was solved with the following

      ffi_lib_flags :now, :global