Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Ruby to find a native lib?

Tags:

ruby

rubygems

I installed some native libraries in /usr/local/lib.

I am now trying to install a gem which needs these in order to build correctly but, the gem build fails as it cannot find the libraries.

The gem's extconf.rb file tries to confirm it can find the library with have_library() but this fails for some reason.

I tried setting a bunch of environment variables but nothing seems to work:

irb(main):003:0> require 'mkmf'
=> true
irb(main):004:0> have_library('gecodesearch')
checking for main() in -lgecodesearch... no
=> false
irb(main):005:0> ENV['LD_LIBRARY_PATH']='/usr/local/lib'
=> "/usr/local/lib"
irb(main):006:0> have_library('gecodesearch')
checking for main() in -lgecodesearch... no
=> false
irb(main):007:0> ENV['DYLD_LIBRARY_PATH']='/usr/local/lib'
=> "/usr/local/lib"
irb(main):008:0> have_library('gecodesearch')
checking for main() in -lgecodesearch... no
=> false
irb(main):009:0> have_library('libgecodesearch')
checking for main() in -llibgecodesearch... no
=> false
irb(main):010:0> ENV['C_INCLUDE_PATH']='/usr/local/lib'
=> "/usr/local/lib"
irb(main):011:0> have_library('gecodesearch')
checking for main() in -lgecodesearch... no
=> false
irb(main):012:0> ENV['PATH']='/usr/local/lib'
=> "/usr/local/lib"
irb(main):013:0> have_library('gecodesearch')
checking for main() in -lgecodesearch... no
=> false 

What is the best way to solve this problem?

like image 962
Adam Russell Avatar asked Feb 16 '12 16:02

Adam Russell


1 Answers

I've read the comments and I know you made it work, but I think I have the proper solution to the problem.

have_library checks whether it is possible to use the given library in your environment. It does so by including the library header and using one of its functions in a temporary C source file. If it is successful, then the library must be available.

have_library 'geocodesearch'
checking for main() in -lgecodesearch... no

have_library could not use the mainfunction from geocodesearch. This either means the library is not available or the function doesn't exist. In your case, it is probably the latter.

You can tell have_library which function to try by passing a second argument. For example:

have_library 'geocodesearch', 'geocodesearch_version'
checking for geocodesearch_version() in -lgecodesearch...

If you don't specify, it will simply look for the main function. You can also specify the headers to be included:

have_library 'geocodesearch', 'geocodesearch_version', %w(geocode/search.h)

In the comments, you said you solved your problem by simply eliminating the have_library calls. This is a localized solution; you will have to reapply it to any new versions of the gem.

I recommend sending a pull request to the author containing the necessary adjustments. The bug will be permanently fixed and you will also help others who might be having the same issue.

mkmf reference:

  • Source code
  • Documentation
like image 102
Matheus Moreira Avatar answered Sep 28 '22 00:09

Matheus Moreira