在visual studio中构建Qt项目时出现LNK2019错误

时间:2018-04-07 13:19:28

标签: visual-studio qt cmake linker

我使用CMake制作了一个visual studio项目,当我尝试编译它时,我得到了这些错误。该错误似乎只与QDomDocument对象有关,我没有得到任何与其他Qt类相关的链接错误;

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol "__declspec(dllimport) public: __cdecl QDomNode::~QDomNode(void)" (__imp_??1QDomNode@@QEAA@XZ) referenced in function "private: void __cdecl Graph::readFromFile(class QString)" (?readFromFile@Graph@@AEAAXVQString@@@Z)    Gps_test    C:\Users\Cristi\Desktop\an2 sem 2\QT\gps_test1\build\graph.obj  1   

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol "__declspec(dllimport) public: class QDomNodeList __cdecl QDomNode::childNodes(void)const " (__imp_?childNodes@QDomNode@@QEBA?AVQDomNodeList@@XZ) referenced in function "private: void __cdecl Graph::readFromFile(class QString)" (?readFromFile@Graph@@AEAAXVQString@@@Z) Gps_test    C:\Users\Cristi\Desktop\an2 sem 2\QT\gps_test1\build\graph.obj  1   

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol "__declspec(dllimport) public: bool __cdecl QDomDocument::setContent(class QIODevice *,class QString *,int *,int *)" (__imp_?setContent@QDomDocument@@QEAA_NPEAVQIODevice@@PEAVQString@@PEAH2@Z) referenced in function "private: void __cdecl Graph::readFromFile(class QString)" (?readFromFile@Graph@@AEAAXVQString@@@Z)  Gps_test    C:\Users\Cristi\Desktop\an2 sem 2\QT\gps_test1\build\graph.obj  1   

这是源文件的CMake文件:

# Defines the minimum CMake version required for the CMakeLists.txt file
# to be correctly interpreted. Older versions of CMake may not contain
# all the features to "understand" this file.
cmake_minimum_required(VERSION 3.10.0 FATAL_ERROR)

# Defines the name and language the project will be using.
project(Gps_test LANGUAGES CXX)

# Set the path to the Qt5 installation's CMake instructions.
set(Qt5_DIR "D:/qt/5.10.1/msvc2017_64/lib/cmake/Qt5")

# Automatically add the current source and build directories to the
# include path.
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Automatically create Qt5 MOCs at compile time.
set(CMAKE_AUTOMOC ON)

# Automatically create Qt5 UICs at compile time.
set(CMAKE_AUTOUIC ON)

# Include Qt5 and its widgets.
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui)

# Add to PROJECT_SOURCES variable all the filenames inside
# ${CMAKE_CURRENT_SOURCE_DIR} which respect the given pattern.
file(GLOB PROJECT_SOURCES "*.cpp")

# Instruct CMake to create an executable based on all the .cpp
# sources of the project. You may, for example, create multiple
# executables based on different source files, inside the same
# project.
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})

# Specify libraries or flags to use when linking a given target. 
target_link_libraries(${PROJECT_NAME} 
    PUBLIC 
    Qt5::Core
    Qt5::Gui
    Qt5::Widgets)

任何人都知道如何解决这个问题?感谢。

1 个答案:

答案 0 :(得分:3)

这些是Xml组件的一部分。试试这个:

find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui Xml)
...
target_link_libraries(${PROJECT_NAME} 
PUBLIC 
Qt5::Core
Qt5::Gui
Qt5::Widgets
Qt5::Xml
)