在 C Ruby 扩展中使用curl/libcurl

发布于 2024-11-13 23:01:23 字数 2226 浏览 3 评论 0原文

前言:我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪荡不羁 2024-11-20 23:01:23

在构建扩展时,您需要告诉 mkmf 链接到 libcurl。使用的命令是 have_library

exconf.rb 中,

have_library("curl", "curl_easy_init")

在对 create_makefile 的调用之前添加。

另外,我认为您不需要 dir_config(extension_name) 行。

(在 Mac 上,您可以使用 otool -L 查看哪些库链接到了二进制文件。

otool -L test.bundle

在添加 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, add

have_library("curl", "curl_easy_init")

before 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. Try

otool -L test.bundle

before 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.)

盛夏已如深秋| 2024-11-20 23:01:23

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 (see curl-config --libs)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文