# Building an extension gem and including a library source

**URL:** <https://www.ruby-forum.com/t/building-an-extension-gem-and-including-a-library-source/114034>\
**Category:** Ruby\
**Created:** [September 7, 2007, 7:56am UTC](https://www.ruby-forum.com/t/building-an-extension-gem-and-including-a-library-source/114034 "2007-09-07T07:56:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [September 7, 2007, 7:56am UTC](https://www.ruby-forum.com/t/building-an-extension-gem-and-including-a-library-source/114034/1 "2007-09-07T07:56:25Z")

</div>

I’m building an extension gem for [http://libharu.sourceforge.net/](http://libharu.sourceforge.net/) My  
intent is to help the spread of the library by uploading it to rubyforge  
(it is already uploaded and working for the windows version, check: gem  
install hpdf on win)

In LINUX, this library and the ruby extension is very easy to setup in  
the traditional way. Just untar and run:

./configure --cflags=-fPIC  
make

This makes libharu.so, wich will be needed by the ruby extension. Then

ruby extconf.rb  
make  
cp hpdf.so /var/lib/gems/hpdf/lib # \<\< Path must be correct on your  
system

That’s all. The problem comes when i try to package the gem with  
rubygems. I have no problems with the gem specification, but for the  
extconf.rb i made a horrible hack that, tough works, is very, very ugly,  
and dependes on “sh” and “sudo” to work properly, wich i don’t know if  
will always be available:

* * *

# First build the library. Horrible hack!

source\_dir= File.join(File.dirname( **FILE** ), ‘./libharu’)  
system(“cd #{source\_dir}; sudo sh ./configure --cflags=-fPIC; make”)

# From now on is ok

## require ‘mkmf’ $CPPFLAGS = $CPPFLAGS + " -I./libharu/include" $LDFLAGS = $LDFLAGS + " -L./libharu" $LIBS = $LIBS + " -lhpdf -lpng -lz" create\_makefile ‘hpdf’

How can i change the gem spec or the extconf.rb file to avoid the use of  
sh and sudo? I tought of moving all the .c and .h files to the same dir,  
but i don’t know if i will need special flags to compile the whole thing  
!

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [September 7, 2007, 3:40pm UTC](https://www.ruby-forum.com/t/building-an-extension-gem-and-including-a-library-source/114034/2 "2007-09-07T15:40:52Z")

</div>

Are you using the Gem::Specifications#extesions method? in your  
gemspec? This builds the extension when you install the gem  
automatically. You just say

spec.extensions = [“ext/[whatever]/extconf.rb”]

in your gemspec
