Robin
Robin

Reputation: 686

How to Statically Link External Libraries when Compiling a Ruby C Extension

I am building a Ruby C Extension on Windows which requires some external C libraries, specifically libcurl and its dependancies. I have the curllib dll's and .a files. However when I build with extconf.rb it always links the libraries dynamically which requires someone to have curl installed and in their windows path to use the compiled extension. Instead I want extconf.rb to link curl and its dependancies statically so that anyone can use the extension on windows without having to add curllib to their path first.

This is my extconf.rb

require 'mkmf'

# Name the extension.
extension_name = 'ConnectionManager'
dir_config("curl")

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

# Create the Makefile.
create_makefile(extension_name)

This is the command I am generating my makefile with

ruby extconf.rb --with-curl-dir=C:/Knapsack/x86-windows

Is there something that I can add to my extconf.rb file or command to force ruby to link the external libraries to my c extension statically? Any help would be appreciated and please let me know if you need any more information.

Upvotes: 3

Views: 1673

Answers (2)

Edward Ellis
Edward Ellis

Reputation: 21

I had a similar problem writing a native extension using gif_lib on linux.

Try adding the following to your extconf.rb:

unless find_library("curl", "curl_version")
  abort "curl is not installed, please install and try again"
end

The find_library function returns true if the library and entry point are present and has the side effect of adding it to the -l option to gcc.

I found these links useful: http://tenderlovemaking.com/2010/12/11/writing-ruby-c-extensions-part-2.html

Here is the C extension (a working example): https://github.com/e-g-r-ellis/ruby-giflib

Upvotes: 2

thomthom
thomthom

Reputation: 2914

When I built my Ruby C Extension using nmake from Visual Studio Express I had to change compiler flag from -MD to -MT in order to avoid dependency on MSVCRT.

I set the $CFLAGS variable in my extconf.rb file. $CFLAGS = '-MT -Ot -Ox -W4' Not sure if it's the correct way to modify these flags, but it worked.

https://bitbucket.org/thomthom/sketchup-ruby-c-extension/commits/3e87351be87f177b8ed43798587f6e798064492d

Upvotes: 1

Related Questions