# Mixing ObjC and C is possible?

**URL:** <https://www.ruby-forum.com/t/mixing-objc-and-c-is-possible/111546>\
**Category:** Ruby\
**Created:** [August 8, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/mixing-objc-and-c-is-possible/111546 "2007-08-08T15:03:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 8, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/mixing-objc-and-c-is-possible/111546/1 "2007-08-08T15:03:00Z")

</div>

hey all, i’d like to mixe ObjC and C into a C extension to Ruby.

the ObjC part (seticon for MacOS X) comes from MacOSXUtils, and works  
fine.

for the C ext 2 Ruby part i’ve allready tested successfully the  
squeleton of my class it works.

the last thing i have to do is to compil everything alltogether BUT  
HERE, i get probs from gcc which doesn’t like the “@” symbol used in  
the \*.h and \*.m files coming from the ObjC side.

presently i’m using, thru an extconf.rb file, mkmf to produce the  
Makefile.

i think the prob arroses here.

is there a way to get that working by modifying, manually, for ex, the  
Makefile, or, proceed in two steps ???

Yvon

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 8, 2007, 3:11pm UTC](https://www.ruby-forum.com/t/mixing-objc-and-c-is-possible/111546/2 "2007-08-08T15:11:02Z")

</div>

On 8/8/07, unbewust [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hey all, i’d like to mixe ObjC and C into a C extension to Ruby.
> 
> the ObjC part (seticon for MacOS X) comes from MacOSXUtils, and works  
> fine.

Have you looked at:  
[http://rubycocoa.sourceforge.net/HomePage](http://rubycocoa.sourceforge.net/HomePage)  
“a bridge between the Ruby and the Objective-C languages, allowing you  
to manipulate Objective-C objects from Ruby”

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 8, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/mixing-objc-and-c-is-possible/111546/3 "2007-08-08T15:31:07Z")

</div>

On 8 août, 15:09, removed\_email\_address@domain.invalid wrote:

> to manipulate Objective-C objects from Ruby"  
> i know RubyCocoa and use it i’ve even used ObjC in RubyCocoa but here  
> i need to write a C ext to ruby being, somehow a wrapper of an ObjC  
> API…

thanks for your answer !

Yvon

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 8, 2007, 5:10pm UTC](https://www.ruby-forum.com/t/mixing-objc-and-c-is-possible/111546/4 "2007-08-08T17:10:47Z")

</div>

On 8/8/07, unbewust [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > hey all, i’d like to mixe ObjC and C into a C extension to Ruby.
> > 
> > the ObjC part (seticon for MacOS X) comes from MacOSXUtils, and works  
> > fine.
> 
> i know RubyCocoa and use it i’ve even used ObjC in RubyCocoa but here  
> i need to write a C ext to ruby being, somehow a wrapper of an ObjC  
> API…

(left off my previous message:  
[rubycocoa.com](http://www.rubycocoa.com/ruby-extensions-with-rubycocoa/2))

What about SWIG?

[http://www.swig.org/Doc1.1/HTML/Introduction.html](http://www.swig.org/Doc1.1/HTML/Introduction.html)  
“SWIG is a code development tool that makes it possible to quickly  
build powerful scripting language interfaces to C, C++, or Objective-C  
programs.”

[http://www.swig.org/Doc1.1/HTML/SWIG.html#n7](http://www.swig.org/Doc1.1/HTML/SWIG.html#n7)

[http://www.swig.org/Doc1.3/Ruby.html](http://www.swig.org/Doc1.3/Ruby.html)

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 8, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/mixing-objc-and-c-is-possible/111546/5 "2007-08-08T17:35:57Z")

</div>

“Tim P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> On 8/8/07, unbewust [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > i know RubyCocoa and use it i’ve even used ObjC in RubyCocoa but here  
> > i need to write a C ext to ruby being, somehow a wrapper of an ObjC  
> > API…
> 
> I think what brabhur was suggesting was to look at the RubyCocoa  
> source code for some insight into how that project was able to call  
> ObjC from a Ruby extension.

My solution for calling ObjC from Perl XS extensions was to tell GCC to  
compile .c files (generated from .xs files and thus not easy to rename  
to .m without patching the utility that generates them) as Objective-C.  
The compiler flag for that is, surprisingly enough, -ObjC.

How to apply the -ObjC compiler flag when building Ruby extensions is a  
question for someone with more Ruby expertise than me. Sorry!

sherm–

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 8, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/mixing-objc-and-c-is-possible/111546/6 "2007-08-08T20:25:56Z")

</div>

On 8 août, 17:32, Sherm P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My solution for calling ObjC from Perl XS extensions was to tell GCC to  
> compile .c files (generated from .xs files and thus not easy to rename  
> to .m without patching the utility that generates them) as Objective-C.  
> The compiler flag for that is, surprisingly enough, -ObjC.

Thanks for this tip !!!

> How to apply the -ObjC compiler flag when building Ruby extensions is a  
> question for someone with more Ruby expertise than me. Sorry!

it’s really easy to do C ext for ruby …

Yvon

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 8, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/mixing-objc-and-c-is-possible/111546/7 "2007-08-08T16:55:44Z")

</div>

On 8/8/07, unbewust [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i know RubyCocoa and use it i’ve even used ObjC in RubyCocoa but here  
> i need to write a C ext to ruby being, somehow a wrapper of an ObjC  
> API…

I think what brabhur was suggesting was to look at the RubyCocoa  
source code for some insight into how that project was able to call  
ObjC from a Ruby extension.

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 8, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/mixing-objc-and-c-is-possible/111546/8 "2007-08-08T20:27:56Z")

</div>

On 8 août, 17:09, removed\_email\_address@domain.invalid wrote:

> [SWIG Basics](http://www.swig.org/Doc1.1/HTML/SWIG.html#n7)
> 
> [SWIG and Ruby](http://www.swig.org/Doc1.3/Ruby.html)

ok thanks for swig i’ll look at 😉

Yvon
