# 2 Getting started on Windows

- Installation on Windows
  - Windows Executable
  - Runtime Libraries
- SWIG Windows Examples
  - Instructions for using the Examples with Visual Studio
    - Python
    - TCL
    - Perl
    - Java
    - Ruby
    - C#
  - Instructions for using the Examples with other compilers
- SWIG on Cygwin and MinGW
  - Building swig.exe on Windows
    - Building swig.exe using MinGW and MSYS
    - Building swig.exe using Cygwin
    - Building swig.exe alternatives
  - Running the examples on Windows using Cygwin

This chapter describes SWIG usage on Microsoft Windows. Installing SWIG and running the examples is covered as well as building the SWIG executable and runtime libraries. Usage within the Unix like environments MinGW and Cygwin is also detailed.

## 2.1 Installation on Windows

SWIG does not come with the usual Windows type installation program, however it is quite easy to get started. The main steps are:

- Download the swigwin zip package from the SWIG website and unzip into a directory. This is all that needs downloading for the Windows platform.
- Set environment variables as described in the SWIG Windows Examples section in order to run examples using Visual C++.

### 2.1.1 Windows Executable

The swigwin distribution contains the SWIG Windows executable, swig.exe, which will run on 32 bit versions of Windows, ie Windows 95/98/ME/NT/2000/XP. If you want to build your own swig.exe have a look at Building swig.exe on Windows.

### 2.1.2 Runtime Libraries

The vast majority of users do not use the SWIG runtime libraries so feel free to ignore this section. The Advanced Topics chapter has further details on the runtime libraries.

The runtime libraries which can be built have a Visual C++ project file (.dsp) file in the top level Runtime directory. Before starting Visual C++, set the environment variables for your target language as described in the SWIG Windows Examples section. Next start Visual C++ and load the project file for your target language. Visual Studio will create a workspace file for you. Ensure the Release build is selected then do a Rebuild All from the Build menu; the required environment variables are displayed with their current values. If all goes well the runtime library dll will appear in the Runtime directory.

Whenever using the runtime library, link against the `.lib` file which also is generated into this directory. Ensure the dll is in the Windows path when executing your program requiring the dll.

Note that the runtime libraries can also be built on Cygwin and is automatically done as part of the general build process on Cygwin.

## 2.2 SWIG Windows Examples

Using Microsoft Visual C++ is the most common approach to compiling and linking SWIG's output. The Examples directory has a few Visual C++ project files (.dsp files). These were produced by Visual C++ 6, although they should also work in Visual C++ 5. Later versions of Visual Studio should also be able to open and convert these project files. The C# examples come with .NET 2003 solution (.sln) and project files instead of Visual C++ 6 project files. The project files have been set up to execute SWIG in a custom build rule for the SWIG interface (.i) file. Alternatively run the examples using Cygwin.

More information on each of the examples is available with the examples distributed with SWIG (Examples/index.html).

### 2.2.1 Instructions for using the Examples with Visual Studio

Ensure the SWIG executable is as supplied in the SWIG root directory in order for the examples to work. Most languages require some environment variables to be set before running Visual C++. Note that Visual C++ must be re-started to pick up any changes in environment variables. Open up an example .dsp file, Visual C++ will create a workspace for you (.dsw file). Ensure the Release build is selected then do a Rebuild All from the Build menu. The required environment variables are displayed with their current values.

The list of required environment variables for each module language is also listed below. They are usually set from the Control Panel and System properties, but this depends on which flavour of Windows you are running. If you don't want to use environment variables then change all occurences of the environment variables in the .dsp files with hard coded values. If you are interested in how the project files are set up there is explanatory information in some of the language module's documentation.

#### 2.2.1.1 Python

`PYTHON_INCLUDE` : Set this to the directory that contains python.h
`PYTHON_LIB` : Set this to the python library including path for linking

Example using Python 2.1.1:
`PYTHON_INCLUDE: d:\python21\include
PYTHON_LIB: d:\python21\libs\python21.lib
`

#### 2.2.1.2 TCL

`TCL_INCLUDE` : Set this to the directory containing tcl.h
`TCL_LIB` : Set this to the TCL library including path for linking

Example using ActiveTcl 8.3.3.3
`TCL_INCLUDE: d:\tcl\include
TCL_LIB: d:\tcl\lib\tcl83.lib
`

#### 2.2.1.3 Perl

`PERL5_INCLUDE` : Set this to the directory containing perl.h
`PERL5_LIB` : Set this to the Perl library including path for linking

Example using nsPerl 5.004_04:

`PERL5_INCLUDE: D:\nsPerl5.004_04\lib\CORE
PERL5_LIB: D:\nsPerl5.004_04\lib\CORE\perl.lib
`

#### 2.2.1.4 Java

`JAVA_INCLUDE` : Set this to the directory containing jni.h
`JAVA_BIN` : Set this to the bin directory containing javac.exe

Example using JDK1.3:
`JAVA_INCLUDE: d:\jdk1.3\include
JAVA_BIN: d:\jdk1.3\bin
`

#### 2.2.1.5 Ruby

`RUBY_INCLUDE` : Set this to the directory containing ruby.h
`RUBY_LIB` : Set this to the ruby library including path for linking

Example using Ruby 1.6.4:
`RUBY_INCLUDE: D:\ruby\lib\ruby\1.6\i586-mswin32
RUBY_LIB: D:\ruby\lib\mswin32-ruby16.lib
`

#### 2.2.1.6 C#

The C# examples do not require any environment variables to be set as a C# project file is included. Just open up the .sln solution file in Visual Studio .NET 2003 and do a Rebuild All from the Build menu. The accompanying C# and C++ project file are automatically used by the solution file.

### 2.2.2 Instructions for using the Examples with other compilers

If you do not have access to Visual C++ you will have to set up project files / Makefiles for your chosen compiler. There is a section in each of the language modules detailing what needs setting up using Visual C++ which may be of some guidance. Alternatively you may want to use Cygwin as described in the following section.

## 2.3 SWIG on Cygwin and MinGW

SWIG can also be compiled and run using Cygwin or MinGW which provides a Unix like front end to Windows and comes free with gcc, an ANSI C/C++ compiler. However, this is not a recommended approach as the prebuilt executable is supplied.

### 2.3.1 Building swig.exe on Windows

If you want to replicate the build of swig.exe that comes with the download, follow the MinGW instructions below. This is not necessary to use the supplied swig.exe. This information is provided for those that want to modify the SWIG source code in a Windows environment. Normally this is not needed, so most people will want to ignore this section.

#### 2.3.1.1 Building swig.exe using MinGW and MSYS

- Install MinGW and MSYS from the MinGW site. This provides a Unix environment on Windows.
- Follow the usual Unix instructions in the README file in the SWIG root directory to build swig.exe from the MinGW command prompt.

#### 2.3.1.2 Building swig.exe using Cygwin

Note that SWIG can also be built using Cygwin. However, the SWIG will then require the Cygwin DLL when executing. Follow the Unix instructions in the README file in the SWIG root directory. The runtime libraries are also built as part of this build process. Note that the Cygwin environment will also allow one to regenerate the autotool generated files which are supplied with the release distribution. These files are generated using the `autogen.sh` script and will only need regenerating in circumstances such as changing the build system.

#### 2.3.1.3 Building swig.exe alternatives

If you don't want to install Cygwin or MinGW, use a different compiler to build SWIG. For example, all the source code files can be added to a Visual C++ project file in order to build swig.exe from the Visual C++ IDE.

### 2.3.2 Running the examples on Windows using Cygwin

The examples and test-suite work as successfully on Cygwin as on any other Unix operating system. The modules which are known to work are Python, Tcl, Perl, Ruby, Java and C#. Follow the Unix instructions in the README file in the SWIG root directory to build the examples.