How to Compile a Ruby C Extension and link libcurl on Windows
Asked Answered
P

1

3

I am trying to build a Ruby C Extensions that uses libcurl. So far I have built it sucessfully on Os X. However I am much less experienced developing in Windows and am not exactly sure how to go about doing this.

So far I can compile a Ruby C Extensions using extconf.rb and nmake from the Visual Studio Command Prompt more or less by following these instructions

http://blogs.law.harvard.edu/hoanga/2006/12/14/getting-a-ruby-c-extension-to-compile-on-windows/

However my Extension links libcurl, there is a line in extconf.rb to check for this

# Make sure the cURL library is installed.
have_library("curl")

When creating the makefile, I get

checking for main() in curl.lib... no
creating Makefile

and when running nmake, I get

fatal error C1083: Cannot open include file: 'curl/curl.h': No such file or directory

This is all expected, since its not installed. I have downloaded curl-7.26.0-devel-mingw64 (I think this is appropriate for Windows 7)

I just cant figure out in the Windows environment where I should put either /bin or /include so that my compiler can find them.

Panhandle answered 14/7, 2012 at 2:1 Comment(2)
Robin, which libcurl version have you downloaded? I tried several version and could only get running with 7.21.1. For whatever reason 7.25.x didn't work for me.Sweettempered
I used 7.24.0-x86 from the link Luis provided. packages.openknapsack.org/libcurl/…Panhandle
C
10

On Windows you can have different compilers, from Visual Studio provided by Microsoft, GCC from MinGW project or Intel compiler.

RubyInstaller (which I presume is the version of Ruby you're using) uses GCC (that is why it generates files with mingw in the name)

Due the way Ruby works, you can't mix and match compilers. If Ruby was build with GCC, you need GCC to compile.

Nowdays the recommended approach has been: RubyInstaller + DevKit. DevKit is a convenience package that provides all the tools required to compile Ruby C extensions.

You can find all this plus installation instructions at RubyInstaller downloads page

Once you have Ruby and DevKit installed, you can compile most of the extensions that do not depend on external libraries.

Now, in your particular case, libcurl, RubyInstaller have provided some pre-compiled binaries that works perfectly to install curl-based tools.

libcurl packages where announced at RubyInstaller group along other packages.

You will need to download x86-windows package (link in the group page), instructions on how to extract and where to place it can be found in the same link. Please note that libcurl also needs c-ares, zlib and openssl packages.

For example, to install curb gem, after I extracted the packages I just did:

gem install curb -- --with-curl-dir=C:/Knapsack/x86-windows

Where C:/Knapsack/x86-windows is the directory I extracted all the binary packages I mentioned before. I've also added the bin directory to my PATH so libcurl DLLs can be found and used.

Note that while Windows uses \ to specify directory separator, you need to use / when providing the option to RubyGems.

Note 2: In your post you mention downloaded mingw64, which most likely are 64bits binaries. Ruby is a 32bits executable so the packages I recommended are 32bits (x86). At this time RubyInstaller does not provide 64bits (x64) binaries.

Hope this helps. If you have further questions on installing and using Ruby on Windows, please join RubyInstaller group

Coconut answered 14/7, 2012 at 13:54 Comment(4)
Thanks for your detailed answer.I followed your instructions. Installed Ruby from RubyInstaller, installed DevKit and extracted the required binaries from the link you sent me. added the directory with the binaries to my PATH, Then I tried to install curb using the command you provided (with the path to my binaries location) I also tried just downloading curl-mingw32 (extracting to c:\curl), and installing curb with this command gem install curb -- --with-curl-lib=C:\curl\bin --with-curl-include=C:\curl\include with both cases I still get extconf.rb:18: Can't find libcurl or curl/curl.hPanhandle
@Panhandle you don't need curl binary to install or use libcurl. You extracted libcurl in C:\curl? If so, what is the entire compile output? (from gem install and what are the contents of mkmf.log found inside the gem directory? Please join RubyInstaller list for further assistance. Once we figure out what is going on will update the answer.Coconut
for Ubuntu folks running into the same issue, try and install the latest version of gcc and make, and you should be fine.Gibrian
comment by @Gibrian worked perfectly, installed sudo apt install build-essentialProtozoal

© 2022 - 2024 — McMap. All rights reserved.