2

So, I ran into the problem of getting VTK (Visualization ToolKit) to work on Mac OSX, especially getting it to work in XCode.

(I got it to work and will post the answer to this question below)

Current setup as of October 2015:

  • MacBook Pro 13" (Early 2015) 2,7 GHz i5, 8GB Memory, Intel Iris Graphics 6100 (1,5GB)
  • OS X El Capitan
  • XCode Version 7.0.1
  • VTK Version 6.1
Scarysize
  • 4,131
  • 25
  • 37

1 Answers1

3

I hope this will help everyone having similiar problems with the whole process of configuring and building VTK on OSX and setting up XCode correctly to successfully build VTK projects. Also I hope that I won´t forget anything relevant. If you have questions or see some errors I made, please comment/answer.

I will go through the whole process from building VTK to setting up XCode step by step. I did A LOT of googling and trying out different things. Also I will try to provide some screenshots to help clarify things, especially for people new to XCode, like me. As far as I remember and bookmarked them, I will provide the sources helping me with setup, too.

Download, Build & Install

This will mostly include steps from this stackoverflow thread: Installing VTK 6.1 for OSX 10.8 with Cocoa support

  1. Clone VTK sources from Github to a directory of your choice

    cd /Users/you/

    git clone https://github.com/Kitware/VTK.git

    cd VTK

    git checkout tags/v6.1.0

  2. Make a build directory

    mkdir VTKBuild

    cd VTKBuild

    If you have no idea what "building" means in a context of C/C++, I suggest you do some further reading: Building C/C++: what really happens and why does it take so long

  3. Editing the CMakeLists.txt

    In your downloaded sources directory (not the /VTKBuild directory), there should be a file "CMakeLists.txt". Open it up in a text editor and search for the lines:

    IF(APPLE)
     SET(VTK_OBJCXX_FLAGS_DEFAULT "-fobjc-gc")
     SET(VTK_REQUIRED_OBJCXX_FLAGS ${VTK_OBJCXX_FLAGS_DEFAULT} CACHE STRING "Extra flags for Objective-C++ compilation")
     MARK_AS_ADVANCED(VTK_REQUIRED_OBJCXX_FLAGS)
    ENDIF()
    

    Delete or comment these lines. If you don´t find them to begin with, I think you should be good to go, as this may be fixed in newer versions.

    Source: [Solved] Build Qt 5.2.1 + VTK 6.1.0 + CMake 2.8.12.2

  4. Run cmake script

    Change in your /VTKBuild directory. You are now inside /Users/you/VTK/VTKBuild

    cmake ..

  5. Edit CMakeCache.txt

    Now we need to edit a few lines in the generated CMakeCache.txt (in your /VTKBuild directory).

    CMAKE_INSTALL_PREFIX. So make sure to use "/Users/you/" instead of "~":
    
    CMAKE_INSTALL_PREFIX:PATH=/Users/you/VTK/VTKBuild
    
    BUILD_SHARED_LIBS:BOOL=OFF
    
    CMAKE_BUILD_TYPE:STRING=Debug
    
    VTK_USE_SYSTEM_ZLIB:BOOL=ON
    
    CMAKE_OSX_ARCHITECTURES:STRING=i386;x86_64
    
    VTK_USE_COCOA:BOOL=ON
    
    VTK_USE_CARBON:BOOL=OFF
    
    CMAKE_OSX_SYSROOT:STRING=/Applications/XCode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk
    

    The value of the last config line may vary if you follow this tutorial having a different OS X version than me. In my CMakeCache.txt it was already configured to the correct value.

    Additional Sources: Part One: Build VTK libraries on Mac

  6. Compile the VTK project

    This may take long, you can accelerate the process by adding the -j ( +number of cores) parameter. This tells your compiler to use more than one core to compile the files and should speed things up. (You are still in the VTKBuild/ directory).

    make

  7. Now copy the header files and libraries to an include directory

    make install

    This should create a /lib and a /include directory in your /VTKBuild directory. We will later reference to them.

    This should do it for the building part. VTK is now correctly installed on your system.

Setting up the XCode project

Source: Part Three: Create Xcode by hand

As a general advise. If you configure some paths, try to use absolute paths. Especially when I write something like '.../VTKBuild', replace '...' with the correct part of the path.

  1. Create a new project

    Launch XCode and create a new project (File -> New Project). Choose OS X -> Application -> Comand Line Tool. Click 'Next', name it, and select C++ as your 'Language'. Choose a location of your desire and finish with 'Create'.

  2. Added necessary frameworks

    Select your project in the left bar, then in the center navigate to 'Build Phases'. This should give you this view:

    Adding libraries in XCode

    Now expand the 'Link Binary with Libraries' row. On a fresh project this should´t contain anything. We now need to link the OpenGL, Cocoa und IOKit frameworks. You do this by hitting the + button on the bottom. This will open a dialogue with some frameworks. Just search for the aformentioned frameworks and hit 'Add'.

    Also we need to add the zlib library. Again hit the + button, and search for 'libz'. Then add the 'lib.tbd' library. There a multiple versions available, I just added the one without a version number. Others may work as well. This is what it should look like by now:

    added frameworks and zlib

    Now we need to add the VTK library files. These are located in your /VTKBuild/lib directory. They should be named something like 'libvtkXXX.a'. You can either just drag&drop them into the window or select them, like the frameworks on the above step, via the file dialogue: + button -> 'Add Other..' -> Navigate to /VTKBuild/lib -> select all *.a files with Shift+Click/Cmd+Click -> 'Open'. It may take a while until they show up on the interface.

  3. Configure Source Tree

    Open the XCode Preferences (CMD+,). In the top bar navigate to 'Locations' and select 'Source Trees'. Add two rows:

    "Name" - "Display-Name" - "Path"

    vtk-debug-include vtk-debug-include ...VTKBuild/include/vtk-6.1

    vtk-debug-lib vtk-debug-lib ...VTKBuild/lib

  4. Add search paths

    We now need to tell XCode where to look for additional header files (*.h), in order for you to be able to '#include <...>' them in your code.

    For this, again, select your project on the left bar. Navigate to 'Build Setting' and type in the Filter 'search'. There should be a row named 'Search Paths' with sub-rows 'Header Search Paths' and 'Library Search Paths'. The first one needs to be set to '...VTKBuild/include/vtk-6.1', the latter one to '...VTKBuild/lib'.

  5. Define Macros

    If you done everything of the above. A project which includes VTK Header files should build and run. But to run correctly, you may need to define two macros on top of your *.cpp files, BEFORE any #include statements:

    #define vtkRenderingCore_AUTOINIT 4(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingFreeTypeOpenGL,vtkRenderingOpenGL)
    #define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL)
    

    Without these I e.g. had problems with the vtkWindowInteractor not working.

That´s it! Have fun visualizing stuff.

Community
  • 1
  • 1
Scarysize
  • 4,131
  • 25
  • 37