# Making a VALUE visible to multiple source files

**URL:** <https://www.ruby-forum.com/t/making-a-value-visible-to-multiple-source-files/184151>\
**Category:** Ruby\
**Created:** [February 25, 2010, 6:39pm UTC](https://www.ruby-forum.com/t/making-a-value-visible-to-multiple-source-files/184151 "2010-02-25T18:39:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 25, 2010, 6:39pm UTC](https://www.ruby-forum.com/t/making-a-value-visible-to-multiple-source-files/184151/1 "2010-02-25T18:39:34Z")

</div>

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

Why do I want to do this? In the long run, I’m trying to figure out  
how to setup Check\_Type calls for my own classes, e.g.  
Check\_Type(some\_value, cFoo);

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 25, 2010, 8:18pm UTC](https://www.ruby-forum.com/t/making-a-value-visible-to-multiple-source-files/184151/2 "2010-02-25T20:18:32Z")

</div>

Daniel B. wrote:

> 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:

Try “extern VALUE …” in the headers, and “VALUE …” in one source  
file.

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 25, 2010, 9:11pm UTC](https://www.ruby-forum.com/t/making-a-value-visible-to-multiple-source-files/184151/3 "2010-02-25T21:11:13Z")

</div>

Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> foo/ext/stuff/stuff.c  
> create\_makefile(‘stuff’)  
> 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;

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 25, 2010, 10:17pm UTC](https://www.ruby-forum.com/t/making-a-value-visible-to-multiple-source-files/184151/4 "2010-02-25T22:17:56Z")

</div>

On Feb 25, 1:08 pm, Ken B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > foo/ext/stuff/baz.h  
> > require ‘mkmf’  
> > #include \<ruby.h\>  
> > #include \<ruby.h\>  
> > VALUE cBar;  
> > Ken, Joel,

Thanks. I don’t know why, but I had it in my head that I didn’t have  
to extern the values.

Regards,

Dan
