ITK 之 HelloWorld
文章目录
0.前言
前面一篇文章已经编译并配置好了ITK
,我们如何使用 ITK 库
呢?
这里,我选择了 Qt Creator
作为 IDE
,使用 CMake
管理项目的方式使用 ITK 库
。
1.新建 HelloWorld 项目
- 打开
Qt Creator
选则【新建文件或项目】–> 【Non-Qt Project】–>【Plain C++ Application】 - 这里选择了 非 Qt 项目,纯 C++ 应用程序
- 指定项目的名称
- 项目路径不可以有中文
- 此处选择
CMake
作为 构建工具
- 选择本项目使用的构建套件,因为我们的
ITK 库编译使用的是
VS 2017 64位,因此,此处我们选择了对应的
MSVC2017 64 bit`。
- 此处可以指定版本控制,我们这里就不指定了,直接点击完成即可。
2.实现 HelloWorld 代码
2.1 CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(HelloWorld LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(ITK REQUIRED)
#loads the UseITK.cmake file which contains the configuration information about the specified ITK build.
include(${ITK_USE_FILE})
# first argument the name of the executable that will be produced as result of this project.
# The remaining argument(s) of add executable are the names of the source files to be compiled.
add_executable(HelloWorld main.cpp)
# call specifies which ITK libraries will be linked against this project
target_link_libraries(HelloWorld ${ITK_LIBRARIES})
2.2 main.cpp
#include "itkImage.h"
#include <iostream>
using namespace std;
int main()
{
using ImageType = itk::Image< unsigned short, 3>;
ImageType::Pointer image = ImageType::New();
cout << "Hello World!" << endl;
return 0;
}
3.编译运行
- 一定要注意一个细节,如果我们使用的是
Debug 版本的 ITK 库
此时我们只能选择Debug
,构建Release
会出现库不相符的情况而报错。 - 如果我们使用的是
Release 版本的 ITK 库
此时我们只能选择Release
4.运行结果
- 如果应用程序输出了 HelloWorld ,则我们的
ITK
库已经可以正常使用了。
- 如果,您这里报错,找不到
ITK_DIR
建议看一下上一篇文章最后,配置ITK
环境变量。