Search code examples
pythonswig

swig cannot wrap exported c++ file into python


I am using swig to wrap c++ into python.

My PackA.h is:

#ifndef PACKA_HEADER_H
#define PACKA_HEADER_H

#include <string>
class  PackA
{
public:
    PackA();

};

#endif

And my PackA.i is:

%module PackA

%{
#define SWIG_FILE_WITH_INIT
#include "PackA.h"
%}
%include "PackA.h"

It successfully generate python module.

However, if I declare PackA to dll by:


// #include "PackAGlobal.h"

#ifndef PACKA_EXPORTES_H
#define PACKA_EXPORTES_H

#if (WIN32)
    #ifdef PackA_EXPORTS
        #define PackA_EXPORT __declspec(dllexport)
    #else
        #define PackA_EXPORT __declspec(dllimport)
    #endif
#else
    #define PackA_EXPORT
#endif


#endif

// PackA.h
#ifndef PACKA_HEADER_H
#define PACKA_HEADER_H

#include "PackAGlobal.h"
#include <string>
class  PackA
{
public:
    PackA();

};

#endif

VS reports bugs:

c4430 missing type specifier - int assumed. note c++ does not support default-int
c2071 abstract declarator illegal storage class
c2513: PackA:  no variable declared before =

How can I wrap this export class into python?

Update-----------------------------------------------------------

Thank suggestion from @Mark Tolonen.

I modify my PackA.i as:

%module PackA

%{
#define SWIG_FILE_WITH_INIT
#include "PackA.h"
%}
%include <windows.i>
%include "PackA.h"

However, the bug is still remained.

I have uploaded the example code to github: https://github.com/zhang-qiang-github/SwigTest


Solution

  • Finally, I find the solution, and I have uploaded the correct code to github: https://github.com/zhang-qiang-github/SwigTest. Hope it can help someone.

    The solution is:

    1. add %include "PackAGlobal.h" in PackA.i.
    2. declare PackA_EXPORTS for PackAWrapping:
    SET_TARGET_PROPERTIES(PackAWrapping
        PROPERTIES
        COMPILE_DEFINITIONS PackA_EXPORTS
    )