Search code examples
c++rubyswigundefined-symbol

SWIG undefined symbols


I am using SWIG to wrap C++ code in Ruby.

I have eight classes defined in eight separate files in a specific location. I had two approaches to wrapping them in Ruby.

In the first approach, I put all the classes in one file, placed that file in the same directory as the SWIG interface file and everything is okay.

I am, however, requested to link to the original location of the files, and have my interface file in a different directory. When I compile, I compile all the files in their directory plus the wrapper code and there are no errors produced. However, I get undefined symbols.

A part of my compile shell script is:

g++ -std=c++11 -fPIC -c ../../dir1/dir2/Class.cpp
g++ -std=c++11 -fPIC -c mymodule_wrap.cxx -I/usr/include/ruby-1.9.1 -I/usr/include/ruby-1.9.1/x86_64-linux

I compile all the other seven files in the same way as the "File.cpp" one. No compilation errors.

Then, when I try the following

require 'mymodule'
c = Mymodule::Class.new

I get an undefined symbol for the Class' constructor (I demangled the undefined symbol using c++filt), which is declared and defined.

Is there something wrong in the way I compile? Or are there some problems when it comes to different locations of the header/source files and the SWIG interface file? Because this is in no way different from when I have all the classes in one file, except for the location.

EDIT: If i move the definitions of the declared functions in the header files I get no undefined symbols. That means that it actually doesn't even reach the definitions in the cpp files. But why? When I had all classes unseparated I still kept the definitions in a cpp files and the declarations in a header file...


Solution

  • When creating the shared library, it didn't know where the object files of the source code were, therefore it never knew the definitions of whatever was declared in the header files.

    For me, all the object files were created in the same folder as the interface file where I was compiling everything, and I added this:

    g++ -shared Class.o mymodule_wrap.o -o mymodule.so
    

    to my compile shell script. Before I was using the extconf makefile creating script, and I am not sure where it searched for the object files.