尝试使用Qt和VTK显示DICOM文件时未定义的引用问题

时间:2016-03-29 16:55:57

标签: c++ qt cmake vtk

我正在尝试使用Ubuntu下的VTK 7.1和Qt 5.5.1开发一个简单的DICOM查看器,但是当我尝试在我的终端上编译项目或使用Creator时,会发现几个未定义的引用错误......

结果:

libDigitalImagingLib.so : undefined reference to 'vtkRenderingVolumeOpenGL2_AutoInit_Construct()'
libDigitalImagingLib.so : undefined reference to 'vtkRenderingVolumeOpenGL2_AutoInit_Construct()'
libDigitalImagingLib.so : undefined reference to 'vtkImageViewer2::New()'

我的项目相关代码就像这样......

的CMakeLists.txt

CMAKE_MINIMUM_REQUIRED (VERSION 2.8)
PROJECT (DigitalImaging)

FIND_PACKAGE (VTK)
INCLUDE (${VTK_USE_FILE})

FIND_PACKAGE (Qt5Widgets)

SET (DigitalImagingLib_src ${PROJECT_SOURCE_DIR}/mainwindow.cpp)
SET (DigitalImagingLib_hdr ${PROJECT_SOURCE_DIR}/mainwindow.h)
SET (DigitalImagingLib_ui ${PROJECT_SOURCE_DIR}/mainwindow.ui)
SET (DigitalImagingBin_src ${PROJECT_SOURCE_DIR}/main.cxx)

qt5_wrap_cpp(DigitalImagingLib_hdr_moc ${DigitalImagingLib_hdr})
qt5_wrap_ui(DigitalImagingLib_ui_moc ${DigitalImagingLib_ui})

include_directories (${PROJECT_SOURCE_DIR})
include_directories (${PROJECT_BINARY_DIR})

add_library (DigitalImagingLib SHARED
    ${DigitalImagingLib_src}
    ${DigitalImagingLib_hdr_moc}
    ${DigitalImagingLib_ui_moc}
)

target_link_libraries (DigitalImagingLib Qt5::Widgets)

ADD_EXECUTABLE(DigitalImaging
    ${DigitalImagingBin_src}
)

TARGET_LINK_LIBRARIES (DigitalImaging
    ${VTK_LIBRARIES}
    DigitalImagingLib
)

mainwindow.cpp:

# include "mainwindow.h"
# include "ui_mainwindow.h"

# include <vtkSmartPointer.h>
# include <vtkImageViewer2.h>
# include <vtkDICOMImageReader.h>
# include <vtkRenderWindow.h>
# include <vtkRenderWindowInteractor.h>
# include <vtkRenderer.h>
# include <QVTKWidget.h>

...

void MainWindow::displayDICOM(std::string file) {
    vtkSmartPointer<vtkDICOMImageReader> reader = vtkSmartPointer<vtkDICOMImageReader>::New();
    reader -> SetFileName(file.c_str());
    reader -> Update();

    vtkSmartPointer<vtkImageViewer2> imageViewer = vtkSmartPointer<vtkImageViewer2>::New();
    imageViewer -> SetInputConnection(reader -> GetOutputPort());
    imageViewer -> SetRenderWindow(ui -> vtkRenderer -> GetRenderWindow());
    imageViewer -> Render();
}

...

作为一名初学者,我非常感谢有关CMake最佳编码实践的任何帮助和提示!也许值得一提的是,我试图实施以下内容而没有任何显着的成功(似乎也是如此)......

# include "vtkAutoInit.h"

VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)

1 个答案:

答案 0 :(得分:1)

似乎您错过了在您的DigitalImagingLib中添加VTK库,因为libDigitalImagingLib.so附带了错误

target_link_libraries (DigitalImagingLib Qt5::Widgets ${VTK_LIBRARIES})