C++ Cmake qt项目链接错误(未定义符号:uu declspec(dllimport))
这是程序的文件结构。C++ Cmake qt项目链接错误(未定义符号:uu declspec(dllimport)),c++,qt,makefile,cmake,C++,Qt,Makefile,Cmake,这是程序的文件结构。 它只是创建一个窗口 test │ CMakeLists.txt │ main.cpp │ mainwindow.cpp | mainwindow.h | mainwindow.ui cmakelists.txt cmake_minimum_required(VERSION 3.10) project(test) set (CMAKE_PREFIX_PATH "G:/qt/5.12.
它只是创建一个窗口
test
│ CMakeLists.txt
│ main.cpp
│ mainwindow.cpp
| mainwindow.h
| mainwindow.ui
cmakelists.txt
cmake_minimum_required(VERSION 3.10)
project(test)
set (CMAKE_PREFIX_PATH "G:/qt/5.12.10/mingw73_64")#my qt path
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
find_package(Qt5 REQUIRED COMPONENTS Widgets)
add_executable(main main.cpp mainwindow.cpp mainwindow.h mainwindow.ui)
target_link_libraries(main Qt5::Widgets)
部分错误消息:
[ 20%] Automatic MOC and UIC for target main
[ 20%] Built target main_autogen
Consolidate compiler generated dependencies of target main
[ 40%] Building CXX object CMakeFiles/main.dir/mainwindow.cpp.obj
[ 60%] Linking CXX executable main.exe
lld-link: error: undefined symbol: __declspec(dllimport) public: static struct QMetaObject const QMainWindow::staticMetaObject
>>> referenced by CMakeFiles/main.dir/main_autogen/mocs_compilation.cpp.obj:(void __cdecl `dynamic initializer for `public: static struct QMetaObject const MainWindow::staticMetaObject''(void))
lld-link: error: undefined symbol: __declspec(dllimport) public: struct QMetaObject * __cdecl QObjectData::dynamicMetaObject(void) const
>>> referenced by F:\test\part1\ui\debug\main_autogen\EWIEGA46WW\moc_mainwindow.cpp:77
>>> CMakeFiles/main.dir/main_autogen/mocs_compilation.cpp.obj:(public: virtual struct QMetaObject const * __cdecl MainWindow::metaObject(void) const)
lld-link: error: undefined symbol: __declspec(dllimport) public: virtual void * __cdecl QMainWindow::qt_metacast(char const *)
>>> referenced by F:\test\part1\ui\debug\main_autogen\EWIEGA46WW\moc_mainwindow.cpp:85
>>> CMakeFiles/main.dir/main_autogen/mocs_compilation.cpp.obj:(public: virtual void * __cdecl MainWindow::qt_metacast(char const *))
这似乎是一个链接错误,但我在CmakeList中链接了QT的库。我如何解决它
编辑1
我将编译器更改为gcc并成功链接(我的代码似乎有问题)。现在,我将文件夹移动到另一个cmake项目,并将其作为程序的子目录。文件结构如下所示:
project
| external/
| include/
| src/
| test/
| ui/(*The folder above)
| CMakeLists.txt
根CMakeLists:
cmake_minimum_required(VERSION 3.1.0)
project(test)
set (CMAKE_PREFIX_PATH "G:/qt/5.12.10/mingw73_64")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
link_directories(
${PROJECT_SOURCE_DIR}/external
)
include_directories(
${PROJECT_SOURCE_DIR}/include
)
add_subdirectory(src)
add_subdirectory(ui)
enable_testing()
add_subdirectory(test)
我使用cmake-G“unixmakefiles”
构建cmake。在make
之后,发生了相同的错误:
[ 71%] Building CXX object test/CMakeFiles/test_goodsrecord.dir/test_goodsrecord.cpp.obj
lld-link: error: undefined symbol: __declspec(dllimport) public: static struct QMetaObject const QMainWindow::staticMetaObject
>>> referenced by CMakeFiles/main.dir/main_autogen/mocs_compilation.cpp.obj:(void __cdecl `dynamic initializer for `public: static struct QMetaObject const MainWindow::staticMetaObject''(void))
lld-link: error: undefined symbol: __declspec(dllimport) public: struct QMetaObject * __cdecl QObjectData::dynamicMetaObject(void) const
>>> referenced by F:\test\part1\build\ui\main_autogen\EWIEGA46WW\moc_mainwindow.cpp:77
>>> CMakeFiles/main.dir/main_autogen/mocs_compilation.cpp.obj:(public: virtual struct QMetaObject const * __cdecl MainWindow::metaObject(void) const)
编辑2
我的环境
海湾合作委员会
gcc安装在MinGW安装程序中
线程型号:posix目标:x86_64-w64-mingw32 gcc版本8.1.0(x86_64-posix-seh-rev0,由MinGW-W64项目构建) 叮当声 使用LLVM WIN64安装程序进行安装。 clang版本12.0.0 目标:x86_64-pc-windows-msvc 线程模型:posix InstalledDir:C:\Program Files\LLVM\bin 克马克
cmake 3.20.0版能否更具体地描述您的环境,并将此信息添加到问题中?MinGW编译器版本,无论您使用的是CLion、mingw64还是MSYS/MSYS2…?@vre ok。我已经添加了这些信息。你在主窗口中搞砸了Q_DECL_导出/Q_DECL_导入。所以链接器认为主窗口类已导入,但它没有。删除那些宏。@chehrlic,但我没有在主窗口中使用Q_DECL_导出/Q_DECL_导入。h。。。