How To Install Swig On MacOS, Linux And Windows

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.

  1. 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
  2. Install swig use homebrew.
    brew install swig
  3. Run swig -version in terminal to verify swig installation.
    $ swig -version
    SWIG Version 3.0.12

2. Install Swig On Linux.

  1. Install g++ if you do not install it already.
    sudo apt-get install g++
  2. Run g++ -version to verify it.
  3. Install pcre.
    sudo apt-get install libpcre3 libpcre3-dev
  4. Go to the swig download page to download Linux version swig installation package.
  5. 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
  6. Specify swig install directory.
    ./configure --prefix=/home/Jerry/library/swigtool
  7. Compile and install.
    sudo make
    sudo make install
  8. 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
  9. Make SWIG_PATH, PATH environment variable settings take effect.
    source /etc/profile
  10. Verify swig installation.
    $ swig -version
    SWIG Version 3.0.12

3. Install Swig On Windows.

  1. Go to swig-win download page to download the swig installer windows version.
  2. Unzip the zip file into a local folder such as C:\\swig-3.0.12.
  3. Add above swig unzip directory path to system environment variable Path values.
  4. Right-click Computer in the file explorer, then clicks Properties menu item in the popup menu list.
  5. Click Advanced System Settings in opened window left panel.
  6. 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.
    change-windows-PATH-system-environment-variable
  7. Open dos window and execute swig -version to verify.

3 thoughts on “How To Install Swig On MacOS, Linux And Windows”

  1. 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.

  2. Hi Jerry,
    Thanks for the article.
    Found a code typo: “Run g++ -version to verify it.” should be –version

    Best regards,
    Henrik

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.