[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Making a VALUE visible to multiple source files
From:       Ken Bloom <kbloom () gmail ! com>
Date:       2010-02-25 20:08:39
Message-ID: 0i1j57-1nh.ln1 () cat-in-the-hat ! dnsalias ! com
[Download RAW message or body]

Daniel Berger <djberg96@gmail.com> wrote:
> Hi,
> 
> I'm having some trouble pre-declaring some VALUE's. I've got a VALUE
> cFoo I want to be visible to multiple source files. Say the layout is
> something like this:
> 
> foo/ext/stuff/extconf.rb
> foo/ext/stuff/foo.c
> foo/ext/stuff/bar.c
> foo/ext/stuff/baz.h
> foo/ext/stuff/stuff.c
> 
> I want to define all the class VALUE's in the header file. I would
> then like to be able to '#include "baz.h"' inside both foo.c and
> bar.c. However, that causes an ld error when I try to build the source
> files:
> 
> ld: duplicate symbol _cFoo in foo.o and bar.o
> 
> # extconf.rb
> require 'mkmf'
> create_makefile('stuff')
> 
> # Rakefile
> require 'rake'
> require 'rake/extensiontask'
> Rake::ExtensionTask.new('stuff')
> 
> // baz.h
> #ifndef BAZ_H_INCLUDED
> #define BAZ_H_INCLUDED
> #include <ruby.h>
> VALUE cFoo;
> VALUE cBar;
> #endif

This is a pretty standard C technique:

in baz.h:

#ifndef BAZ_H_INCLUDED
#define BAZ_H_INCLUDED
#include <ruby.h>
//these lines just say that these variables exist
extern VALUE cFoo;
extern VALUE cBar;
#endif

in *one* of your .c files (presumably baz.c)

//these lines declare memory for these variables
//and need to appear exactly once in your project
VALUE cFoo;
VALUE cBar;

-- 
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic