编译vtk 7.0,pcl 1.8.0和itk 1.14.0

ubuntu下编译vtk,pcl和itk

如何在ubuntu16.04下编译vtk7.0,pcl1.8.0和itk4.11(动态链接库,Release版本)?


1. 编译vtk 7.0

下载vtk7.0源码,参考官方的编译vtk指南,大致有以下步骤:

Step 1: 可能需要安装一些依赖,包括x11和opengl

1
2
sudo apt-get install libx11-dev libxext-dev libxtst-dev libxrender-dev libxmu-dev libxmuu-dev # x11
sudo apt-get install build-essential libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev # opengl

Step 2: 安装qt(可选步骤,可跳过),建议使用installer(例如qt-opensource-linux-x64-5.6.2.run)而不是从源码编译

1
2
chmod 777 qt-opensource-linux-x64-5.6.2.run
./qt-opensource-linux-x64-5.6.2.run

选择安装到自定义目录(例如/home/aaron/yalong/software/Qt5.6.2),或者默认目录

Step 3: 使用cmake配置vtk相关编译选项,这里采用cmake-gui进行配置,生成Makefile

首先,安装cmake-gui程序

1
sudo apt-get install cmake-qt-gui

启动cmake-gui图形应用程序后,选择下载的vtk解压的目录(例如/home/aaron/yalong/library/VTK-7.0.0)为源目录,创建一个新的目录(例如/home/aaron/yalong/library/VTK-7.0.0-build-release)作为build目录,这里只编译release版本的动态库。配置cmake时需要调整的项目

  • CMAKE_BUILD_TYPE: Release
  • BUILD_SHARED_LIBS: On

(为了减少编译时间)

  • BUILD_TESTING: Off
  • BUILD_EXAMPLES: Off
  • BUILD_DOCUMENT: Off

如果想启用qt的支持,则勾选VTK_Group_Qt选项,然后Configure。如果有报错,找不到合适的qt,比如可能提示如下:

1
2
3
4
CMake Error at /usr/share/cmake-3.5/Modules/FindQt4.cmake:1326 (message):
Found unsuitable Qt version "" from NOTFOUND, this code requires Qt 4.x
Call Stack (most recent call first):
GUISupport/Qt/CMakeLists.txt:71 (find_package)

那么手工设置cmake中qt相关的项,帮助寻找安装好的qt

  • 设置QT_QMAKE_EXECUTABLE=/home/aaron/yalong/software/Qt5.6.2/5.6/gcc_64/bin/qmake,设置VTK_QT_VERSION=5,点击Configure。接着可能会提示如下错误:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    CMake Error at GUISupport/Qt/CMakeLists.txt:58 (find_package):
    By not providing "FindQt5.cmake" in CMAKE_MODULE_PATH this project has
    asked CMake to find a package configuration file provided by "Qt5", but
    CMake did not find one.

    Could not find a package configuration file provided by "Qt5" with any of
    the following names:

    Qt5Config.cmake
    qt5-config.cmake

    Add the installation prefix of "Qt5" to CMAKE_PREFIX_PATH or set "Qt5_DIR"
    to a directory containing one of the above files. If "Qt5" provides a
    separate development package or SDK, be sure it has been installed.
  • 按照提示,可以将Qt5_DIR设置为/home/aaron/yalong/software/Qt5.6.2/5.6/gcc_64/lib/cmake/Qt5,因为该目录下包含Qt5Config.cmake文件

在调整相关编译选项时,比如设置目录或开启和关闭某些选项后,需要多次点击Configure,并且调整至最后没有错误提示,最后点击Generate,生成Makefile

Step 4: 编译和安装

1
2
make # 或者make -j4,多线程编译提升编译速度
sudo make install # 默认情况下libs会安装到/usr/loca/lib目录下,具体位置也可在cmake选项内设置

Step 5: 测试

建立一个使用vtk的main.cpp文件,完成读取ply,写成stl的功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkPLYReader.h>
#include <vtkSTLWriter.h>

int main(int argc, char** argv) {
if (argc > 3) {
const std::string inFileName{ argv[1] };
const std::string outFileName{ argv[2] };

auto reader = vtkSmartPointer<vtkPLYReader>::New();

reader->SetFileName(inFileName.c_str());
reader->Update();

auto mesh = reader->GetOutput();
auto writer = vtkSmartPointer<vtkSTLWriter>::New();

writer->SetFileName(outFileName.c_str());
writer->SetInputData(mesh);
writer->Write();
writer->Update();
}
else {
std::cout << "Usage: main mesh.ply mesh.stl" << std::endl;
}
}

使用cmake构建工程

1
2
3
4
5
6
7
8
9
10
11
12
cmake_minimum_required(VERSION 3.0)
project(hellovtk)

# enable c++11
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

# vtk
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(main main.cpp)
target_link_libraries(main ${VTK_LIBRARIES})


编译PCL 1.8.0

下载pcl 1.8.0源码,按照pcl官网的编译指南进行编译,主要有以下几步:

Step 1: 安装pcl的必要依赖,vtk(已安装)、eigen、flann和boost

eigen,flann和boost在ubuntu下都可以通过apt-get安装,具体可先使用apt-cache search先查找合适的版本,然后apt-get install进行安装

1
2
3
aaron@pc:~$ sudo apt-get install libeigen3-dev 
aaron@pc:~$ sudo apt-get install libflann-dev
aaron@pc:~$ sudo apt-get install libboost-dev

其中,pcl依赖boost的system, filesystem, thread, date_time, iostreams, chrono模块。如果在cmake时提示缺少某一个模块,可直接安装该模块。比如缺少filesystem

1
aaron@pc:~$ sudo apt-get install libboost-filesystem-dev

1
openni不是必须的依赖,这里不安装它

Step 2: 配置cmake,生成Makefile;编译并安装

CMAKE_BUILD_TYPE: Release

Step 3: 测试,cmake示例

1
2
3
4
5
6
7
8
9
10
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(hellopcl)

find_package(PCL 1.8 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable(main main.cpp)
target_link_libraries(main ${PCL_LIBRARIES})

编译itk 1.14.0

itk基本没有外部依赖。如果需要启用跟vtk模块的交互,在cmake中开启选项itk_vtk_glue。当然,还需要设置built_type等基本选项(比如关闭doc, example, doxgen和testing编译选项)。如果发现报如下跟kwstyle相关的错,可使用apt-get install安装kwstyle

1
2
CMake Error at /usr/share/cmake-3.5/Modules/ExternalProject.cmake:1757 (message):
error: could not find git for clone of KWStyle

cmake文件示例

1
2
3
4
5
6
7
8
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(helloitk)

find_package(ITK REQUIRED)
include(${ITK_USE_FILE})

add_executable(main main.cpp)
target_link_libraries(main ${ITK_LIBRARIES})