Cmake qt 项目链接错误(未定义符号:__declspec(dllimport))

问题描述 投票:0回答:0

这是程序的文件结构。
它只是创建一个窗口。

    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 *))

这似乎是一个链接错误,但我在cmakelists中链接了QT的库。我该如何解决它?

编辑1

我将编译器更改为 gcc 并成功链接(我的 clang 似乎有问题)。
现在我将该文件夹移动到另一个 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 "Unix Makefiles"
来构建 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安装程序进行安装。 铿锵版本 12.0.0 目标:x86_64-pc-windows-msvc 线程模型:posix 安装目录:C:\Program Files\LLVM in

cmake

cmake版本3.20.0

c++ qt makefile cmake
© www.soinside.com 2019 - 2024. All rights reserved.