xpg94
xpg94

Reputation: 513

SWIG shows error when wrapping C++ for Java

I want to wrap a c++ class using SWIG for java. I've followed the official tutorial and documentation and tried to make it work, but I get some errors. I use windows 7 x64.

So I opened cmd and typed:swig -c++ -java myclass.i

That command executed and generated few files.(it did not produce errors)

After that, I typed in: gcc -c myclass_wrap.cxx -I"C:\Program Files\Java\jdk1.7.0_60\include" -I"C:\Program Files\Java\jdk1.7.0_60\include\win32" And that command was successful as well. (did not produce errors)

Finally, I typed in ld -G myclass_wrap.o -o libmyclass.so

which produced bunch of undefined reference errors like:

myclass_wrap.o:myclass_wrap.cxx:<.text+0xa8>: undefined reference to '__cxa_allocate_exception'

Here is my initial c++ code, the following c++ class: header file:

/*myclass.h*/
#define MYCLASS_H
#ifndef MYCLASS_H
#include <vector>
#include <iostream>

class myClass
{
private:
    int ID;
    std::vector<int> vector;

public:
    myClass();
    myClass(int id, std::vector<int> v);

    void setID(int id);
    int getID();

    void setVector(std::vector<int> v);
    std::vector<int> getVector();

    void insertIntoVector(int num);
    void printVector();
};

#endif // MYCLASS_H

and cpp file:

/*myclass.cpp*/
#include "myclass.h"
myClass::myClass()
{
    ID=0;
    vector=std::vector<int>();
}

myClass::myClass(int id, std::vector<int> v)
{
    ID=id;
    vector=v;
}

void myClass::setID(int id)
{
    ID=id;
}

int myClass::getID()
{
    return ID;
}


void myClass::setVector(std::vector<int> v)
{
    vector=v;
}
std::vector<int> myClass::getVector()
{
    return vector;
}

void myClass::insertIntoVector(int num)
{
    std::vector<int>::iterator it;
    it=vector.end();
    vector.insert(it,num);
}

void myClass::printVector()
{
    std::vector<int>::iterator it;
    for (it=vector.begin(); it<vector.end(); it++)
        std::cout << ' ' << *it<< std::endl;
}

and the swig module file:

/*myclass.i*/    
%module test
%{
#include "myclass.h"
%}

%include "std_vector.i"
namespace std {
   %template(IntVector) vector<int>;
}

%include "myclass.h"

Upvotes: 1

Views: 767

Answers (1)

softwariness
softwariness

Reputation: 4052

After that, I typed in: gcc -c myclass_wrap.cxx -I"C:\Program Files\Java\jdk1.7.0_60\include" -I"C:\Program Files\Java\jdk1.7.0_60\include\win32"

It's C++ code, so in general prefer to compile with g++ instead of gcc (though not strictly necessary):

g++ -c myclass_wrap.cxx -I"C:\Program Files\Java\jdk1.7.0_60\include" -I"C:\Program Files\Java\jdk1.7.0_60\include\win32"

Finally, I typed in ld -G myclass_wrap.o -o libmyclass.so

which produced bunch of undefined reference errors like:

myclass_wrap.o:myclass_wrap.cxx:<.text+0xa8>: undefined reference to '__cxa_allocate_exception'

That example for building a shared library from the SWIG documentation was not for C++ (so C++-specific symbols were not found by the linker), nor was it for Windows.

Assuming you're using the Mingw version of gcc for Windows, use the following to compile a DLL myclass.dll instead:

g++ -shared -o myclass.dll myclass_wrap.o

For more information on building a DLL with Mingw, see this example on the Mingw site.

See also this SWIG FAQ page which has links to information about building shared libraries or DLLs for a variety of platforms and compilers (but beware many example commands are for C rather than C++).

Upvotes: 1

Related Questions