简体   繁体   中英

How to link C library in Ruby extension

I'm building a relatively simple C extension to count pixels in a PNG file using libpng; I built the original version in C and it compiles (and works) fine in Cygwin with the command:

gcc -o shadow_counter shadow_counter.c -lpng

If I don't include the "-lpng" command it won't compile and kicks out a bunch of errors when trying to read the libpng functions ("undefined reference to `png_sig_cmp' ", etc.)

In order to package it for ruby, I put together the gemspec and files and folders and build it into a gem:

gem build shadow_png.gemspec

Then I head over to Powershell and install the gem:

gem install shadow_png-0.0.1.gem

If I leave out the libpng stuff, it installs fine and talks to Ruby the way it's supposed to. But once I start trying to reference the libpng functions and try to install it, it starts kicking out the SAME errors that I saw above (undefined reference to `png_sig_cmp' etc.). So my theory is that somehow the linking command -lpng isn't included.

How do I include these instructions as part of the process? Is it something I need to add to the gemspec? Or the command to build the gem? Or does it have to happen in the gem installation?

Thanks!

I think you're looking for the have_library method, to be placed in your extconf.rb file.

ie

abort 'Missing PNG library.' unless have_library('png')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM