Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7783701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:54:53+00:00 2026-06-01T19:54:53+00:00

I am trying to compile a c++ extension for Ruby, and the compilation does

  • 0

I am trying to compile a c++ extension for Ruby, and the compilation does not return an error, but it does not seem to be compiling correctly. What am I doing wrong?

I have the main cpp script foo.cpp:

#include <iostream>
#include <ruby.h>
extern "C"

VALUE cFoo;
void Init_foo(){cFoo = rb_define_class("Foo", rb_cObject);}

and extconf.rb:

require "mkmf"
$libs += " -lstdc++ "
create_makefile("foo")

and within the directory of these files, I did

$ ruby extconf.rb
creating Makefile
$ make
compiling foo.cpp
linking shared-object foo.so
$ ls
Makefile extconf.rb foo.cpp foo.o foo.so

Then, I have a Ruby script test.rb:

#!/usr/bin/env ruby
require "path_to_this_directory/foo"

and I run test.rb. It returns an error:

... in `require': .../foo.so: undefined symbol: cFoo - .../foo.so (LoadError)

What am I doing wrong?

Environment

  • OS: Ubuntu Linux 11.10
  • Ruby: 1.9.3
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:54:55+00:00Added an answer on June 1, 2026 at 7:54 pm

    You’re declaring that cFoo exists:

    extern "C"
    
    VALUE cFoo;
    

    but you never define it. Saying extern "C" blahblah just says that blahblah exists, that it is externally visible, and that it won’t have its name mangled (i.e. “C” linkage) but an extern declaration doesn’t actually define anything; extern says that something exists but it doesn’t bring it into existence.

    Your C++ should look more like this:

    #include <iostream>
    #include <ruby.h>
    
    extern "C" VALUE cFoo;
    extern "C" void Init_foo();
    
    VALUE cFoo;
    void Init_foo(){cFoo = rb_define_class("Foo", rb_cObject);}
    

    You need two extern "C" to tell the C++ compiler to leave the names alone and then separate definitions for the symbols. You could also say this:

    #include <iostream>
    #include <ruby.h>
    
    extern "C" VALUE cFoo;
    
    VALUE cFoo;
    extern "C" void Init_foo(){cFoo = rb_define_class("Foo", rb_cObject);}
    

    I’m not sure if the second version is standard C++ or a GCC extension.

    I’m not much of a C++ person so I hope I haven’t butchered the terminology too badly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to compile an extension library for CppUnit. But it gives me the
I'm trying to write a Ruby extension, and I've been successfully compiling my nmatrix.so
Trying to compile a iPhone/iPad application with SDK3.2 and am getting this error: Undefined
When trying to compile my class I get an error: The constant 'NamespaceName.ClassName.CONST_NAME' cannot
When trying to compile GNU software on HP-UX (binutils for example) I'm getting error
I'm trying to compile pymssql for python 2.5. I'm doing this because I think
I have been having a terrible time trying to compile a python extension -
I am trying to compile a simple cython extension from the example page here
I've been trying to compile a Borland C++ Builder 6 project, but linker dies
I'm trying to call an extension method on my own class, but it fails

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.