使用 cmake 在 mainwindow.cpp (Qt5) 中包含头文件?

Include headerfiles in the mainwindow.cpp (Qt5) using cmake?

首先:我是使用 cmake 的新手,尤其是 Qt5。所以我的解释对你来说可能有点奇怪,但我会尽力而为。

目前我正准备使用 cmake、Qt5 和 mingw 设置我的项目。

我的项目结构如下:

Cmake (my project name)
├── headers
│   ├── Controller
│   │   └── Controller.h
│   └── IUI
│       └── mainwindow.h
└── source
    ├── Controller
    │   └── Controller.cpp
    │   └── cmakelists.txt
    └── IUI
        ├── main.cpp
        ├── mainwindow.cpp
        └── mainwindow.ui
        └── cmakelists.txt

这是 cmakelists.txt 位于 source/IUI

cmake_minimum_required(VERSION 2.8.11)

project(CellAnalyser)

find_package (Qt5Widgets)

set (CellAnalyserLib_src ${PROJECT_SOURCE_DIR}/source/IUI/mainwindow.cpp)
set (CellAnalyserLib_hdr ${PROJECT_SOURCE_DIR}/header/IUI/mainwindow.h)
set (CellAnalyserLib_ui  ${PROJECT_SOURCE_DIR}/source/IUI/mainwindow.ui)
set (CellAnalyserBin_src ${PROJECT_SOURCE_DIR}/source/IUI/main.cpp)

set (Source ${CellAnalyserLib_src} ${CellAnalyserBin_src})

qt5_wrap_cpp(CellAnalyserLib_hdr_moc ${CellAnalyserLib_hdr})
qt5_wrap_ui (CellAnalyserLib_ui_moc  ${CellAnalyserLib_ui})

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

add_library (CellAnalyserLib SHARED 
 ${CellAnalyserLib_src}
 ${CellAnalyserLib_hdr_moc}
 ${CellAnalyserLib_ui_moc}
)

add_executable(CellAnalyser ${Source}
 ${CellAnalyserLib_hdr}
 ${CellAnalyserLib_src}
 ${CMAKE_SOURCE_DIR}/header/Controller/Controller.h
 ${CMAKE_SOURCE_DIR}/source/Controller/Controller.cpp
)

target_link_libraries (CellAnalyserLib Qt5::Widgets)

target_link_libraries (CellAnalyser ${ITK_LIBRARIES})
target_link_libraries(CellAnalyser ${OPENGL_LIBRARIES})
target_link_libraries (CellAnalyser CellAnalyserLib)

这就是您需要了解的重要部分。现在的问题是:为什么我不能在 mainwindow.cpp 中包含 Header "controller.h"? main.cpp 中的包含工作正常,我能够实例化 controller-class 和调用函数。但是如果我在 mainwindow.cpp 中尝试完全相同的事情,我会得到以下输出:

CMakeFiles\CellAnalyserLib.dir/objects.a(mainwindow.cpp.obj):mainwindow.cpp:(.text+0x820): undefined reference to `_imp___ZN6itksys18SystemToolsManagerD1Ev'
CMakeFiles\CellAnalyserLib.dir/objects.a(mainwindow.cpp.obj):mainwindow.cpp:(.text+0x859): undefined reference to `_imp___ZN6itksys18SystemToolsManagerC1Ev'
CMakeFiles\CellAnalyserLib.dir/objects.a(mainwindow.cpp.obj):mainwindow.cpp:(.data+0xfffff740): undefined reference to `itk::NiftiImageIOFactoryRegister__Private()'
C:/PROGRA~2/MINGW-~1/I686-4~1.2-P/mingw32/bin/../lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/bin/ld.exe: CMakeFiles\CellAnalyserLib.dir/objects.a(mainwindow.cpp.obj): bad reloc address 0x0 in section `.data'
collect2.exe: error: ld returned 1 exit status 
source\IUI\CMakeFiles\CellAnalyserLib.dir\build.make:134: recipe for target 'source/IUI/libCellAnalyserLib.dll' failed
mingw32-make[2]: *** [source/IUI/libCellAnalyserLib.dll] Error 1
CMakeFiles\Makefile2:263: recipe for target 'source/IUI/CMakeFiles/CellAnalyserL
ib.dir/all' failed
mingw32-make[1]: *** [source/IUI/CMakeFiles/CellAnalyserLib.dir/all] Error 2
makefile:82: recipe for target 'all' failed
mingw32-make: *** [all] Error 2

那我做错了什么?

首先,如果您提供的错误消息是完整的,那么包含 controller.h 似乎工作正常。只有在链接 CellAnalyserLib 时链接失败。

原因是您正在使用从 controller.cpp 获得的函数,但您没有以任何方式将 controller.cpp 包含在您的链接中(既不是直接作为源,也不是间接通过另一个库).此外,您没有将 ITK 与您的 CellAnalyserLib 链接。

一个可能的解决方法是完全放弃共享库(替换你的 add_*target_link_libraries 语句):

add_executable(CellAnalyser ${Source}
 ${CellAnalyserLib_hdr}
 ${CellAnalyserLib_src}
 ${CellAnalyserLib_hdr_moc}
 ${CellAnalyserLib_ui_moc}
 ${CMAKE_SOURCE_DIR}/header/Controller/Controller.h
 ${CMAKE_SOURCE_DIR}/source/Controller/Controller.cpp
)

target_link_libraries (CellAnalyser Qt5::Widgets)
target_link_libraries (CellAnalyser ${ITK_LIBRARIES})
target_link_libraries(CellAnalyser ${OPENGL_LIBRARIES})

如果放弃共享库不是一个选项,您需要通过在两个不同的 objects 中不使用相同的源来清理您的链接(同样,替换您的 add_*target_link_libraries 语句):

add_library (CellAnalyserLib SHARED 
 ${CellAnalyserLib_src}
 ${CellAnalyserLib_hdr_moc}
 ${CellAnalyserLib_ui_moc}
 ${CMAKE_SOURCE_DIR}/source/Controller/Controller.cpp
)

add_executable(CellAnalyser ${CellAnalyserBin_src}
)

target_link_libraries (CellAnalyserLib ${ITK_LIBRARIES})
target_link_libraries(CellAnalyserLib ${OPENGL_LIBRARIES})
target_link_libraries (CellAnalyserLib Qt5::Widgets)

target_link_libraries (CellAnalyser CellAnalyserLib)

不需要将 headers 添加到 add_*