Compiling multiple C source files into a single library



Hi,

In the past I've always put all my code into a single source file and
built it that way. However, I'd like to split out my C source files
into a "one class per file" arrangement for a particular project, but
I'm not sure how to do it. I'm using rake-compiler on Snow Leopard,
btw.

Here's the layout I have:

my_project
Rakefile
ext/
foo/
extconf.rb
foo.c
bar.c

foo.c and bar.c are pretty basic C source files:

// foo.c
#include <ruby.h>

void Init_foo(){
VALUE cFoo = rb_define_class("Foo", rb_cObject);
rb_define_const(cFoo, "VERSION", rb_str_new2("0.0.1"));
}

// bar.c
#include <ruby.h>

// I also tried Init_foo here, but that lead to a compiler error
void Init_bar(){
VALUE cBar = rb_define_class("Bar", rb_cObject);
rb_define_const(cBar, "VERSION", rb_str_new2("0.0.1"));
}

The extconf.rb file is just "require 'mkmf'; create_makefile('foo')".

The Rake task is simply "Rake::ExtensionTask.new('foo')".

Running "rake compile:stuff" builds like so:

gcc -I. -I/opt/ree/lib/ruby/1.8/i686-darwin10.2.0 -I/opt/ree/lib/ruby/
1.8/i686-darwin10.2.0 -I../../../../ext/foo -D_XOPEN_SOURCE -
D_DARWIN_C_SOURCE -fno-common -g -Os -fno-strict-aliasing -pipe -
fno-common -c ../../../../ext/foo/bar.c
gcc -I. -I/opt/ree/lib/ruby/1.8/i686-darwin10.2.0 -I/opt/ree/lib/ruby/
1.8/i686-darwin10.2.0 -I../../../../ext/foo -D_XOPEN_SOURCE -
D_DARWIN_C_SOURCE -fno-common -g -Os -fno-strict-aliasing -pipe -
fno-common -c ../../../../ext/foo/foo.c
cc -dynamic -bundle -undefined suppress -flat_namespace -o foo.bundle
bar.o foo.o -L. -L/opt/ree/lib -L. -ldl -lobjc
cd -
cp tmp/i686-darwin10.2.0/foo/1.8.7/foo.bundle lib/foo.bundle

But, if I run the following test.rb program it fails:

$:.unshift 'lib'
require 'stuff' # ok
p Foo::VERSION # ok
p Bar::VERSION # fail, uninitialized constant Bar

Why isn't Bar visible? What's the right way to do what I'm attempting?

Regards,

Dan

.



Relevant Pages

  • Re: Building module Encode::Detect on solaris 10 platform
    ... all source files are compiled, but then "make test" fails: ... compiler almost works for me, too, on Win32. ... loadable object didn't build, the tests should not even run - at least ...
    (comp.lang.perl.modules)
  • Re: 4.9 buildworld fails
    ... > fails to complete a buildworld. ... The randomly named file ... scp all he source files from the working machine and recompile. ...
    (freebsd-questions)
  • Re: Compiling multiple C source files into a single library
    ... I'd like to split out my C source files ... The create_makefilecall determines the name of the shared object ... if I run the following test.rb program it fails: ...
    (comp.lang.ruby)
  • insufficient virtual memory
    ... Running cc on a VS4000/90 with 64MB physical memory ... under vax/vms 7.3 works fine on "small" source files, ... but fails on an 8K line source file, ... Compiler abort - virtual memory limits exceeded. ...
    (comp.os.vms)
  • Re: (Programming) Various Syntax styles
    ... one syntax style: ... > int main ... is proportional to the number of lines into the source files. ... ,len))return len;}return 0;}static void byebye{clean;exit(-1 ...
    (comp.lang.cpp)