Error finding opencv shared libraries with Rice (Ruby) on Ubuntu 12.04-Collection of common programming errors

I’m trying to correctly link OpenCV shared libraries to a ruby c++ extension and for some reason any call to have_library() is failing for the opencv libraries. I installed opencv to /usr/local so the header files are all in /usr/local/include and the libraries are all in /usr/local/lib. I’ll attach my extconf.rb and related output below.

require "mkmf-rice"

dir_config("opencv_core", "/usr/local/include", "/usr/local/lib")
unless have_library("opencv_core")
    raise "Could not find opencv_core"
end

dir_config("opencv_flann", "/usr/local/include", "/usr/local/lib")
unless have_library("opencv_flann")
    raise "Could not find opencv_flann"
end

dir_config("opencv_highgui", "/usr/local/include", "/usr/local/lib")
unless have_library("opencv_highgui")
    raise "Could not find opencv_highgui"
end

dir_config("opencv_features2d", "/usr/local/include", "/usr/local/lib")
unless have_library("opencv_features2d")
    raise "Could not find opencv_features2d"
end

dir_config("opencv_nonfree", "/usr/local/include", "/usr/local/lib")
unless have_library("opencv_nonfree")
    raise "Could not find opencv_nonfree"
end

dir_config("opencv_objdetect", "/usr/local/include", "/usr/local/lib")
unless have_library("opencv_objdetect")
    raise "Could not find opencv_objdetect"
end

create_makefile("something")

Running $ ruby extconf.rb outputs the following error indicating that it cannot find the shared library for opencv_core. I also tried each other library on its own and they all failed as well.

checking for main() in -lopencv_core... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/home/alec/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
    --with-libpath
    --without-libpath
    --with-opencv_core-dir
    --without-opencv_core-dir
    --with-opencv_core-include
    --without-opencv_core-include=${opencv_core-dir}/include
    --with-opencv_core-lib
    --without-opencv_core-lib=${opencv_core-dir}/
    --with-opencv_corelib
    --without-opencv_corelib
extconf.rb:7:in `': Could not find opencv_core (RuntimeError)

And mkmf.log contains the following at the beginning:

home/alec/.rvm/gems/ruby-2.0.0-p247/gems/rice-1.5.1/ruby/lib/mkmf-rice.rb:136:in `link_command': Use RbConfig instead of obsolete and deprecated Config.
have_library: checking for main() in -lopencv_core... -------------------- no

/home/alec/.rvm/gems/ruby-2.0.0-p247/gems/rice-1.5.1/ruby/lib/mkmf-rice.rb:136:in `link_command': Use RbConfig instead of obsolete and deprecated Config.
/home/alec/.rvm/gems/ruby-2.0.0-p247/gems/rice-1.5.1/ruby/lib/mkmf-rice.rb:136:in `link_command': Use RbConfig instead of obsolete and deprecated Config.
/home/alec/.rvm/gems/ruby-2.0.0-p247/gems/rice-1.5.1/ruby/lib/mkmf-rice.rb:136:in `link_command': Use RbConfig instead of obsolete and deprecated Config.
"g++ -o conftest -I/home/alec/.rvm/rubies/ruby-2.0.0-p247/include/ruby-2.0.0/x86_64-linux -I/home/alec/.rvm/rubies/ruby-2.0.0-p247/include/ruby-2.0.0/ruby/backward -I/home/alec/.rvm/rubies/ruby-2.0.0-p247/include/ruby-2.0.0 -I. -I/usr/local/include     -I/home/alec/.rvm/gems/ruby-2.0.0-p247/gems/rice-1.5.1/ruby/lib/include  -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration conftest.c  -L. -L/home/alec/.rvm/rubies/ruby-2.0.0-p247/lib -Wl,-R/home/alec/.rvm/rubies/ruby-2.0.0-p247/lib -L/usr/local/lib -Wl,-R/usr/local/lib -L. -fstack-protector -rdynamic -Wl,-export-dynamic  -L/home/alec/.rvm/gems/ruby-2.0.0-p247/gems/rice-1.5.1/ruby/lib/lib -lrice     -Wl,-R -Wl,/home/alec/.rvm/rubies/ruby-2.0.0-p247/lib -L/home/alec/.rvm/rubies/ruby-2.0.0-p247/lib -lruby  -lpthread -lrt -ldl -lcrypt -lm   -lc"
cc1plus: warning: command line option ‘-Wdeclaration-after-statement’ is valid for C/ObjC but not for C++ [enabled by default]
cc1plus: warning: command line option ‘-Wimplicit-function-declaration’ is valid for C/ObjC but not for C++ [enabled by default]
checked program was:

The g++ command seems to show that it’s correctly looking for the headers in /usr/local/include and that it’s also correctly looking for the libraries in /usr/local/lib from the flags:

-I/usr/local/include
-L/usr/local/lib

but for some reason it can’t seem to find any of the libraries.

The really odd part is that two days ago I built everything (with the same version of opencv) on a different ubuntu instance (also 12.04) and everything worked fine.

I looked around on stackoverflow for a while and found this, which seems to be a similar problem except he’s on NetBSD and I’m running Ubuntu.

How to get Ruby to find a native lib?

Unfortunately it looks like nobody has confirmed a solution to his issue yet though either.

Any help would be greatly appreciated, thanks.

EDIT:

Hmm, I was using ruby version 2.0.0-p247 and I just switched back to 1.9.3-p362 and now everything works fine.

EDIT 2:

Solved it, sort of. I had to rebuild ruby 1.9.3-p362 with the configuration option –enable-shared set. Via rvm this would be:

$ rvm install 1.9.3-p362 -C --enable-shared