I have rich experience using swig. SWIG claims that it is a quick solution for packing things. But in real life ...
Minuses:
SWIG is designed as a common language for all and for more than 20 languages. As a rule, this leads to disadvantages:
- configuration is needed (SWIG.i templates), sometimes it’s difficult,
- lack of treatment for some special cases (see python properties below),
- lack of performance for some languages.
Python cons:
1) Code style inconsistency . C ++ and python have very different code styles (this is obvious, of course), the possibilities of swig for creating the target code over pythonish are very limited. As an example, you can create properties from getters and setters. See this q & a
2) Lack of a wide community . Swig has some good documentation. But if someone caught something that is not in the documentation, there is no information at all. No blogs or searches help. Therefore, in such cases, you need to strongly break the code generated by SWIG ... This is terrible, I can say ...
Procs:
In simple cases, it is very fast, easy and straightforward.
If you created the swig interface files once, you can wrap this C ++ code with any other 20+ languages (!!!).
One big problem with SWIG is performance. Since version 2.0.04 SWIG includes the “-builtin” flag, which makes SWIG even faster than other automated packaging methods. At least some tests show this.
When to use US SWIG?
So, I concluded for myself two cases when swig is good to use:
2) If you need to wrap C ++ code for several languages . Or, if possible, there may be a time when you need to distribute code for several languages. Using SWIG in this case is reliable.
1) If you need to quickly wrap only a few functions from some C ++ library for end use.
Live experience
Update :
A year and a half has passed since we converted our library using SWIG.
First we made a python version. There were a few moments when we ran into problems with SWIG - that’s true. But now we have expanded our library to Java and .NET. So, we have 3 languages with 1 SWIG. And I can say that SWIG rock in terms of time saving.
Update 2 :
This is two years when we use SWIG for this library. SWIG is integrated into our build system. We recently had a big change to the API in C ++. SWIG worked fine. The only thing we needed to do was add a few% renames to the .i files so that our CppCamelStyleFunctions() now looks_more_pythonish in python. At first, I was worried about some problems that might arise, but nothing happened. It was awesome. Just a few changes and everything that is distributed in 3 languages. Now I am sure that this is a good solution for using SWIG in our case.
Update 3 :
This is already 3 years, we use SWIG for our library. Major change : part of python is completely rewritten in pure python. The reason is that python is used for most of the applications in our library. Even if a pure version of python is slower than C ++ packaging, it is more convenient for users to work with pure python without struggling with native libraries.
SWIG is still used for versions of .NET and Java.
The main question is: "Would we use SWIG for python if we started the project from the very beginning?". We will! SWIG allowed us to quickly distribute our product in many languages. It worked for a certain period of time, which gave us the opportunity to better understand the requirements of our users.