1

I am trying to use swig to call c++ functions from python. Its actually working (I can import the module in python), but there are some warnings that i want to solve before i get in trouble later.

I tried to just include the code, that produces the warning.

I have this Classes in my MDF4.h Header File:

// MDF4.h
.
.
.
typedef struct
{
  // Block Header
  enum { RID=M4ID_FH };
  // enumeration of links
  enum
  {
    fh_fh_next, // Link to next FHBLOCK (can be NIL if list finished)
    fh_md_comment, 
    LinkMax       // # of known links
  };
  // Data members
  M_DATE   fh_time;
  M_UINT8  fh_reserved[3]; // Reserved
} m4FHRecord;

.
.

template<class R,class T=BYTE,int ID=R::RID> class m4BlockImpl : public m4Block,public R
{
public:
  m4BlockImpl(size_t nVar=0) : m4Block(R::LinkMax),m_var(nVar)
  {
    m_Hdr.hdr_hdr=M4ID_ID;
    m_Hdr.hdr_id=ID;
    R *pThis=static_cast<R *>(this);
    memset(pThis,0,sizeof(R));
  }
  BOOL setCommentBlk(m4Block &TXorMD,int linkNo)
  {
    // cannot call twice
    if (!hasLink(linkNo))
    {
      ATLASSERT(TXorMD.hdrID()==M4ID_TX || TXorMD.hdrID()==M4ID_MD);
      ATLASSERT(m_File);
      M_LINK mdAt=TXorMD.Create(m_File,3);
      if (mdAt)
      {
        setLink(linkNo,mdAt);
        return TRUE;
      }
    }
    return FALSE;
  }
};

.
.

class M4FHBlock : public m4BlockImpl<m4FHRecord>
{
public:
  M4FHBlock(MDF4File *File);     // ctor: create and insrt current time
  M4FHBlock();                   // used for reading

  BOOL setComment(m4Block &md);  // CANNOT be a TX Block!
  DECLARE_DUMP
};

And this is my MDF4.i interface file:

%module MDF4

%{
#include "mdf4_lib_v2_019\\stdafx.h"
#include "mdf4_lib_v2_019\\utf8.h"
#include "mdf4_lib_v2_019\\Resource.h"
#include "mdf4_lib_v2_019\\mdfConfig.h"
#include "mdf4_lib_v2_019\\Ptrlist.h"
#include "mdf4_lib_v2_019\\dynArray.h"
#include "mdf4_lib_v2_019\\miniz.c"
#include "mdf4_lib_v2_019\\md5.h"
#include "mdf4_lib_v2_019\\m4Dump.h"
#include "mdf4_lib_v2_019\\mdFile.h"
#include "mdf4_lib_v2_019\\mdfTypes.h"
#include "mdf4_lib_v2_019\\mdf4.h"

%}

%feature("autodoc", "1");
%feature("flatnested");

%rename(__incr__)   Indent::operator++;
%rename(__incr__)   ptrlist::iterator::operator++;
%rename(__eq__)     ptrlist::iterator::operator=;
%rename(__decr__)   ptrlist::iterator::operator--;
%rename(__eq__)     ptrlist::iterator::operator=;
%rename(__invert__) ptrlist::iterator::operator!;
%rename(__incr__)   DbtObjPtrList::iterator::operator++;
%rename(__eq__)     DbtObjPtrList::iterator::operator=;
%rename(__decr__)   DbtObjPtrList::iterator::operator--;
%rename(__eq__)     DbtObjPtrList::iterator::operator=;
%rename(__invert__) DbtObjPtrList::iterator::operator!;

%include <wchar.i>
%include <cwstring.i> 
%include <std_vector.i>
%include <std_map.i>

%include "mdf4_lib_v2_019\\stdafx.h"
%include "mdf4_lib_v2_019\\utf8.h"
%include "mdf4_lib_v2_019\\Resource.h"
%include "mdf4_lib_v2_019\\mdfConfig.h"
%include "mdf4_lib_v2_019\\Ptrlist.h"
%include "mdf4_lib_v2_019\\dynArray.h"
%include "mdf4_lib_v2_019\\miniz.c"
%include "mdf4_lib_v2_019\\md5.h"
%include "mdf4_lib_v2_019\\m4Dump.h"
%include "mdf4_lib_v2_019\\mdFile.h"
%include "mdf4_lib_v2_019\\mdfTypes.h"
%include "mdf4_lib_v2_019\\mdf4.h"

But i get always this warning after calling swig -c++ -python MDF4.i:

mdf4_lib_v2_019\mdf4.h(1226) : Warning 401: Nothing known about base class 'm4BlockImpl< m4FHRecord >'. Ignored.
mdf4_lib_v2_019\mdf4.h(1226) : Warning 401: Maybe you forgot to instantiate 'm4BlockImpl< m4FHRecord >' using %template.



---- EDIT ----- I created a simple example with the same warning output:
test.h:

typedef struct
{
  enum { RID=1}; 
}myStruct;


template<class A,class B=BYTE,int ID=A::RID> class Foo : public A
{
  int a;

public:
  Foo()
  {
    int a = 1;
  }
};


class Bar : public Foo<myStruct>
{
public:
  Bar(); 
};

test.i:

%module test

%{

#include "test.h"

%}

%include "test.h"


hkn27
  • 51
  • 5
  • 1
    That's not an easy reproducer, so just a few guesses: did you try adding `%template(m4BlockImplFHRecord) m4BlockImpl< m4FHRecord >;` to the .i file? Or adding an explicit instantiation `template class m4BlockImpl< m4FHRecord >;` to a .cpp file that gets linked into the wrapper module? – Wim Lavrijsen Nov 14 '19 at 20:31
  • %template is the missing thing there for sure. – Flexo Nov 15 '19 at 08:22
  • If i add %template.. before MDF4.h -> It changes nothing – hkn27 Nov 15 '19 at 09:37
  • If i add %template... after MDF4.h -> it says: ``` mdf4_lib_v2_019\mdf4.h(1228) : Warning 401: Base class 'm4BlockImpl .. undefined. mdf4.i(54) : Warning 401: 'm4BlockImpl .. must be defined before it is used as a base class. ``` – hkn27 Nov 15 '19 at 09:38
  • I created a simple example with the same warning – hkn27 Nov 15 '19 at 09:55
  • Answer is here: https://stackoverflow.com/questions/41607471/swig-c-to-c-how-to-wrap-classes-from-c-to-make-methods-from-template-class I.e. would be best if you could split the base and derived classes so that the `%template` directive can be put in between. – Wim Lavrijsen Nov 15 '19 at 21:45

0 Answers0