I'm trying to wrap an opaque type in C using SWIG but I cant understand how to. I have three files listed below:
simplelib.c:
#include <assert.h>
#include <stdlib.h>
#include "simplelib.h"
struct _simplelib_my_type {
double x;
double y;
};
simplelib_MyType *
simplelib_mytype_create(double x, double y)
{
simplelib_MyType *mt = NULL;
if (mt = calloc(1, sizeof(*mt))) {
mt->x;
mt->y;
}
return mt;
}
void
simplelib_mytype_destroy(simplelib_MyType *mt)
{
if (mt) {
free(mt);
mt = NULL;
}
}
int
simplelib_mytype_calc(const simplelib_MyType *mt, double z, double *res)
{
int ok = 0;
assert(mt);
if (z != 0.0) {
*res = mt->x * mt->y / z;
ok = 1;
}
return ok;
}
simplelib.h:
#ifndef SIMPLELIB_H
#define SIMPLELIB_H
typedef struct _simplelib_my_type simplelib_MyType;
simplelib_MyType *simplelib_mytype_create(double x, double y);
void simplelib_mytype_destroy(simplelib_MyType *mt);
int simplelib_mytype_calc(const simplelib_MyType *mt, double z, double *res);
#endif // SIMPLELIB_H
and my interface file simplelibswig.i:
%module simplelibswig
%{
extern "C" {
#include "simplelib.h"
}
%}
%include "simplelib.h"
I build everything using CMake, using this CMakeLists.txt:
project(simplelib)
cmake_minimum_required(VERSION 2.8)
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})
find_package(PythonLibs)
include_directories(${PYTHON_INCLUDE_PATH})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
SET(CMAKE_SWIG_FLAGS "")
SET_SOURCE_FILES_PROPERTIES(simplelibswig.i PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(simplelibswig.i PROPERTIES SWIG_FLAGS "-includeall")
add_library(${PROJECT_NAME}
simplelib.h
simplelib.c
)
swig_add_module(simplelibswig python simplelibswig.i)
swig_link_libraries(simplelibswig ${PYTHON_LIBRARIES} ${PROJECT_NAME})
Now, what I would like to do is to 1) rename the opaque type from simplelib_MyType to MyType 2) expose the type with constructor/destructor/method using %extend
The problem is that the above does not expose the type in the built python module. I would expect the interface file to expose the typedef as a class with the typedefed name but that is not happening. Thus I can't move on to point 1 and 2 above. What am I doing wrong?
Best regards, Rickard