1
votes

I'm having trouble building a project using Qt and VTK. I'm using Cmake 3.5.1, vtk7.1 and Qt5.2.1. When I run make after running cmake I get this error:

CMakeFiles/MainWindow.dir/main.cpp.o: In function main': main.cpp:(.text+0x10a): undefined reference toMainWindow::MainWindow(QWidget*)' main.cpp:(.text+0x12f): undefined reference to MainWindow::~MainWindow()' main.cpp:(.text+0x1b5): undefined reference toMainWindow::~MainWindow()' collect2: error: ld returned 1 exit status CMakeFiles/MainWindow.dir/build.make:218: recipe for target 'MainWindow' failed make[2]: * [MainWindow] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/MainWindow.dir/all' failed make[1]: * [CMakeFiles/MainWindow.dir/all] Error 2 Makefile:83: recipe for target 'all' failed make: *** [all] Error 2

My CmakeLists.txt looks like this:

cmake_minimum_required(VERSION 2.8.8)
if(POLICY CMP0020)
  cmake_policy(SET CMP0020 NEW)
endif()
if(POLICY CMP0025)
  cmake_policy(SET CMP0025 NEW) # CMake 3.0
endif()
if(POLICY CMP0043)
  cmake_policy(SET CMP0043 NEW) # CMake 3.0
endif()
if(POLICY CMP0053)
  cmake_policy(SET CMP0053 NEW) # CMake 3.1
endif()
project(MainWindow)

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

if("${VTK_QT_VERSION}" STREQUAL "")
  message(FATAL_ERROR "VTK was not built with Qt")
endif()

set( Srcs main.cpp )

if(VTK_QT_VERSION VERSION_GREATER "4")
  find_package(Qt5 COMPONENTS Core REQUIRED QUIET)

  add_executable(MainWindow ${Srcs})
  qt5_use_modules(MainWindow Core Gui Widgets)
  target_link_libraries(MainWindow ${VTK_LIBRARIES})
else()
  find_package(Qt4 REQUIRED)
  include(${QT_USE_FILE})
  # Use what VTK built with
  set(QT_QMAKE_EXECUTABLE ${VTK_QT_QMAKE_EXECUTABLE} CACHE FILEPATH "")
  set(QT_MOC_EXECUTABLE ${VTK_QT_MOC_EXECUTABLE} CACHE FILEPATH "")
  set(QT_UIC_EXECUTABLE ${VTK_QT_UIC_EXECUTABLE} CACHE FILEPATH "")
  include_directories(${QT_INCLUDE_DIR})

  add_executable(MainWindow ${Srcs})
  target_link_libraries(MainWindow ${QT_LIBRARIES} ${VTK_LIBRARIES})
endif()

The error seems to be associated with the cons and ~cons. Here is a snippet from the MainWindow cpp where I think the problem is arising:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow){    

MainWindow::~MainWindow()
{
    writeSettings();
    axes->Delete();
    marker->Delete();
    delete ui;
} 

and here is some of the header:

class MainWindow : public QMainWindow
{
    Q_OBJECT
    /*signals:
    void overVolume(int, int, int);*/
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

I exclude the body of the constructor because it is very long and I don't think it has anything to do with the error.

Thoughts?

1

1 Answers

1
votes

You probably forgot to update your Srcs variable to include all necessary cpp files to build the MainWindow target (executable).

As a result you omit the code for implementing the MainWindow class entirely in the add_executable(MainWindow ${Srcs}) statement. That has the side-effect that the linker sees only function calls to MainWindow from your main.cpp which probably amount to just creating one and (implicitly) destroying one, hence the linker complaining about these exact 2 functions being undefined.

So, to fix all this, expand your definition of ${Srcs} in set( Srcs main.cpp ) with something like this:

# assuming MainWindow is implemented in mainwindow.cpp
set( Srcs main.cpp mainwindow.cpp)