Search code examples
crubycurllibcurlruby-c-extension

Using curl/libcurl in a C Ruby extension


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


Solution

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