在 Windows 上使用 CMake 2.8.11 构建自定义 Qt 4.8.3 库时出错
Error while building custom Qt 4.8.3 library with CMake 2.8.11 on Windows
我正在尝试将我的Qt项目从QMake迁移到CMake,但是在尝试使用MSVC10在Windows 7上构建我的库时遇到一些错误。我的CMakeLists.txt文件如下:
cmake_minimum_required( VERSION 2.8.11 )
project( MyLibrary )
find_package(Qt4 REQUIRED)
set( CMAKE_AUTOMOC ON )
set( QT_USE_QTSCRIPT TRUE )
include( ${QT_USE_FILE} )
include_directories( ${CMAKE_CURRENT_BINARY_DIR}/include ${QT_INCLUDES} )
set( MyLibrary_SOURCES
src/connection.cpp
src/mylibrary.cpp
src/node.cpp
src/socket.cpp
include/connection.h
include/mylibrary.h
include/node.h
include/socket.h
include/MyLibrary_global.h
)
add_library(MyLibrary SHARED ${MyLibrary_SOURCES})
target_link_libraries(MyLibrary ${QT_LIBRARIES} )
当我运行时,我尝试使用生成的 nmake Makefile 进行构建,出现以下错误:
C2491:'Connection::staticMetaObjectExtraData' : definition of dllimport static data member not allowed
C2491: 'Connection::staticMetaObject' : definition of dllimport static data member not allowed
我是CMake的新手,所以我不确定CMakeLists.txt文件中是否缺少某些内容,或者是否有更多技巧可以将其与Qt一起使用。我应该提到,出现错误的文件是MOC生成的文件。
编辑:所以输出
消息(${QT_INCLUDES})
如下所示(格式是为了提高可读性):
C:/Qt/4.8.3/include/QtDesigner
C:/Qt/4.8.3/include/QtDeclarative
C:/Qt/4.8.3/include/QtScriptTools
C:/Qt/4.8.3/include/QtDBus
C:/Qt/4.8.3/include/QtDesigner
C:/Qt/4.8.3/include/QtXml
C:/Qt/4.8.3/include/QtSql
C:/Qt/4.8.3/include/QtOpenGL
C:/Qt/4.8.3/include/QtMultimedia
C:/Qt/4.8.3/include/QtNetwork
C:/Qt/4.8.3/include/phonon
C:/Qt/4.8.3/include/QtXmlPatterns
C:/Qt/4.8.3/include/QtWebKit
C:/Qt/4.8.3/include/QtHelp
C:/Qt/4.8.3/include/QtUiTools
C:/Qt/4.8.3/include/QtTest
C:/Qt/4.8.3/include/QtScript
C:/Qt/4.8.3/include/QtSvg
C:/Qt/4.8.3/include/Qt3Support
C:/Qt/4.8.3/include/QtGui
C:/Qt/4.8.3/include/QtCore
C:/Qt/4.8.3/mkspecs/default
C:/Qt/4.8.3/include
C:/Qt/4.8.3/include/QtCore
我尝试不使用自动moc,但我仍然得到相同的结果和错误。
阅读本文: 将基于 QObject 的类导出到 DLL 我发现了问题所在:
在 .pro 文件中,我有以下内容: 定义 +=APPLETTUTORIAL1_LIBRARY
然后在小程序tutorial1_global.h中,我有:
#if defined(APPLETTUTORIAL1_LIBRARY)
# define APPLETTUTORIAL1SHARED_EXPORT Q_DECL_EXPORT
#else
# define APPLETTUTORIAL1SHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // APPLETTUTORIAL1_GLOBAL_H
由于我在 CMakeList 中没有等效项.txt因此编译器转到
# define APPLETTUTORIAL1SHARED_EXPORT Q_DECL_IMPORT
行而不是预期的行:
# define APPLETTUTORIAL1SHARED_EXPORT Q_DECL_EXPORT
所以我的解决方案是只留下这一行,我的共享库构建得很好!
# define APPLETTUTORIAL1SHARED_EXPORT Q_DECL_EXPORT
在阅读了以下内容后.txt我尝试了CMakeLists中的以下内容:http://www.cmake.org/Wiki/CMake:ConvertFromQmake,但我认为我做错了,因此上面的解决方案。
SET(DEFINES "APPLETTUTORIAL1_LIBRARY")
大部分
http://www.cmake.org/cmake/help/v3.0/manual/cmake-qt.7.html
也适用于 CMake 2.8.11。使用导入的目标,而不是使用文件。
好的,我有 3 个想法,不确定它们是否会导致解决方案,但它可以工作。
首先,${QT_INCLUDES}
里有什么?请添加一些message(${QT_INCLUDES})
并分享输出。
第二个想法是手动模拟,因为模拟中的错误或错误。为此,请尝试删除set( CMAKE_AUTOMOC ON )
并添加:
set(QT_USE_QTUITOOLS true)
QT4_WRAP_UI(UI UiFiles.ui)
QT4_WRAP_CPP(MOC3 QObjectFiles.h)
add_library(..........
${UI}
${MOC3})
我的第三个想法是删除SHARED
,也许这会导致错误,但我不确定。
请分享你得到的,我会尽力帮助:)
我没有足够的声誉来添加评论,所以我必须写一个答案......同样的问题,但这里是Qt4.8.5。
继续使用CMAKE_AUTOMOC因为QT4_WRAP_CPP是旧的做法......我发现这个:http://plagatux.es/2012/12/qt-automoc-with-cmake/
当我使用旧方式时,我遇到了与您相同的链接错误。使用 AUTOMOC,我遇到了其他链接/编译错误,但我认为我可以走得更远:
applettutorial1.cpp.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) const tutorial1::AppletTutorial1::`vftable'{for `XAppletPlugin'}" (__imp_??_7AppletTutorial1@tutorial1@@6BXAppletPlugin@@@) referenced in function "public: __cdecl tutorial1::AppletTutorial1::AppletTutorial1(void)" (??0AppletTutorial1@tutorial1@@QEAA@XZ)
您可以将其添加到您的包含中:
${QT_QTCORE_INCLUDE_DIR}
${QT_INCLUDE_DIR}
希望您解决了并可以给我们反馈,以便这对其他人有所帮助。
编辑:就我而言,我解决了以下错误以获得成功的构建:
- 正确检查CMAKE_BUILD_TYPE与"调试"字符串,而不是我在这里和那里阅读的"调试"。RTFM。
- 将我的库构建为静态的,而不是共享的。
- Qt自定义QPush按钮未显示在布局上
- Qt自定义小部件外观在设置样式表后没有变化
- QT 自定义图不正确
- Qt:自定义小部件的多个布局
- QT自定义小部件无法找到对象文件
- 重叠小部件的 QT 自定义布局示例无法编译
- QT自定义小部件无法构建
- Qt自定义树模型显示正确,但有缺陷且速度慢
- Qt自定义插件无法加载 - "Shared Library Not Found"
- Qt:自定义 LineEditDelegate 的绘制功能
- Qt自定义QPushButton点击信号
- Qt自定义嵌套向量的树模型
- Qt自定义小部件不显示子小部件
- QT自定义鼠标事件和拖动模式
- Qt自定义委托崩溃没有错误代码
- Qt自定义插槽
- Qt自定义委托
- QT自定义树视图
- 有没有一种更干净的方式来注册Qt自定义事件
- Qt自定义小部件错误