Swig is the abbreviation of Simplified Wrapper and Interface Generator, it can give script language such as python the ability to invoke C and C++ libraries interface method indirectly.
It is implemented by compiling the C/C++ declaration file (.i file) into the C/C++ wrapper source code (.c or.cxx) file. By calling such wrapper interfaces directly, scripting languages can indirectly call C/C++ programming interfaces.
Using swig does not require modification of the wrapped C/C++ source code, and it takes only a few minutes to generate the wrapper source code file that you need.
Before you can use Swig, you should make sure it is installed already. If you meet error messages like unable to execute ‘swig’: No such file or directory, this means swig is not installed in your os.
1. Install Swig On macOS.
The macOS use HomeBrew to manage software installation, so you need to install HomeBrew first and then install swig.
- Run the below command to install HomeBrew in MacOS.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
- Install swig use homebrew.
brew install swig
- Run
swig -version
in terminal to verify swig installation.$ swig -version SWIG Version 3.0.12
2. Install Swig On Linux.
- Install g++ if you do not install it already.
sudo apt-get install g++
- Run
g++ -version
to verify it. - Install pcre.
sudo apt-get install libpcre3 libpcre3-dev
- Go to the swig download page to download Linux version swig installation package.
- Unzip the swig zip source code to a local directory.
// Change zip file permission. chmod 777 swig-3.0.12.tar.gz // Unzip the tar file. tar -xzvf swig-3.0.12.tar.gz
- Specify swig install directory.
./configure --prefix=/home/Jerry/library/swigtool
- Compile and install.
sudo make sudo make install
- Add SWIG_PATH environment variable, also add it in PATH environment variable.
sudo vim /etc/profile export SWIG_PATH=/home/Jerry/library/swigtool/bin export PATH=$SWIG_PATH:$PATH
- Make SWIG_PATH, PATH environment variable settings take effect.
source /etc/profile
- Verify swig installation.
$ swig -version SWIG Version 3.0.12
3. Install Swig On Windows.
- Go to swig-win download page to download the swig installer windows version.
- Unzip the zip file into a local folder such as C:\\swig-3.0.12.
- Add above swig unzip directory path to system environment variable Path values.
- Right-click Computer in the file explorer, then clicks Properties menu item in the popup menu list.
- Click Advanced System Settings in opened window left panel.
- Click Environment Variables button, then double click Path system variable and add the above swig unzip folder path value at the variable value end, remember use
;
to separate.
- Open dos window and execute
swig -version
to verify.
I have an old program which is wrote in C++, and I want to use Python as it’s front-end to wrap it, so I use SWIG. I use CMake to build the old program to make it running on both Linux, macOS and Windows. On Linux I use gcc as the compiler, on Windows I use MinGW and on macOS I use XCode.
Now I distribute the python lib by installing CMake, a C++ compiler and SWIG for different OS, and this is not very convenience. I want to distribute the python lib with something like a .exe file or .pkg file, can someone tell me how to do this easily? I know CPack maybe a good choice, but I am not familiar with it, can someone give me an example? Thanks a lot.
Hi Jerry,
Thanks for the article.
Found a code typo: “Run g++ -version to verify it.” should be –version
Best regards,
Henrik
Thank you very much:)