# Mkmf: how to include .c and .h files within a subdirectory?

**URL:** <https://www.ruby-forum.com/t/mkmf-how-to-include-c-and-h-files-within-a-subdirectory/219540>\
**Category:** Ruby\
**Created:** [May 7, 2012, 12:58am UTC](https://www.ruby-forum.com/t/mkmf-how-to-include-c-and-h-files-within-a-subdirectory/219540 "2012-05-07T00:58:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 7, 2012, 12:58am UTC](https://www.ruby-forum.com/t/mkmf-how-to-include-c-and-h-files-within-a-subdirectory/219540/1 "2012-05-07T00:58:56Z")

</div>

Hi, having the following file tree under my ext/ directory:

my\_ext/  
extconf.rb  
my\_ext.c  
utils/  
utils.c  
utils.h

How to say in extconf.rb that files under utils/ subdirectory must  
also be compiled?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/u/SSISSaki_Baz_C.SS_S)\
**Post date:** [May 7, 2012, 11:33am UTC](https://www.ruby-forum.com/t/mkmf-how-to-include-c-and-h-files-within-a-subdirectory/219540/2 "2012-05-07T11:33:05Z")

</div>

Am 07.05.2012 00:58, schrieb Iñaki Baz C.:

> also be compiled?
> 
> Thanks a lot.

I’ve once had the same problem and asked here on Ruby T., but got no  
satisfying answer. It is not possible without hacking mkmf, I finally  
ended up putting everything in one directory. #create\_makefile  
([Class: Object (Ruby 1.9.3)](http://ruby-doc.org/stdlib-1.9.3/libdoc/mkmf/rdoc/Object.html#method-i-create_makefile))  
has an option for specifying _one_ subdirectory where all files reside,  
but for multiple subdirectories you’d have to create multiple C  
extensions.

If someone knows another way to accomplish the task, I’d be intererested  
in it, because large C extensions are easier organised by use of  
multiple directories.

Vale,  
Marvin

---

<div class="post-metadata">

**Author:** ![SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/u/SSISSaki_Baz_C.SS_S)\
**Post date:** [May 7, 2012, 2:45pm UTC](https://www.ruby-forum.com/t/mkmf-how-to-include-c-and-h-files-within-a-subdirectory/219540/3 "2012-05-07T14:45:19Z")

</div>

On Mon, May 7, 2012 at 7:58 AM, Iaki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> also be compiled?  
> Though, IMO, there is no official way, here is a hack.

* * *

require ‘mkmf’

# set all object files to $objs before create\_makefile.

$objs = [“my\_ext.o”, “utils/utils.o”]

create\_makefile(“my\_ext”)

# to clean up object files under util subdirectory.

open(‘Makefile’, ‘a’) do |f|  
f.write \<\<EOS

CLEANOBJS := $(CLEANOBJS) utils/\*.#{CONFIG[“OBJEXT”]}  
EOS  
end

---

<div class="post-metadata">

**Author:** ![SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@SSISSaki\_Baz\_C.SS\_S](https://www.ruby-forum.com/u/SSISSaki_Baz_C.SS_S)\
**Post date:** [May 7, 2012, 7:53pm UTC](https://www.ruby-forum.com/t/mkmf-how-to-include-c-and-h-files-within-a-subdirectory/219540/4 "2012-05-07T19:53:34Z")

</div>

2012/5/7 Kubo T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > How to say in extconf.rb that files under utils/ subdirectory must
> 
> But it doesn’t work for rubinius.  
> Thanks a lot. Very useful information.
