Questions tagged [swig]

SWIG is an interface compiler that connects programs written in C and C++ with scripting languages such as Perl, Python, Ruby, and Tcl.

SWIG is typically used to parse C/C++ interfaces and generate the 'glue code' required for the target language to call into the C/C++ code. SWIG can also export its parse tree in the form of XML and Lisp s-expressions. SWIG is free software and the code that SWIG generates is compatible with both commercial and non-commercial projects.

For information about the features of SWIG and the supported languages see: http://www.swig.org/compare.html

One of many projects using SWIG to generate libraries for the above languages is Subversion. Other projects listed here.

Documentation:

3063 questions
20
votes
6 answers

Python Properties & Swig

I am attempting to create python bindings for some C++ code using swig. I seem have run into a problem trying to create python properties from some accessor functions I have for methods like the following: class Player { public: void…
fuzzy-waffle
  • 830
  • 5
  • 11
19
votes
1 answer

SWIG Python bindings to native code not working with OpenCV 2.1

I have an OpenCV project mixing Python and C. After changing to OpenCV 2.1, my calls to C code are not working any more, probably because OpenCV is no more using SWIG bindings. From Python, I was used to call a C function with the following…
TH.
  • 1,738
  • 1
  • 12
  • 15
18
votes
1 answer

Using SWIG with methods that take std::string as a parameter

I used SWIG to wrap my c++ class. Some methods have a const std::string& as a parameter. SWIG creates a type called SWIGTYPE_p_std__string however you cannot just pass a normal string for this when invoking the method in c#. The below example is…
Seth
  • 8,213
  • 14
  • 71
  • 103
18
votes
1 answer

How can I generate C wrappers over C++ API using SWIG?

I would like to generate C wrappers over some C++ API using SWIG. From the SWIG documentation SWIG and C++: 6.2 Approach To wrap C++, SWIG uses a layered approach to code generation. At the lowest level, SWIG generates a collection of procedural…
piokuc
  • 25,594
  • 11
  • 72
  • 102
18
votes
2 answers

Pocketsphinx install fail? Raspberry Pi Zero (Raspbian Jessie)

This will probably get tagged as a duplicate, but I haven't had any luck, so here we go. I'm trying to develop a "Jarvis" like setup with Python2.7. I', looking to use Pocketsphinx as part of that. I tried to do this on my Windows 10 machine, but…
Bugiroff
  • 215
  • 2
  • 7
18
votes
2 answers

How to expose std::vector as a Python list using SWIG?

I'm trying to expose this function to Python using SWIG: std::vector get_match_stats(); And I want SWIG to generate wrapping code for Python so I can see it as a list of integers. Adding this to the .i file: %include "typemaps.i" %include…
Marcos Lara
  • 1,883
  • 5
  • 18
  • 20
18
votes
3 answers

Does CMake support Python3?

I am not able to build a Python wrapper module for my C library via CMake and Swig for Python3. Everything works fine for Python2.x but it looks like CMake cannot find Python3. I already looked around and tried a couple of things. For example, my…
mijc
  • 1,189
  • 3
  • 9
  • 16
17
votes
6 answers

How to Install M2crypto on Windows

After installing OpenSSL, downloading the pre-built Swig executable, and ensuring the openssl libraries are located in the default c:\pkg, pip install m2crypto results in: ... C:\Program Files (x86)\gfortran\bin\gcc.exe -mno-cygwin -mdll -O -Wall…
ecoe
  • 4,994
  • 7
  • 54
  • 72
17
votes
1 answer

How to create passable from C# into C++ delegate that takes a IEnumerable as argument with SWIG?

So I have next C++ code: #ifdef WIN32 # undef CALLBACK # define CALLBACK __stdcall #else # define CALLBACK #endif #include #include namespace OdeProxy { typedef std::vector< double > state_type; typedef void…
myWallJSON
  • 9,110
  • 22
  • 78
  • 149
16
votes
2 answers

Swig / Python memory leak detected

I have a very complicated class for which I'm attempting to make Python wrappers in SWIG. When I create an instance of the item in Python, however, I'm unable to initialize certain data members without receiving the message: >>> myVar =…
Zack
16
votes
2 answers

Difference between "%include" and "#include"

In SWIG, what is the difference between the "%include" directive, and the standard C "#include"? For instance, in all the tutorials, why do they typically look something like this: %module my_module %{ #include "MyHeader.h" %} %include…
J T
  • 4,946
  • 5
  • 28
  • 38
16
votes
1 answer

SWIG: difference between %import and %include

The SWIG docs explain these two directives as follows: %include: "To include another file into a SWIG interface, use the %include directive ... Unlike, #include, %include includes each file once (and will not reload the file on subsequent %include…
spencerlyon2
  • 9,476
  • 4
  • 30
  • 39
16
votes
2 answers

Convert a std::vector to a NumPy array without copying data

I have a C++ library which currently has some methods inside which return a std::vector defined like public: const std::vector& getValues() const; I'm currently working on wrapping the whole library for Python using SWIG and this is…
Milliams
  • 1,474
  • 3
  • 21
  • 30
16
votes
2 answers

What is the correct way to use add_jar with Java bindings produced by Swig?

I am using Swig to produce a Java binding for a C library. The build system is written in CMake in order to achieve platform neutrality. I wish to produce a JAR file containing the Java bindings (i.e. the .class files resulting from the compilation…
k-wall
  • 409
  • 3
  • 9
16
votes
3 answers

What Is The Cleanest Way to Call A Python Function From C++ with a SWIG Wrapped Object

I have the following code, which implements a simple C++ class (ObjWithPyCallback) with a Python callback function. The idea is to call the Python function with "this" as the single argument. The problem is that since ObjWithPyCallback is a SWIG…
Craig Wright
  • 1,575
  • 1
  • 11
  • 19