[Swig-user] C++ templates

112 views
Skip to first unread message

Kraus Philipp

unread,
Mar 23, 2012, 1:24:17 PM3/23/12
to swig...@lists.sourceforge.net
Hello,

I'm new with swig and I try to portage a C++ framework (primary templated based) to Java, Python and PHP.
I'm reading the documentation for C++ class structures, but can I use my template classes and tell swig, that
it should replace the template parameter with a static type? eg:

My template class (C++)

template<typename T> class myclass {
 
     public :
       myclass ( T );
       T get();
}

So can I tell swig that it should build a java class like myclass<double>, so it creates the wrapper with a
double type or should I write a own C++ class, that uses the in this case:

class myclassproxy {
     public :
          myclassproxy( double );
          double get();

     private :
        myclass<double> obj;
}

and the CPP:

myclassproxy::myclassproxy(double x) : obj(x) {}
double myclassproxy::get() { return obj.get() };

Thanks a lot

Phil

Philipp Kraus

unread,
Mar 23, 2012, 1:44:03 PM3/23/12
to swig...@lists.sourceforge.net
Hi,


On 2012-03-23 18:39:22 +0100, Kris Thielemans said:

> Hi
> I think you missed the part of the doc that says you should use %template
> for explicit template instantiation. See
> <http://www.swig.org/Doc2.0/SWIGPlus.html#SWIGPlus_nn30>
>
> A pain, but much better than having to create wrappers!

yesss, thanks a lot :-P That speeds up my work :-P
I have missed this chapter

So I need only to creare a *.i file with this template parameter and than I can
run swig with the target compiler

>
> Kris

> ------------------------------------------------------------------------------
> This SF email is sponsosred by:
> Try Windows Azure free for 90 days Click Here
> http://p.sf.net/sfu/sfd2d-msazure


------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Swig-user mailing list
Swig...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user

Philipp Kraus

unread,
Mar 23, 2012, 3:03:37 PM3/23/12
to swig...@lists.sourceforge.net
On 2012-03-23 18:39:22 +0100, Kris Thielemans said:

> Hi
> I think you missed the part of the doc that says you should use %template
> for explicit template instantiation. See
> <http://www.swig.org/Doc2.0/SWIGPlus.html#SWIGPlus_nn30>
>
> A pain, but much better than having to create wrappers!
>

> Kris


I try to compile my template class, so I have created a *.i file

#include "../../../myheaderfile.h"

%module "mymodulename"
%template(myclass) mynamespace::mysubnamespace::myclass <double>;

If I run swig I get the errir:
Tenplate mynamespace::mysubnamespace::myclass undefined

I have switched the code also to

%module "mymodulename"
% {
#include "../../../myheaderfile.h"
%}
%template(myclass) mynamespace::mysubnamespace::myclass <double>;

I would expand the template parameter T to double and setup the name
for the class.
Can I do this without adding some special code? Can swig extend the template
parameter T to double and use all method / properties definitions on my
class directly?

Thanks

Phil

------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure

Philipp Kraus

unread,
Mar 23, 2012, 4:24:55 PM3/23/12
to swig...@lists.sourceforge.net


I have add this code to my *.i file:

%module "mynamespace_mysubnamespace"

%{
#include "../../../mycppheader.h"
%}


%include "../../../mytemplateclassdef.hpp"
%template(myclass_template) mynamespace::mysubnamespace::myclass<double>;

Now I get the errors:

Error: Unknown namespace 'boost::numeric::ublas'
Error: Unknown namespace 'boost::iostreams'

That's correct, the template class mytemplateclassdef.hpp uses the Boost types.
I have setup my CPPPATH to the Boost include directory, can I tell
swig, that it
should use the path also or can it ignore this errors, because only the
compiler
needs the namespaces?

Can I rename this line to:


%template(myclass) mynamespace::mysubnamespace::myclass<double>;

?

Because swig creates the message:
Identifier 'myclass' redefined (ignored) (Renamed from 'myclass<double >')

I would like to use the same name of the created target class which has
also the
template class, so I would remove only the "template parameter".

Thanks

Philipp Kraus

unread,
Mar 23, 2012, 5:17:03 PM3/23/12
to swig...@lists.sourceforge.net


I try to compile my code with

swig -Wall -I/Developer/opt/Boost/1.48.0/include/ -includeall -c++
-python myclass.i

And get a lot of errors (excerpt):

../../../distances/myclass.hpp:29: Error: Unable to find 'omp.h'
../../../distances/myclass.hpp:30: Error: Unable to find 'string'
../../../distances/myclass.hpp:31: Error: Unable to find 'sstream'
../../../distances/myclass.hpp:32: Error: Unable to find 'iostream'
../../../distances/myclass.hpp:33: Error: Unable to find 'fstream'

/Developer/opt/Boost/1.48.0/include//boost/config.hpp:26: Error: Unable
to find 'BOOST_USER_CONFIG'
/Developer/opt/Boost/1.48.0/include//boost/config/select_stdlib_config.hpp:18:
Error: Unable to find 'cstddef'
/Developer/opt/Boost/1.48.0/include//boost/config/no_tr1/utility.hpp:21:
Error: Unable to find 'utility'
/Developer/opt/Boost/1.48.0/include//boost/config/suffix.hpp:57: Error:
Unable to find 'limits.h'
/Developer/opt/Boost/1.48.0/include//boost/numeric/ublas/storage.hpp:16:
Error: Unable to find 'algorithm'
/Developer/opt/Boost/1.48.0/include//boost/serialization/array.hpp:9:
Error: Unable to find 'iostream'
/Developer/opt/Boost/1.48.0/include//boost/serialization/array.hpp:10:
Error: Unable to find 'cstddef'
/Developer/opt/Boost/1.48.0/include//boost/serialization/array.hpp:11:
Error: Unable to find 'cstddef'
/Developer/opt/Boost/1.48.0/include//boost/serialization/nvp.hpp:19:
Error: Unable to find 'utility'
/Developer/opt/Boost/1.48.0/include//boost/type_traits/is_array.hpp:24:
Error: Unable to find 'cstddef'
/Developer/opt/Boost/1.48.0/include//boost/type_traits/remove_cv.hpp:19:
Error: Unable to find 'cstddef'
/Developer/opt/Boost/1.48.0/include//boost/mpl/aux_/include_preprocessed.hpp:37:
Error: Unable to find
'BOOST_PP_STRINGIZE(boost/mpl/aux_/preprocessed/AUX778076_PREPROCESSED_HEADER)'

/Developer/opt/Boost/1.48.0/include//boost/serialization/void_cast_fwd.hpp:20:
Error: Unable to find 'cstddef'
/Developer/opt/Boost/1.48.0/include//boost/mpl/apply_fwd.hpp:30: Error:
Macro 'BOOST_MPL_PREPROCESSED_HEADER' redefined,
/Developer/opt/Boost/1.48.0/include//boost/mpl/aux_/comparison_op.hpp:34:
Error: previous definition of 'BOOST_MPL_PREPROCESSED_HEADER'.
/Developer/opt/Boost/1.48.0/include//boost/mpl/assert.hpp:35: Error:
Unable to find 'cstddef'
/Developer/opt/Boost/1.48.0/include//boost/mpl/arg.hpp:36: Error: Macro
'BOOST_MPL_PREPROCESSED_HEADER' redefined,
/Developer/opt/Boost/1.48.0/include//boost/mpl/aux_/comparison_op.hpp:34:
Error: previous definition of 'BOOST_MPL_PREPROCESSED_HEADER'.
/Developer/opt/Boost/1.48.0/include//boost/mpl/placeholders.hpp:42:
Error: Macro 'BOOST_MPL_PREPROCESSED_HEADER' redefined,
/Developer/opt/Boost/1.48.0/include//boost/mpl/aux_/comparison_op.hpp:34:
Error: previous definition of 'BOOST_MPL_PREPROCESSED_HEADER'.
/Developer/opt/Boost/1.48.0/include//boost/mpl/bind_fwd.hpp:31: Error:
Macro 'BOOST_MPL_PREPROCESSED_HEADER' redefined,
/Developer/opt/Boost/1.48.0/include//boost/mpl/aux_/comparison_op.hpp:34:
Error: previous definition of 'BOOST_MPL_PREPROCESSED_HEADER'.
/Developer/opt/Boost/1.48.0/include//boost/mpl/bind.hpp:48: Error:
Macro 'BOOST_MPL_PREPROCESSED_HEADER' redefined,
/Developer/opt/Boost/1.48.0/include//boost/mpl/aux_/comparison_op.hpp:34:
Error: previous definition of 'BOOST_MPL_PREPROCESSED_HEADER'.
/Developer/opt/Boost/1.48.0/include//boost/mpl/quote.hpp:44: Error:
Macro 'BOOST_MPL_PREPROCESSED_HEADER' redefined,
/Developer/opt/Boost/1.48.0/include//boost/mpl/aux_/comparison_op.hpp:34:
Error: previous definition of 'BOOST_MPL_PREPROCESSED_HEADER'.
/Developer/opt/Boost/1.48.0/include//boost/mpl/aux_/template_arity.hpp:42:
Error: Macro 'BOOST_MPL_PREPROCESSED_HEADER' redefined,
/Developer/opt/Boost/1.48.0/include//boost/mpl/aux_/comparison_op.hpp:34:
Error: previous definition of 'BOOST_MPL_PREPROCESSED_HEADER'.
/Developer/opt/Boost/1.48.0/include//boost/mpl/aux_/full_lambda.hpp:43:
Error: Macro 'BOOST_MPL_PREPROCESSED_HEADER' redefined,
/Developer/opt/Boost/1.48.0/include//boost/mpl/aux_/comparison_op.hpp:34:
Error: previous definition of 'BOOST_MPL_PREPROCESSED_HEADER'.
/Developer/opt/Boost/1.48.0/include//boost/mpl/apply.hpp:35: Error:
Macro 'BOOST_MPL_PREPROCESSED_HEADER' redefined,
/Developer/opt/Boost/1.48.0/include//boost/mpl/aux_/comparison_op.hpp:34:
Error: previous definition of 'BOOST_MPL_PREPROCESSED_HEADER'.
/Developer/opt/Boost/1.48.0/include//boost/type_traits/remove_const.hpp:20:
Error: Unable to find 'cstddef'
/Developer/opt/Boost/1.48.0/include//boost/array.hpp:41: Error: Unable
to find 'cstddef'
/Developer/opt/Boost/1.48.0/include//boost/array.hpp:42: Error: Unable
to find 'stdexcept'
/Developer/opt/Boost/1.48.0/include//boost/assert.hpp:50: Error: Unable
to find 'assert.h'
/Developer/opt/Boost/1.48.0/include//boost/assert.hpp:81: Error: Unable
to find 'cstdlib'
/Developer/opt/Boost/1.48.0/include//boost/assert.hpp:82: Error: Unable
to find 'iostream'
/Developer/opt/Boost/1.48.0/include//boost/utility/swap.hpp:24: Error:
Unable to find 'algorithm'
/Developer/opt/Boost/1.48.0/include//boost/utility/swap.hpp:25: Error:
Unable to find 'cstddef'


Can swig handle the Boost, STL and OpenMP structurs ?

Phil

Kris Thielemans

unread,
Mar 23, 2012, 5:59:55 PM3/23/12
to Philipp Kraus, swig...@lists.sourceforge.net
> From: Philipp Kraus
> Sent: 23 March 2012 21:17
> To: swig...@lists.sourceforge.net
> Subject: Re: [Swig-user] C++ templates
> Error: Unable to find 'cstddef'
> /Developer/opt/Boost/1.48.0/include//boost/mpl/aux_/include_preprocess
> /Developer/opt/Boost/1.48.0/include//boost/type_traits/remove_const.hp

> p:20:
> Error: Unable to find 'cstddef'
> /Developer/opt/Boost/1.48.0/include//boost/array.hpp:41: Error: Unable
> to find 'cstddef'
> /Developer/opt/Boost/1.48.0/include//boost/array.hpp:42: Error: Unable
> to find 'stdexcept'
> /Developer/opt/Boost/1.48.0/include//boost/assert.hpp:50: Error: Unable
> to find 'assert.h'
> /Developer/opt/Boost/1.48.0/include//boost/assert.hpp:81: Error: Unable
> to find 'cstdlib'
> /Developer/opt/Boost/1.48.0/include//boost/assert.hpp:82: Error: Unable
> to find 'iostream'
> /Developer/opt/Boost/1.48.0/include//boost/utility/swap.hpp:24: Error:
> Unable to find 'algorithm'
> /Developer/opt/Boost/1.48.0/include//boost/utility/swap.hpp:25: Error:
> Unable to find 'cstddef'
>
>
> Can swig handle the Boost, STL and OpenMP structurs ?
>
I think you're trying to handle too many things at the same time. You should
try a simple template example first, to make sure that you understand how
swig works, before heading straight into the real stuff.

A few ideas:
- don't use -includeall. You don't really want to wrap all of boost, openmp,
c++ library etc etc. it's not going to work, and even if it did, it would
create a huge python library. -includeall is why you're getting the "file
not found" messages. You'd have to tell it where omp lives etc etc (swig is
not a C++ compiler, and doesn't know where your "normal" c++ compiler would
find these files).

- you ask "I would like to use the same name of the created target class
which has also the template class". Maybe this can do done, but sort out all
the other stuff first without renaming.

Good luck

Kris

Reply all
Reply to author
Forward
0 new messages