在 C Ruby 扩展中使用curl/libcurl
前言:我对 C 非常陌生,所以我可能遗漏了一些明显的东西,但已经运行了好几天试图弄清楚它是什么......
我正在尝试创建一个可以在 Mac 和 Mac 上运行的 Ruby C 扩展PC并使用libcurl下载文件。
基本上,该工具所做的就是从 Ruby 获取文件列表,下载文件并将它们放在 Ruby 指示的位置。
我在 Ruby 中运行了该扩展,并编译了一个 C 扩展来与 Ruby 交互。
基本上我的代码如下所示:
#include <string.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include "ruby.h"
VALUE Test = Qnil;
void Init_test();
VALUE download_file(VALUE self, VALUE from, VALUE to);
void Init_test()
{
Test = rb_define_class("Test", rb_cObject);
rb_define_method(Test, "download_file", download_file, 2);
}
VALUE download_file(VALUE self, VALUE from, VALUE to)
{
CURL *curl;
FILE *fp;
CURLcode res;
char *url = STR2CSTR(from);
char outfilename[FILENAME_MAX] = STR2CSTR(to);
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
// curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}
我遇到的问题是让 libcurl 实际工作:
$ ruby test.rb
dyld: lazy symbol binding failed: Symbol not found: _curl_easy_init
Referenced from: /path/to/test.bundle
Expected in: flat namespace
dyld: Symbol not found: _curl_easy_init
Referenced from: /path/to/test.bundle
Expected in: flat namespace
Trace/BPT trap
我正在使用 Ruby 的 mkmf
为我的扩展制作 Makefile:
require 'mkmf'
extension_name = 'test'
dir_config(extension_name)
create_makefile(extension_name)
我假设在编译扩展时, Makefile 找不到 Curl 文件,但由于我是 C/Ruby 扩展的新手,所以我不明白为什么会这样。
当我运行 curl-config --cflags
时,我得到的是:
$ curl-config --cflags
-I/usr/local/include
我的 libcurl 包含/库文件是:
/usr/local/include/curl/
/usr/local/lib/libcurl.dylib
我的设置:
- Mac OSX 10.6.4
- Ruby 1.8.6-p420
- Curl 7.21.7-DEV (i386-apple-darwin10.7.0) libcurl/7.21.7-DEV OpenSSL/0.9.8l zlib/1.2.3
任何帮助将不胜感激!
干杯
To preface: I am very new to C, so I am probably missing something obvious but have been running around for days trying to figure out what it is...
I am trying to create a Ruby C extension that will work on both Mac and PC and that uses libcurl to download files.
Basically, all the tool does is gets a list of files from Ruby, downloads the files and puts them where Ruby tells them to.
I have the extension working from within Ruby and have compiled a C extension to interface with Ruby.
Basically my code looks like this:
#include <string.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include "ruby.h"
VALUE Test = Qnil;
void Init_test();
VALUE download_file(VALUE self, VALUE from, VALUE to);
void Init_test()
{
Test = rb_define_class("Test", rb_cObject);
rb_define_method(Test, "download_file", download_file, 2);
}
VALUE download_file(VALUE self, VALUE from, VALUE to)
{
CURL *curl;
FILE *fp;
CURLcode res;
char *url = STR2CSTR(from);
char outfilename[FILENAME_MAX] = STR2CSTR(to);
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
// curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}
What I am having problems with is getting libcurl to actually work:
$ ruby test.rb
dyld: lazy symbol binding failed: Symbol not found: _curl_easy_init
Referenced from: /path/to/test.bundle
Expected in: flat namespace
dyld: Symbol not found: _curl_easy_init
Referenced from: /path/to/test.bundle
Expected in: flat namespace
Trace/BPT trap
I am using Ruby's mkmf
to make a Makefile for my extension:
require 'mkmf'
extension_name = 'test'
dir_config(extension_name)
create_makefile(extension_name)
I am assuming that when compiling the extension, the Makefile cannot find that Curl files but since I am new to C/Ruby extensions I cannot figure out why that would be.
When I run curl-config --cflags
this is what I get:
$ curl-config --cflags
-I/usr/local/include
My libcurl include/library files are:
/usr/local/include/curl/
/usr/local/lib/libcurl.dylib
My setup:
- Mac OSX 10.6.4
- Ruby 1.8.6-p420
- Curl 7.21.7-DEV (i386-apple-darwin10.7.0) libcurl/7.21.7-DEV OpenSSL/0.9.8l zlib/1.2.3
Any help would be greatly appreciated!
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在构建扩展时,您需要告诉 mkmf 链接到 libcurl。使用的命令是
have_library
。在
exconf.rb
中,在对
create_makefile
的调用之前添加。另外,我认为您不需要
dir_config(extension_name)
行。(在 Mac 上,您可以使用
otool -L
查看哪些库链接到了二进制文件。在添加
have_library
之前和之后尝试,您应该会看到类似的行>/usr/lib/libcurl.4.dylib(兼容版本6.0.0,当前版本6.1.0)
添加。)You need to tell mkmf to link to libcurl when building your extension. The command to use is
have_library
.In your
exconf.rb
, addbefore the call to
create_makefile
.Also, I don't think you need the
dir_config(extension_name)
line.(On a Mac, you can see what libraries are linked to a binary with
otool -L
. Trybefore and after adding
have_library
, you should see a line something like/usr/lib/libcurl.4.dylib (compatibility version 6.0.0, current version 6.1.0)
added.)Symbol not find: _curl_easy_init
对我来说听起来像是链接错误。看来您应该将一些标志传递给链接器(请参阅curl-config --libs
)Symbol not found: _curl_easy_init
sounds like a link error to me. It seems like you should be passing some flags to your linker (seecurl-config --libs
)