OpenSCAD User Manual/Building on Windows

From Wikibooks, open books for an open world
Jump to: navigation, search

This is a set of instructions for building OpenSCAD with the Microsoft Visual C++ compilers.

The 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[edit]

start by downloading:

Installing[edit]

  • 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 will 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 will put 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[edit]

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[edit]

Qt needs to be recompiled to get a static C runtime build. To do so, open the command prompt and do:

configure -static -platform win32-msvc2008 -no-webkit

Configure will take 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:

nmake

This takes a very, very long time. Have a nap. Get something to eat. On a Pentium 4, 2.8GHZ CPU with 1 Gigabyte RAM, Windows XP, it took more than 7 hours, (that was with -O2 turned off)

CGAL[edit]

cd C:\CGAL-3.7\
set BOOST_ROOT=C:\boost_1_46_1\
cmake .

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

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:

nmake

You should now have a CGAL-vc90-mt-s.lib file under C:\CGAL-3.7\lib . If not, see Troubleshooting, below.

OpenCSG[edit]

Launch Visual Express.

cd C:\OpenCSG-1.3.2
vcexpress OpenCSG.sln
Substitute devenv for vcexpress if you are not using the express version
  • 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:

cmd /c vcexpress OpenCSG.sln /build
Again, substitute devenv if you have the full visual studio

The cmd /c bit is needed otherwise you will be returned to the shell immediately and have to Wait for build process to complete (there will be no indication that this is happening appart from in task manager)

OpenSCAD[edit]

  • 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
cd C:\openscad
set INCLUDE=%INCLUDE%C:\CGAL-3.7\include;C:\CGAL-3.7\auxiliary\gmp\include;
set INCLUDE=%INCLUDE%C:\boost_1_46_1;C:\glew-1.5.8\include;C:\OpenCSG-1.3.2\include;C:\eigen2
set LIB=%LIB%C:\CGAL-3.7\lib;C:\CGAL-3.7\auxiliary\gmp\lib;
set LIB=%LIB%C:\boost_1_46_1\lib;C:\glew-1.5.8\lib;C:\OpenCSG-1.3.2\lib
qmake
nmake -f Makefile.Release

Wait for the nmake to end. There are usually a lot of non-fatal warnings about the linker. On success, there will be an openscad.exe file in the release folder. Enjoy.

Building an installer[edit]

  • 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 will spit out a nice, easy installer. Enjoy.

Compiling the regression tests[edit]

  • 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
set PATH=%PATH%;C:\Python27 (or your version of python)
cd c:\openscad\tests\
cmake . -DCMAKE_BUILD_TYPE=Release
Edit the CMakeCache.txt file, search/replace /MD to /MT
cmake .
nmake -f Makefile
  • This should produce a number of test .exe files in your directory. Now run
ctest

If you have link problems, see Troubleshooting, below.

Troubleshooting[edit]

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:

 QMAKE_LFLAGS   += -VERBOSE

Now rerun

 nmake -f Makefile.Release > log.txt

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:

compiler switch - type - linked runtime C library
/MT - Multithreaded static Release - link to LIBCMT.lib 
/MTd - Multithreaded static Debug - link to LIBCMTD.lib 
/MD - Multithreaded DLL Release - link to MSVCRT.lib (which itself helps link to the DLL)
/MDd - Multithreaded DLL Debug - link to MSVCRTD.lib (which itself helps link to the DLL)

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" will give you a list of DEFAULTLIB symbols inside of CGAL.lib. Look for LIBCMT, LIBCMTD, MSVCRT, or MSVCRTD. That will tell 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[edit]

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:

cd C:\CGAL-3.7\lib
dumpbin /all CGAL-vc90-mt.lib | find /i "DEFAULTLIB"
(if you have mt-gd, use that name instead)

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.

move CGAL-vc90-mt.lib CGAL-vc90-mt-s.lib

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 will give 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:

vcexpress CGAL.sln 

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[edit]