- SWIG is a development tool that helps software written in C or C++ to be embedded with other high-level programming languages.
- SWIG can be used in a variety of different languages, including common script compiling languages, such as Perl, PHP, Python, Tcl, Ruby and PHP.
- SWIG is widely used to create high-level language parsing or assembly environment, user interface, as a tool for testing C/C++ or prototyping.
There is a small problem in the work: an encrypted/decrypted program was originally written in C++ language, and later needed to be called in Python. The simple and crude way is to rewrite a Python version of the program according to C++ code, but it's troublesome and brings the following problems:
- Time costs, development costs
- Maintenance cost, need self-maintenance, repeat wheels
- Code redundancy, multiple sets of code, difficult to maintain
Later, the veteran introduced that SWIG can be used to encapsulate C++ code into Python library for Python calls.
SWIG (Simplified Wrapper and Interface Generator) is a software development tool for constructing scripting language interfaces for C and C++ programs. SWIG is actually a compiler that takes C/C++ declarations and wraps them in a shell so that they can be accessed in other languages. Therefore, the greatest advantage of SWIG is to combine the development efficiency of scripting language with the running efficiency of C/C++.
1 Original document
Write the original h and cpp files. Examples are as follows:
//File: EncryptTool.h
int EncryptFile(const char *szInputFile, const char *szOutputFile);
int DecryptFile(const char *szInputFile, const char *szOutputFile);
//File: EncryptTool.cpp
int EncryptFile(const char *szInputFile, const char *szOutputFile)
{
... //Omit implementation code
}
int DecryptFile(const char *szInputFile, const char *szOutputFile)
{
... //Omit implementation code
}
2. Writing. i Interface File
New file EncryptTool.i is added as follows:
//File: EncryptTool.i
%module EncryptTool
%{
#define SWIG_FILE_WITH_INIT
#include "EncryptTool.h"
%}
int EncryptFile(const char *szInputFile, const char *szOutputFile);
int DecryptFile(const char *szInputFile, const char *szOutputFile);
The.i interface file consists of three parts:
- % The name after module is the encapsulated module name through which Python loads the program.
- %... %} The contents added between them generally contain some function declarations and header files needed by this file.
- The last part declares the functions and variables to be encapsulated.
If the function declaration part to be encapsulated is written in the header file, the last part can include the header file name directly with% include.
//File: EncryptTool.i
%module EncryptTool
%{
#define SWIG_FILE_WITH_INIT
#include "EncryptTool.h"
%}
%include "EncryptTool.h"
The effect of the two. i files is the same. The second method is recommended to simplify the interface files.
3 Encapsulation Code
Encapsulate the c++ code by executing the following commands:
swig -python -c++ EncryptTool.i
EncryptTool.py and EncryptTool_wrap.cxx are generated after execution, which is equivalent to encapsulating the original cpp file, wrap has one layer.
4 Generating Dynamic Link Library
Write setup.py file for automatic compilation of dynamic link libraries:
#File: setup.py
#!/usr/bin/python2.7
from distutils.core import setup, Extension
#Generate an extension module
pht_module = Extension('_EncryptTool', #Module name must be underlined
sources=['EncryptTool_wrap.cxx', #Encapsulated interface cxx file
'base32.cpp', #Here are the files on which the original code depends
'BlowFish.cpp',
'CyFile.cpp',
'CyHash.cpp',
'Encrypt.cpp',
'EncryptTool.cpp',
'md5.cpp',
'sha1.cpp',
'stdafx.cpp'
],
)
setup(name = 'EncryptTool', #Packed Name
version = '0.1',
author = 'SWIG Docs',
description = 'Simple swig pht from docs',
ext_modules = [pht_module], #Consistent with the extension module name above
py_modules = ['EncryptTool'], #List of modules to be packaged
)
After compiling, the following commands are executed for compilation:
python setup.py build
You may encounter the following error:
Python.h: No such file or directory
Solution
yum install python-devel
You may encounter the following error:
Gcc error: gcc: error trying to exec 'cc1': execvp: No such file or directory
Solution
yum install gcc-c++
The compiled Python library files: _EncryptTool.so and EncryptTool.py can be found in the subdirectory at the beginning of build/lib. *.
5 Loading libraries in Python
The generated dynamic link library can be invoked in the following way:
import EncryptTool
EncryptTool.EncryptFile( "data.xml" , "data.pyblf" ) #encryption
EncryptTool.DecryptFile( "data.pyblf" , "data.pyxml" ) #Decrypt