VS2013的Qt4到Qt5迁移链接器警告(和错误)

时间:2013-08-25 14:20:43

标签: c++ qt qt4 cmake qt5

我有一个项目,我最初开始使用Qt4开发,现在正在迁移到Qt5。几个小时后重新编写一些代码并删除新弃用的调用,我能够让项目进行编译,但现在我遇到了链接问题。

我正在使用CMake生成项目文件。我开始迁移首先修改“解决方案”的CMake文件并替换:

#############################################
# find QT
FIND_PACKAGE(Qt4 REQUIRED)

# configure QT modules
SET(QT_USER_QTCORE TRUE)
SET(QT_USE_QTMAIN TRUE)
SET(QT_USE_QTWEBKIT TRUE)
SET(QT_USE_QTNETWORK TRUE)
SET(QT_USE_QTSQL TRUE)

SET(QT_USE_QTXML TRUE)
SET(QT_USE_QTUITOOLS TRUE)

# include QT requirements
INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS})
#############################################

with(需要清理)

#############################################
# find QT
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)

FIND_PACKAGE(Qt5Concurrent REQUIRED)
FIND_PACKAGE(Qt5Core REQUIRED)
FIND_PACKAGE(Qt5Network REQUIRED)
FIND_PACKAGE(Qt5Sql REQUIRED)
FIND_PACKAGE(Qt5WebKit REQUIRED)
FIND_PACKAGE(Qt5WebKitWidgets REQUIRED)
FIND_PACKAGE(Qt5Widgets REQUIRED)
FIND_PACKAGE(Qt5Xml REQUIRED)

INCLUDE_DIRECTORIES(
    ${Qt5Concurrent_INCLUDE_DIRS}
    ${QT5_FOLDER}
    "C:/Qt/Qt5.0.2/5.0.2/msvc2012_64/include"
)

ADD_DEFINITIONS(${QT_DEFINITIONS})
#############################################

在此之后,我注释了我对ADD_SUBDIRECTORY()的所有电话,并开始一次一个地添加它们。我的项目使用Log4Qt,因为它没有其他子目录的其他依赖项,它是我开始工作的第一个子项目。

在Log4Qt的CMakeLists.txt文件中,我只是将QT4_WRAP_CPP(MOC_SOURCE_FILES ${MOC_HEADERS})更改为QT5_WRAP_CPP(MOC_SOURCE_FILES ${MOC_HEADERS}),并将MOC_HEADERS定义为Log4Qt中所有必需的MOC标头。

当我在VS2013中加载并构建时,它编译得很好,但链接会为每个moc发送相同的警告_ *:

>moc_stringmatchfilter.obj : warning LNK4006: "public: static struct QMetaObject const Log4Qt::StringMatchFilter::staticMetaObject" (?staticMetaObject@StringMatchFilter@Log4Qt@@2UQMetaObject@@B) already defined in log4qt_automoc.obj; second definition ignored
>moc_stringmatchfilter.obj : warning LNK4006: "private: static void __cdecl Log4Qt::StringMatchFilter::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@StringMatchFilter@Log4Qt@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z) already defined in log4qt_automoc.obj; second definition ignored
>moc_stringmatchfilter.obj : warning LNK4006: "public: virtual struct QMetaObject const * __cdecl Log4Qt::StringMatchFilter::metaObject(void)const " (?metaObject@StringMatchFilter@Log4Qt@@UEBAPEBUQMetaObject@@XZ) already defined in log4qt_automoc.obj; second definition ignored
>moc_stringmatchfilter.obj : warning LNK4006: "public: virtual void * __cdecl Log4Qt::StringMatchFilter::qt_metacast(char const *)" (?qt_metacast@StringMatchFilter@Log4Qt@@UEAAPEAXPEBD@Z) already defined in log4qt_automoc.obj; second definition ignored
>moc_stringmatchfilter.obj : warning LNK4006: "public: virtual int __cdecl Log4Qt::StringMatchFilter::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@StringMatchFilter@Log4Qt@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z) already defined in log4qt_automoc.obj; second definition ignored
>moc_stringmatchfilter.obj : warning LNK4221: This object file does not define any previously undefined public symbols, so it will not be used by any link operation that consumes this library

我继续我的迁移并继续得到相同的警告。例如,以下是其中一个子项目的整个CMakeLists.txt文件:

  

PROJECT(网络)

     

INCLUDE_DIRECTORIES($ {CMAKE_CURRENT_BINARY_DIR})

     

SET(SOURCE_FILES RequestCache.cpp WebClient.cpp)

     

SET(MOC_HEADERS RequestCache.h WebClient.h)

     

SET(HEADER_FILES $ {MOC_HEADERS})

     

SET(DEPENDENCY_LIST log4qt Core)

     

ADD_LIBRARY(网络STATIC $ {SOURCE_FILES} $ {HEADER_FILES}     $ {MOC_SOURCE_FILES})

     

QT5_USE_MODULES(网络网络)

和(某些)警告:

moc_WebClient.obj : warning LNK4006: "public: virtual struct QMetaObject const * __cdecl owl::WebClient::metaObject(void)const " (?metaObject@WebClient@owl@@UEBAPEBUQMetaObject@@XZ) already defined in Network_automoc.obj; second definition ignored
moc_WebClient.obj : warning LNK4006: "public: virtual void * __cdecl owl::WebClient::qt_metacast(char const *)" (?qt_metacast@WebClient@owl@@UEAAPEAXPEBD@Z) already defined in Network_automoc.obj; second definition ignored
moc_WebClient.obj : warning LNK4006: "public: virtual int __cdecl owl::WebClient::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@WebClient@owl@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z) already defined in Network_automoc.obj; second definition ignored
moc_WebClient.obj : warning LNK4006: "private: static void __cdecl owl::WebClient::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@WebClient@owl@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z) already defined in Network_automoc.obj; second definition ignored
moc_WebClient.obj : warning LNK4006: "public: static struct QMetaObject const owl::WebClient::staticMetaObject" (?staticMetaObject@WebClient@owl@@2UQMetaObject@@B) already defined in Network_automoc.obj; second definition ignored
moc_WebClient.obj : warning LNK4221: This object file does not define any previously undefined public symbols, so it will not be used by any link operation that consumes this library

继续前进,我继续并添加了所有相关项目并尝试构建项目的可执行文件。然后我得到以下错误(可执行文件的项目名称为'Owl')(这是错误的摘录,而不是完整列表):

error LNK2005: "public: static struct QMetaObject const owl::PostListWebView::staticMetaObject" (?staticMetaObject@PostListWebView@owl@@2UQMetaObject@@B) already defined in moc_PostListView.obj   C:\vbulletin\cmake\deltas\Win64\Deltas\Owl_automoc.obj  Owl
error LNK2005: "private: static void __cdecl owl::PostListWebView::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@PostListWebView@owl@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z) already defined in moc_PostListView.obj   C:\vbulletin\cmake\deltas\Win64\Deltas\Owl_automoc.obj  Owl
error LNK2005: "public: virtual struct QMetaObject const * __cdecl owl::BoardTreeView::metaObject(void)const " (?metaObject@BoardTreeView@owl@@UEBAPEBUQMetaObject@@XZ) already defined in moc_BoardTreeView.obj    C:\vbulletin\cmake\deltas\Win64\Deltas\Owl_automoc.obj  Owl
error LNK2005: "public: virtual void * __cdecl owl::BoardTreeView::qt_metacast(char const *)" (?qt_metacast@BoardTreeView@owl@@UEAAPEAXPEBD@Z) already defined in moc_BoardTreeView.obj C:\vbulletin\cmake\deltas\Win64\Deltas\Owl_automoc.obj  Owl
error LNK2005: "public: virtual int __cdecl owl::BoardTreeView::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@BoardTreeView@owl@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z) already defined in moc_BoardTreeView.obj    C:\vbulletin\cmake\deltas\Win64\Deltas\Owl_automoc.obj  Owl
error LNK2005: "private: static void __cdecl owl::BoardTreeView::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@BoardTreeView@owl@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z) already defined in moc_BoardTreeView.obj  C:\vbulletin\cmake\deltas\Win64\Deltas\Owl_automoc.obj  Owl
error LNK2005: "public: virtual struct QMetaObject const * __cdecl owl::ConfiguringBoardDlg::metaObject(void)const " (?metaObject@ConfiguringBoardDlg@owl@@UEBAPEBUQMetaObject@@XZ) already defined in moc_ConfiguringBoardDlg.obj  C:\vbulletin\cmake\deltas\Win64\Deltas\Owl_automoc.obj  Owl
error LNK2005: "public: virtual void * __cdecl owl::ConfiguringBoardDlg::qt_metacast(char const *)" (?qt_metacast@ConfiguringBoardDlg@owl@@UEAAPEAXPEBD@Z) already defined in moc_ConfiguringBoardDlg.obj   C:\vbulletin\cmake\deltas\Win64\Deltas\Owl_automoc.obj  Owl

我做错了什么?我是否必须在我的CMake文件中修复此问题?我没有看到我如何错误地配置CMake或者是否还有其他我缺少的东西?

谢谢!

1 个答案:

答案 0 :(得分:3)

感觉您没有正确清理项目构建目录。此外,如果您的CMake版本足够高,您可以尝试使用CMAKE_AUTOMOC。更多详情here