1

I have function which contains 3, one dimension array arguments and one length argument. for example:

float my_func(int *arr1, int *arr2, int *arr3, int length)

This array would be IN_ARRAY and needed to write swig wrapping for it so from the document of the swig, I found

DATA_TYPE IN_ARRAY3[ANY][ANY][ANY]

Is this the right way to do it?

3
  • You need pairs int *arr1, int len1 for each of the one-dimensional inputs, if you use the numpy.i file Commented Oct 23, 2020 at 18:26
  • @JensMunk Yes, I agree with you and I am aware of it as well. But I have the C function as described here. where only one length parameter is there. Commented Oct 26, 2020 at 9:41
  • Then create in your libraryName.i a C-wrapper function with a signature (int* arr1, int len1, int* arr2, int len2, int* arr3, int len3), which calls your function. Afterwards, you can use the %rename functionality of swig to make the wrapped functions name equal the original name Commented Oct 26, 2020 at 16:40

1 Answer 1

1

What you can is the following.

zerocool.i

%module zerocool
%{
  #define SWIG_FILE_WITH_INIT
  #include "zerocool.h"
%}

#ifdef SWIGPYTHON
%include "numpy.i"
%init {
  import_array();
 }
#endif

%apply (int* IN_ARRAY1, int DIM1) \
{(int* arr1, int len1)}
%apply (int* IN_ARRAY1, int DIM1) \
{(int* arr2, int len2)}
%apply (int* IN_ARRAY1, int DIM1) \
{(int* arr2, int len2)}

%inline %{
  float my_func(int *arr1, int len1,
                int *arr2, int len2,
                int *arr3, int len3) {
    return my_func(arr1, arr2, arr3, len3);
  }
%}

You original header, say zerocool.h

#pragma once

float my_func(int *arr1, int *arr2, int *arr3, int length);

Your source, say zerocool.cpp

#include "zerocool.h"

#include "stdio.h"

float my_func(int *arr1, int *arr2, int *arr3, int length) {
  printf("Function is called\n");
  // Do some work on your input data
  return 0.0f;
}

Sample CMakeLists.txt file

cmake_minimum_required(VERSION 3.0)

find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})

set(Python_ADDITIONAL_VERSIONS 3.5 3.6 3.7)
find_package(PythonInterp 3 REQUIRED)
find_package(PythonLibs)

include_directories(${PYTHON_INCLUDE_PATH})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

set_property(SOURCE zerocool.i PROPERTY SWIG_FLAGS "-D_SWIG_WIN32")
set_source_files_properties(zerocool.i PROPERTIES CPLUSPLUS ON)

swig_add_library(zerocool LANGUAGE python SOURCES zerocool.i zerocool.cpp)
swig_link_libraries(zerocool ${PYTHON_LIBRARIES})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.