# OpenSCAD User Manual/Building on Windows

This is a set of instructions for building OpenSCAD with the Microsoft Visual C++ compilers. It has not been used since circa 2012 and is unlikely to work properly. It is maintained here for historical reference purposes.
A newer build is being attempted with the Msys2 system, please see http://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Building_on_Microsoft_Windows
This MSVC build is as static as reasonable, with no external DLL dependencies that are not shipped with Windows
Note: It was last tested on the Dec 2011 build. Newer checkouts of OpenSCAD may not build correctly or require extensive modification to compile under MSVC. OpenSCAD releases of 2012 were typically cross-compiled from linux using the Mingw & MXE system. See Cross-compiling for Windows on Linux or Mac OS X.

## Downloads

start by downloading:
* Visual Studio Express http://download.microsoft.com/download/E/8/E/E8EEB394-7F42-4963-A2D8-29559B738298/VS2008ExpressWithSP1ENUX1504728.iso
* QT (for vs2008) http://get.qt.nokia.com/qt/source/qt-win-opensource-4.7.2-vs2008.exe
* git http://msysgit.googlecode.com/files/Git-1.7.4-preview20110204.exe
* glew https://sourceforge.net/projects/glew/files/glew/1.5.8/glew-1.5.8-win32.zip/download
* cmake http://www.cmake.org/files/v2.8/cmake-2.8.4-win32-x86.exe
* boost http://www.boostpro.com/download/boost_1_46_1_setup.exe
* cgal https://gforge.inria.fr/frs/download.php/27647/CGAL-3.7-Setup.exe
* OpenCSG http://www.opencsg.org/OpenCSG-1.3.2.tar.gz
* eigen2 http://bitbucket.org/eigen/eigen/get/2.0.15.zip
* gmp/mpfr http://holoborodko.com/pavel/downloads/win32_gmp_mpfr.zip
* MinGW http://netcologne.dl.sourceforge.net/project/mingw/Automated%20MinGW%20Installer/mingw-get-inst/mingw-get-inst-20110316/mingw-get-inst-20110316.exe

## Installing

* Install Visual Studio
** No need for siverlight or mssql express
** You can use a virtual-CD program like MagicDisc to mount the ISO file and install without using a CD
* Install QT
** Install to default location C:\Qt\4.7.2\
* Install Git
** Click Run Git and included Unix tools from the Windows Command Prompt despite the big red letters warning you not to.
* Install Cmake
** Check the 'Add cmake to the system path for the current user' checkbox
** Install to default location C:\Program Files\CMake 2.8
* Install Boost
** Select the VC++ 9.0 vs2008 radio
** Check the 'multithreaded static runtime' checkbox only
** Install into C:\boost_1_46_1\
* Install CGAL
** Note - CGAL 3.9 fixes several bugs in earlier versions of CGAL, but CGAL 3.9 does not compile under MSVC without extensive patching. Please keep that in mind when compiling OpenSCAD with MSVC - there may be bugs due to the outdated version of CGAL required to use MSVC.
** Note its not a binary distribution, just an installer that installs the source.
** No need for CGAL Examples and Demos
** Make sure mpfr and gmp precompiled libs is checked
** The installer wants you to put this in C:\Program Files\CGAL-3.7\ I used C:\CGAL-3.7\
** Make sure CGAL_DIR environment checked.
* Install MinGW
** Make sure you select the MSYS Basic System under components
* Extract downloaded win32_gmp_mpfr.zip file to C:\win32_gmp_mpfr\
* Replace the mpfr and gmp .h files in CGAL with the ones from win32_gmp_mpfr
** Delete, or move to a temp folder, all files in CGAL-3.7\auxiliary\gmp\include folder
** Copy all the .h files in C:\win32_gmp_mpfr\gmp\Win32\Release to CGAL-3.7\auxiliary\gmp\include
** Copy all the .h files in C:\win32_gmp_mpfr\mpfr\Win32\Release to CGAL-3.7\auxiliary\gmp\include
* Replace the mpfr and gmp libs in CGAL with the ones from win32_gmp_mpfr
** Delete, or move to a temp folder, all (06/20/2011 libmpfr-4.lib is needed 7/19/11 - i didnt need it) files in CGAL-3.7\auxiliary\gmp\lib folder.
** Copy C:\win32_gmp_mpfr\gmp\Win32\Release\gmp.lib to CGAL-3.7\auxiliary\gmp\lib
** Copy C:\win32_gmp_mpfr\mpfr\Win32\Release\mpfr.lib to CGAL-3.7\auxiliary\gmp\lib
** Go into CGAL-3.7\auxiliary\gmp\lib and copy gmp.lib to gmp-vc90-mt-s.lib, and mpfr.lib to mpfr-vc90-mt-s.lib (so the linker can find them in the final link of openscad.exe)
To get OpenSCAD source code:
* Open "Git Bash" (or MingW Shell) (the installer may have put a shortcut on your desktop). This launches a command line window.
* Type cd c: to change the current directory.
* Type git clone git://github.com/openscad/openscad.git This puts OpenSCAD source into C:\openscad\
Where to put other files:
I put all the dependencies in C:\ so for example,
* C:\eigen2\
* C:\glew-1.5.8\
* C:\OpenCSG-1.3.2\
.tgz can be extracted with tar -zxvf from the MingW shell, or Windows tools like 7-zip. Rename and move sub-directories if needed. I.e eigen-eigen-0938af7840b0 should become c:\eigen2, with the files like COPYING and CMakeLists.txt directly under it. c:\glew-1.5.8 should have 'include' and 'lib' directly under it.

## Compiling Dependencies

For compilation I use the QT Development Command Prompt
Start->Program Files->Qt by Nokia v4.7.2 (VS2008 OpenSource)->QT 4.7.2 Command Prompt

### Qt

Qt needs to be recompiled to get a static C runtime build. To do so, open the command prompt and do:
Configure takes several minutes to finish processing. After it is done, open up the file Qt\4.7.2\mkspecs\win32-msvc2008\qmake.conf and replace every instance of -MD with -MT. Then:
This takes a very, very long time. Have a nap. Get something to eat. On a Pentium 4, 2.8 GHz CPU with 1 Gigabyte RAM, Windows XP, it took more than 7 hours, (that was with -O2 turned off)

### CGAL

Now edit the CMakeCache.txt file. Replace every instance of /MD with /MT . Now, look for a line like this:
CMAKE_BUILD_TYPE:STRING=Debug
Change Debug to Release. Now re-run cmake
It should scroll by, watch for lines saying "--Building static libraries" and "--Build type: Release" to confirm the proper settings. Also look for /MT in the CXXFLAGS line. When it's done, you can do the build:
You should now have a CGAL-vc90-mt-s.lib file under C:\CGAL-3.7\lib . If not, see Troubleshooting, below.

### OpenCSG

Launch Visual Express.
* Manually step through project upgrade wizard
* Make sure the runtime library settings for all projects is for Release (not Debug)
** Click Build/Configuration Manager
** Select "Release" from "Configuration:" drop down menu
** Hit Close
* Make sure the runtime library setting for OpenCSG project is set to multi-threaded static
** Open the OpenCSG project properties by clicking menu item "Project->OpenCSG Properties" (might be just "Properties")
** Make sure it says "Active(Release)" in the "Configuration:" drop down menu
** Click 'Configuration Properties -> C/C++ -> Code Generation'
** Make sure "Runtime Library" is set to "Multi-threaded (/MT)"
** Click hit OK
* Make sure the runtime library setting for glew_static project is set to multi-threaded static
** In "Solution Explorer - OpenCSG" pane click "glew_static" project
** Open the OpenCSG project properties by clicking menu item "Project->OpenCSG Properties" (might be just "Properties")
** Make sure it says "Active(Release)" in the "Configuration:" drop down menu
** Click C/C++ -> Code Generation
** Make sure "Runtime Library" is set to "Multi-threaded (/MT)"
** Click hit OK
* Close Visual Express saving changes
Build OpenCSG library. You can use the GUI Build/Build menu (the Examples project might fail, but glew and OpenCSG should succeed). Alternatively you can use the command line:
The cmd /c bit is needed otherwise you return to the shell immediately and must wait for the build process to complete (there is no indication that this is happening apart from viewing in task manager).

### OpenSCAD

* Bison/Flex: Open the mingw shell and type mingw-get install msys-bison. Then do the same for flex: mingw-get install msys-flex
* Open the QT Shell, and copy/paste the following commands
Wait for the nmake to end. There are usually a lot of non-fatal warnings about the linker. On success, an openscad.exe file appears in the release folder. Enjoy.

## Building an installer

* Download and install NSIS from http://nsis.sourceforge.net/Download
* Put the FileAssociation.nsh macro from http://nsis.sourceforge.net/File_Association in the NSIS Include directory, C:\Program Files\NSIS\Include
* Run 'git submodule init' and 'git submodule update' to download the MCAD system (https://github.com/elmom/MCAD) into the openscad/libraries folder.
* Copy the OpenSCAD "libraries" and "examples" directory into the "release" directory
* Copy OpenSCAD's "scripts/installer.nsi" to the "release" directory.
* Right-click on the file and compile it with NSIS. It produces a nice, easy installer. Enjoy.

## Compiling the regression tests

* Follow all the above steps, build openscad, run it, and test that it basically works.
* Install Python 2.x (not 3.x) from http://www.python.org
* Install Imagemagick from http://www.imagemagick.org
* read openscad\docs\testing.txt
* Go into your QT shell
* This should produce a number of test .exe files in your directory. Now run
If you have link problems, see Troubleshooting, below.

## Troubleshooting

Linker errors
If you have errors during linking, the first step is to improve debug logging, and redirect to a file. Open Openscad.pro and uncomment this line:
Now rerun
You can use a program like 'less' (search with '/') or wordpad to review the log.
To debug these errors, you must understand basics about Windows linking. Windows links to its standard C library with basic C functions like malloc. But there are four different ways to do this, as follows:
All of the libraries that are link together in a final executable must be compiled with the same type of linking to the standard C library. Otherwise, you get link errors like, "LNK2005 - XXX is already defined in YYY". But how can you track down which library wasn't linked properly? 1. Look at the log, and 2. dumpbin.exe
dumpbin.exe
dumpbin.exe can help you determine what type of linking your .lib or .obj files were created with. For example, dumpbin.exe /all CGAL.lib | find /i "DEFAULTLIB" gives you a list of DEFAULTLIB symbols inside of CGAL.lib. Look for LIBCMT, LIBCMTD, MSVCRT, or MSVCRTD. That tells you, according to the above table, whether it was built Static Release, Static Debug, DLL Release, or DLL Debug. (DLL, of course means Dynamic Link Library in this conversation.) This can help you track down, for example, linker errors about conflicting symbols in LIBCMT and LIBCMTD.
dumpbin.exe can also help you understand errors involving unresolved external symbols. For example, if you get an error about unresolved external symbol ___GLEW_NV_occlusion_query, but your VERBOSE error log says the program linked in glew32.lib, then you can dumpbin.exe /all glew32.lib | find /i "occlusion" to see if the symbol is actually there. You may see a mangled symbol, with __impl, which gives you another clue with which you can google. In this particular example, glew32s.lib (s=static) should have been linked instead of glew32.lib.

### CGAL

CGAL-vc90-mt-s.lib
After compilation, it is possible that you might get a file named CGAL-vc90-mt.lib or CGAL-vc90-mt-gd.lib instead of CGAL-vc90-mt-s.lib. There are many possibilities: you accidentally built the wrong version, or you may have built the right version and VCExpress named it wrong. To double check, and fix the problem, you can do the following:
If this shows lines referencing LIBCMTD, MSVCRT, or MSVCRTD then you accidentally built the debug and/or dynamic version, and you need to clean the build, and try to build again with proper settings to get the multi-threaded static release version. However, if it just says LIBCMT, then you are probably OK. Look for another line saying DEFAULTLIB:CGAL-vc90-mt-s. If it is there, then you can probably just rename the file and have it work.
Visual Studio build
You can build CGAL using the GUI of visual studio, as an alternative to nmake. You have to use an alternate cmake syntax. Type 'cmake' by itself and it gives you a list of 'generators' that are valid for your machine; for example Visual Studio Express is cmake -G"Visual Studio 9 2008" .. That should get you a working .sln (solution) file.
Then run this:
Modify the build configure target to Release (not Debug) and change the properties of the projects to be '/MT' multithreaded static builds. This is the similar procedure used to build OpenCSG, so refer to those instructions above for more detail.
Note for Unix users
The 'MingW Shell' (Start/Programs) provide tools like bash, sed, grep, vi, tar, &c. The C:\ drive is under '/c/'. MingW has packages, for example: mingw-get install msys-unzip downloads and installs the 'unzip' program. Git contains some programs by default, like perl. The windows command shell has cut/paste - hit alt-space. You can also change the scrollback buffer settings.

### References

* Windows Building, OpenSCAD mailing list, 2011 May.
* C Run-Time Libraries linking, Microsoft.com for Visual Studio 8 (The older manual is good too, here)
* old nabble on _isatty, flex
* Windows vs. Unix: Linking dynamic load modules by Chris Phoenix
* Static linking in CMAKE under MS Visual C (cmake.org)
* __imp , declspec(dllimport), and unresolved references (stackoverflow.com)