首先:我是使用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
这是位于source / IUI中的cmakelists.txt
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中的include工作正常,我能够实例化控制器类和调用函数。但是如果我在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
那么我做错了什么?
答案 0 :(得分:0)
首先,如果您提供的错误消息已完成,则包括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})
如果不能选择放弃共享库,则需要清理链接,方法是在两个不同的对象中没有相同的源(同样,替换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)
不需要向add_*
添加标题。