ITK4.9 在QtCreator环境中的配置 以及相关问题

</pre><pre name="code" class="cpp">

计算机系统:Win10(64 bit)

 ITK版本:4.9

 编译器:MSVC 2013(64 bit)

 Qt Creator:3.6(64 bit)

 Qt版本:5.4.2(64 bit)

1前言

 翻阅网上资料大部分关于ITK测试都是在VS中完成的,本文根据查阅相关资料尝试了如何在QtCreator环境下 测试ITK 其核心是如何像在VS中一样添加库目录和包含目录。 最后描述一个棘手问题的解决方案

2 创建pri文件

 pri文件是QtCreator一个特有文件 有点类似于Vs中的头文件 使用时要用 include(文件名)来引用。我们创建一个ITKlib.pri文件,然后添加如下内容: INCLUDEPATH+=F:/ITKVTK/InsightToolkit-4.9.0_install/include/ITK-4.9 QMake_LIBDIR+=F:/ITKVTK/InsightToolkit-4.9.0_install/lib/ LIBS+=itkdouble-conversion-4.9.lib \ .............. .lib\ 添加内容注意格式: INCLUDEPATH后面添加包含目录 QMAKE_LIBDIR后面添加库目录 LIBS后面添加lib文件 如果内容太长使用 \ 做分隔符空到下一行写。

3 将pri文件添加到pro中

 include(.....pri) 

注:我们可以把上一节内容直接添加的pro文件中 而不需使用pri 但这样会使得pro文件过于臃肿 且不利于复用。比如我们以后新建一个ITK工程就可以直接将pri文件拷贝到新工程的pro目录下就好

 4测试test.h--------------------------------end

#ifndef TEST
#define TEST
#include <QObject>
class ITKFUNC : public QObject
{
    Q_OBJECT
public:
    explicit ITKFUNC(QObject *parent = 0);
public:
    void testITK();
    void readITK();
};
#endif // TEST

test.h--------------------------------end

test.cpp---------------------------start

#define _SCL_SECURE_NO_WARNINGS
#include"test.h"
#include<itkImage.h>
#include<itkRandomImageSource.h>
#include<itkImageFileWriter.h>
#include"itkImageFileReader.h"
#include<itkPNGImageIOFactory.h>
#include<itkJPEGImageIOFactory.h>
#include<iostream>
#include<itkMesh.h>
ITKFUNC::ITKFUNC(QObject *parent)
    :QObject(parent)
{
    itk::ObjectFactoryBase::RegisterFactory(itk::PNGImageIOFactory::New());
}
void ITKFUNC::testITK()
{
    typedef unsigned char PixekType;
    typedef itk::Image<PixekType,2> ImageType;
    ImageType::SizeType size;
    size.Fill(1000);
    typedef itk::RandomImageSource<ImageType> RandomSourceType;
    RandomSourceType::Pointer randomSource=RandomSourceType::New();
    randomSource->SetNumberOfThreads(1);
    randomSource->SetSize(size);
    typedef itk::ImageFileWriter<ImageType> WriterType;
    WriterType::Pointer writer=WriterType::New();
    writer->SetFileName("testITK.png");
    writer->SetInput(randomSource->GetOutput());
    try{writer->Update();}
       catch(itk::ExceptionObject&error)
    {std::cerr<<"Error:"<<error<<std::endl;}
}
test.cpp---------------------end

5 测试过程中出现的问题

itksys-4.9.lib(SystemTools.obj):-1: error: LNK2019: 无法解析的外部符号 __imp_RegCloseKey,该符号在函数 "public: static bool __cdecl itksys::SystemTools::GetRegistrySubKeys(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &,enum itksys::SystemTools::KeyWOW64)" (?GetRegistrySubKeys@SystemTools@itksys@@SA_NAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEAV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@4@W4KeyWOW64@12@@Z) 中被引用

 问题描述:根据参考资料(见文章末尾)主要是由于未添加advapi32.lib这个lib文件 这个lib文件位于C:/Program Files (x86)/Windows Kits/8.1/Lib/winv6.3/um/x64中  Wiindows Kits 是window系统的SDK .是window的部署工具包 里面有一些我们会经常在VS的附加依赖项里见到的lib文件

例如:kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib等等 在VS中会自动连接这些lib文件。而在QtCreator中需要手动添加

 问题解决:在再pro目录下新建一个pri文件 命名为WindowKits 在里面添加如下:

QMAKE_LIBDIR +=  C:/Program Files (x86)/Windows Kits/8.1/Lib/winv6.3/um/x64
LIBS+=kernel32.lib \
      user32.lib \
      gdi32.lib \
      winspool.lib \
      comdlg32.lib \
      advapi32.lib \
      shell32.lib \
      ole32.lib \
      oleaut32.lib \
      uuid.lib \
      odbc32.lib \
      odbccp32.lib \
和前面一样操作,在pro里添加这个pri文件。 重新qmake 构建程序。问题就解决了

参考文献:
【1】VS2012编译安装ITK-4.7.2_支持Qt5.2.1

你可能感兴趣的:(ITK4.9 在QtCreator环境中的配置 以及相关问题)