Skip to content

Building External Libraries

Addison Elliott edited this page Jul 25, 2017 · 1 revision

Table of Contents

Overview

This guide will explain how to install the required external libraries necessary to build VisceralFatValidation. Qt Creator is a development framework that utilizes the compiler to compile the application. Thus, it is important to know what compiler Qt Creator is using to build the application so the libraries can be built with the same compiler.

To find this out, open Qt Creator and select Tools->Options->Build & Run->Kits tab. The kits listed are the possible compilers that Qt Creator can use to compile the application. Pick which compiler you would like to use and stick with it when compiling these libraries.

Here are some examples (many more options besides these) of compilers for each OS:

  • Windows
    • Visual Studio (MSVC) 2015 x86 (32-bit)
    • Visual Studio (MSVC) 2015 x64 (64-bit)
    • MinGW 32-bit
    • MinGW 64-bit
  • Linux
    • GCC 32-bit
    • GCC 64-bit
  • Mac OS X
    • clang 32-bit
    • clang 64-bit

If prebuilt binaries are used, then there is no need to build the libraries and this guide can be skipped. Prebuilt binaries must have been built using the same compiler as you are building the application with. This means the following must be true:

  • Same compiler is used (MinGW, MSVC 2015, clang, GCC)
  • Same architecture is used (e.g. 32-bit or 64-bit)

VisceralFatValidation requires the following external libraries:

  • Zlib 1.2.0 or greater
  • NIFTIclib 2.0.0 or greater
  • OpenCV 3.1.0 or greater

Installing CMake

Luckily, the installation of each of the libraries is made simple with the aid of CMake. This is tool that helps to build software on a variety of platforms and compilers.

If you do not have CMake already, download and install the latest version before continuing.

Note: This process shown for compiling projects using CMake is standard regardless of the project. Therefore, if you run across projects in your future that use CMake, then the fundamentals of this guide still apply.

Building zlib

  1. Download latest zlib source code from here

    Zlib Download Page

  2. Extract the contents of the ZIP folder to somewhere

    • For example, I have a folder named DevelLibs in Documents for all libraries I build
  3. Open CMake if it is not already open

    • On Windows, the application name may be cmake-gui
  4. Set the source code directory to the location of the extracted source code from Step 1

  5. Set the build directory to an empty folder where the binaries will be built

    • Typical location is the folder 'build' within the source folder (e.g. zlib-1.2.11/build)

    Zlib CMake

  6. Click Configure

    • Note: A message box may appear asking if you want to create the build directory. Select Yes
  7. Next, a dialog box should appear requesting the desired generator to use to build the library. Select the compiler you want to use to build this library. The appropriate compiler was discussed above

    • For OS X and Linux, set to Unix Makefiles
    • For Windows, select the compiler you want to use to build the library
      • Visual Studio has 32-bit and 64-bit compilers so make sure to select the desired one (Win64 is for 64-bit)

    Zlib CMake

  8. If everything was successful, then you should have a bunch of red background items in a list. The first time you configure the build, the options will appear red to signify they are new and need to be reviewed. These are the configuration options for building the application. Many of these can be left untouched but I will cover the more common ones you should check

    You may need to check the "Advanced" option to see some of these values

    • INSTALL_* - This specifies the directories which the respective data will be installed
      • For Windows, it is usually best to set this to be in the build directory
      • For Linux/OSX, this should be located in /usr/local/{bin/lib/include}
  9. Once the configuration values are set to desired values, click Configure again to finalize any changes

    Zlib CMake

  10. Click Generate and this will create the project that is used to make the libraries

    Zlib CMake

  11. This part is compiler specific but I will give a general outline of what needs to be done. CMake generated a build file depending on your selected compiler and now the goal is to use that build file to generate the binaries

    • If building on OS X or Linux or using MinGW on Windows, you will need to open a terminal window and go to the build directory (using cd). Afterwards, run the following commands

      With sudo (used on OS X and some distributions of Linux)

      sudo make
      sudo make install
      

      Without sudo

      make
      make install
      

      Make generates the binaries and the install command will then install them in the specified directories (INSTALL_* directories)

      It may be necessary to prefix the commands with sudo depending on your OS for permission reasons

    • If building on Windows with Visual Studio, go to the build directory and open the project solution file (.sln) in Visual Studio

      • Click Build -> Build Solution. This should build the project
      • Afterwards, go to the INSTALL project in Visual Studio, right-click and select Build
      • This will install the libraries in the location specified (INSTALL_* directories)

    Zlib CMake Zlib CMake

  12. Zlib is a common package included by project and luckily, CMake makes it extremely easy to find packages on its own. For this to work, I recommend that you add a environmental variable ZLIB_ROOT that points to where the root folder is located. CMake will search for %ZLIB_ROOT%/include, %ZLIB_ROOT%/lib, and %ZLIB_ROOT%/bin to find the package. This tip also applies to other packages commonly implemented; just define an environmental variable _ROOT and it will search for it.

Building NIFTIclib

  1. Download latest NIFTIclib source code from here

  2. Extract the contents of the ZIP folder to somewhere

    • For example, I have a folder named DevelLibs in Documents for all libraries I build
  3. Open CMake if it is not already open

    • On Windows, the application name may be cmake-gui
  4. Set the source code directory to the location of the extracted source code from Step 1

  5. Set the build directory to an empty folder where the binaries will be built

    • Typical location is the folder 'build' within the source folder (e.g. niftilib/build)
  6. Click Configure

    • Note: A message box may appear asking if you want to create the build directory. Select Yes
  7. Next, a dialog box should appear requesting the desired generator to use to build the library. Select the compiler you want to use to build this library. The appropriate compiler was discussed above

    • For OS X and Linux, set to Unix Makefiles
    • For Windows, select the compiler you want to use to build the library
      • Visual Studio has 32-bit and 64-bit compilers so make sure to select the desired one (Win64 is for 64-bit)

    NIFTI CMake

  8. If everything was successful, then you should have a bunch of red background items in a list. The first time you configure the build, the options will appear red to signify they are new and need to be reviewed. These are the configuration options for building the application. Many of these can be left untouched but I will cover the more common ones you should check

    You may need to check the "Advanced" option to see some of these values

    • BUILD_SHARED_LIBS - If checked, this creates a shared library. Otherwise, the library is linked statically.
      • If unsure, select this to build shared libs
    • CMAKE_INSTALL_PREFIX - This specifies the directories which the respective data will be installed
      • On Linux/OS X, the location should be /usr/local
      • For Windows, it is usually best to set this to be in the build directory
    • ZLIB_INCLUDE_DIR and ZLIB_LIBRARY_DEBUG or ZLIB_LIBRARY_RELEASE - Zlib is a third-party library very commonly used for compression of data. It is used in NIFTIclib for extracting GZ files. CMake will make an attempt to search for a Zlib library but it may be unsuccessful. You will want to make sure that the INCLUDE_DIR is set to a valid directory and that either ZLIB_LIBRARY_DEBUG or ZLIB_LIBRARY_RELEASE is set to a valid library file (file extensions: *.dylib, *.lib, *.so, *.a)
      • On Linux/OS X, these should be set to /usr/local/{bin/lib/include}
      • On Windows, these should be set to the directory specified (INSTALL_*) when you built the Zlib library above

    NIFTI CMake

  9. Once the configuration values are set to desired values, click Configure again to finalize any changes

  10. Click Generate and this will create the project that is used to make the libraries

    NIFTI CMake

  11. This part is compiler specific but I will give a general outline of what needs to be done. CMake generated a build file depending on your selected compiler and now the goal is to use that build file to generate the binaries

    • If building on OS X or Linux or using MinGW on Windows, you will need to open a terminal window and go to the build directory (using cd). Afterwards, run the following commands

      With sudo (used on OS X and some distributions of Linux)

      sudo make
      sudo make install
      

      Without sudo

      make
      make install
      

      Make generates the binaries and the install command will then install them in the specified directories (INSTALL_* directories)

      It may be necessary to prefix the commands with sudo depending on your OS for permission reasons

    • If building on Windows with Visual Studio, go to the build directory and open the project solution file (.sln) in Visual Studio

      • Click Build -> Build Solution. This should build the project
      • Afterwards, go to the INSTALL project in Visual Studio, right-click and select Build
      • This will install the libraries in the location specified (CMAKE_INSTALL_PREFIX directories)

Building OpenCV

  1. Download latest OpenCV source code from here

  2. Extract the contents of the ZIP folder to somewhere

    • For example, I have a folder named DevelLibs in Documents for all libraries I build
  3. Open CMake if it is not already open

    • On Windows, the application name may be cmake-gui
  4. Set the source code directory to the location of the extracted source code from Step 1

  5. Set the build directory to an empty folder where the binaries will be built

    • Typical location is the folder 'build' within the source folder (e.g opencv/build)
  6. Click Configure

    • Note: A message box may appear asking if you want to create the build directory. Select Yes
  7. Next, a dialog box should appear requesting the desired generator to use to build the library. Select the compiler you want to use to build this library. The appropriate compiler was discussed above

    • For OS X and Linux, set to Unix Makefiles
    • For Windows, select the compiler you want to use to build the library
      • Visual Studio has 32-bit and 64-bit compilers so make sure to select the desired one (Win64 is for 64-bit)

    OpenCV CMake

  8. If everything was successful, then you should have a bunch of red background items in a list. The first time you configure the build, the options will appear red to signify they are new and need to be reviewed. These are the configuration options for building the application. Many of these can be left untouched but I will cover the more common ones you should check

    You may need to check the "Advanced" option to see some of these values

    • CMAKE_INSTALL_PREFIX - This specifies the directories which the respective data will be installed
      • On Linux/OS X, the location should be /usr/local
      • For Windows, it is usually best to set this to be in the build directory
    • It is advised you go through the configuration and validate that CMake found the external libraries you desire. OpenCV uses third-party libraries to speed up their code such as OpenCL, TBB, CUDA, etc. If these libraries are not found, OpenCV will still compile fine but will not have the features these libraries bring. Research the libraries and decide if you want to install them.

    NIFTI CMake

  9. Once the configuration values are set to desired values, click Configure again to finalize any changes

    NIFTI CMake

  10. Click Generate and this will create the project that is used to make the libraries

  11. This part is compiler specific but I will give a general outline of what needs to be done. CMake generated a build file depending on your selected compiler and now the goal is to use that build file to generate the binaries

    • If building on OS X or Linux or using MinGW on Windows, you will need to open a terminal window and go to the build directory (using cd). Afterwards, run the following commands

      With sudo (used on OS X and some distributions of Linux)

      sudo make
      sudo make install
      

      Without sudo

      make
      make install
      

      Make generates the binaries and the install command will then install them in the specified directories (INSTALL_* directories)

      It may be necessary to prefix the commands with sudo depending on your OS for permission reasons

    • If building on Windows with Visual Studio, go to the build directory and open the project solution file (.sln) in Visual Studio

      • Click Build -> Build Solution. This should build the project
      • Afterwards, go to the INSTALL project in Visual Studio, right-click and select Build
      • This will install the libraries in the location specified (CMAKE_INSTALL_PREFIX directories)

For more information on installing OpenCV, visit here

Output of Build

You should have three things that come out of each build:

  1. Header files. These are typical in a include folder and contain .h files. This tells the compiler what functions/variables are available. The actual code or variable data is not stored within this file typically.
  2. Library files.
    • Statically linked - This will contain the code that goes with the declarations in the header file. This code will be compiled into your application. (.so, .a)
    • Dynamically linked - Since the code is contained in the DLLs, this is a small file that tells the linker the function/variable declarations will be in a DLL and which DLL will be needed. (.dylib, .lib, .so)
  3. DLLs (Shared linking only). If you linked the library dynamically, then DLLs should be included which contain the bulk of the library. This is where the actual code is stored. These will need to be distributed alongside your program if you use shared libraries. (.dll)

Take note of where each of these things were installed at. If on Linux or OS X, they should all be within /usr/local/{bin/lib/include}. However, on Windows, there is not a single directory to store all binaries and so you must manually keep track of them.

The application uses the custom.pro file to search for the external libraries and dependencies. You must alter custom.pro to match the location of the libraries.