【发布时间】: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