[Python] I cannot get swig to apply a basic typemap when mapping a python list to a pointer to...

Discussão em 'Python' iniciado por Stack, Agosto 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to create a python interface for a larger project but as I kept running into this issue I decided to rtfm and do a minimal example. Turns out that I run into the same issue both on windows and on linux so there's obviously something I don't understand here. These are the files:

    // example.c

    #include <math.h>
    #include "example.h"

    double rms(double* seq, int n) {
    double sum = 0.0;
    for (int i = 0; i < n; i++) {
    sum += seq * seq;
    }
    return sqrt(sum / n);
    }


    // example.h


    #ifndef EXAMPLE_H
    #define EXAMPLE_H

    double rms(double* seq, int n);

    #endif


    // example.i

    %module example

    %include "typemaps.i"

    %{
    #include "example.h"
    %}

    %apply (double *IN_ARRAY1, int DIM1) { (double *seq, int n) };


    %include "example.h"


    Running swig -python example.i consistently gives me

    Warning 453: Can't apply (double *IN_ARRAY1,int DIM1). No typemaps are defined.


    The examples that do work are simple examples from the SWIG website without typemaps:

    int fact(int n);

    int my_mod(int x, int y);

    char *get_time();


    Structs also work fine in my larger project.

    I confirmed that typemaps.i is available to swig (by introducing a typo such as 'trmpaps.i') and observing that it complains.

    I did some tests with numpy.i with similar results but don't want to introduce more confusion to this question.

    Continue reading...

Compartilhe esta Página