简体   繁体   English

在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

I am trying to migrate my Qt project from QMake to CMake but I am getting some errors when trying to build my library on Windows 7 with MSVC10. 我正在尝试将Qt项目从QMake迁移到CMake,但是在尝试使用MSVC10在Windows 7上构建库时遇到一些错误。 My CMakeLists.txt files is as follows: 我的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} )

When I run I try to build with the generated nmake Makefile I get the following errors: 运行时,我尝试使用生成的nmake Makefile生成以下错误:

C2491:'Connection::staticMetaObjectExtraData' : definition of dllimport static data member not allowed
C2491: 'Connection::staticMetaObject' : definition of dllimport static data member not allowed

I'm new to CMake so I'm not sure if I'm missing something in the CMakeLists.txt file or if there are more tricks to using it with Qt. 我是CMake的新手,所以我不确定CMakeLists.txt文件中是否缺少某些内容,或者是否还有其他技巧可以与Qt一起使用。 I should mention that the files that are getting the errors are the MOC generated ones. 我应该提到出现错误的文件是MOC生成的文件。

Edit: So the output of 编辑:所以输出

message(${QT_INCLUDES}) 消息($ {QT_INCLUDES})

is as follows (formatting is for readability): 如下(格式化是为了便于阅读):

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

I made tried not using auto-moc but I still get the same result and errors. 我尝试不使用auto-moc,但仍然得到相同的结果和错误。

Reading this: Export QObject-based class to DLL I found what's wrong: 阅读此内容: 将基于QObject的类导出到DLL中,我发现了问题所在:

In .pro file I had the following: DEFINES +=APPLETTUTORIAL1_LIBRARY 在.pro文件中,我具有以下内容:DEFINES + = APPLETTUTORIAL1_LIBRARY

Then in applet-tutorial1_global.h I have: 然后在applet-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

Since I didn't have the equivalent in CMakeLists.txt the compiler goes to the 由于我在CMakeLists.txt中没有等效项,因此编译器转到

#  define APPLETTUTORIAL1SHARED_EXPORT Q_DECL_IMPORT

line rather than the expected one: 行而不是预期的:

#  define APPLETTUTORIAL1SHARED_EXPORT Q_DECL_EXPORT

So my solution was to leave only this line and my shared library built well! 因此,我的解决方案是仅保留这一行,并且共享库构建良好!

#  define APPLETTUTORIAL1SHARED_EXPORT Q_DECL_EXPORT

I tried the folllowing in CMakeLists.txt after reading this: http://www.cmake.org/Wiki/CMake:ConvertFromQmake but I did it wrong I think, thus the solution above. 阅读以下内容后,我尝试在CMakeLists.txt中进行以下跟踪: http ://www.cmake.org/Wiki/CMake:ConvertFromQmake,但我认为我做错了,因此上面提供了解决方案。

SET(DEFINES "APPLETTUTORIAL1_LIBRARY")

Most of 大多数

http://www.cmake.org/cmake/help/v3.0/manual/cmake-qt.7.html http://www.cmake.org/cmake/help/v3.0/manual/cmake-qt.7.html

applies to CMake 2.8.11 too. 也适用于CMake 2.8.11。 Use imported targets, not the Use file. 使用导入的目标,而不是使用文件。

Ok, I have 3 Ideas, not sure if they lead to a solution, but it could work. 好的,我有3个想法,不确定它们是否可以带来解决方案,但可以解决。

First of all, what is in ${QT_INCLUDES} ? 首先, ${QT_INCLUDES}什么? Add some message(${QT_INCLUDES}) please and share the output. 请添加一些message(${QT_INCLUDES})并共享输出。

The second Idea would be to mock manually, as the error orrurs in the mock. 第二个想法是手动模拟,因为模拟中的错误或规则。 For this try to remove the set( CMAKE_AUTOMOC ON ) and add: 为此,请尝试删除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})

My third idea is to remove the SHARED , maybe this causes the error, but I am not sure on that one. 我的第三个想法是删除SHARED ,这可能会导致错误,但是我不确定那个错误。

Please share what you get and I'll try to help :) 请分享您所得到的,我会尽力帮助:)

I don't have enough reputation to add a comment so I have to write an answer... Same problem but Qt4.8.5 here. 我没有足够的声誉来添加评论,所以我不得不写一个答案...同样的问题,但这里是Qt4.8.5。

Keep using CMAKE_AUTOMOC because QT4_WRAP_CPP is the old way of doing... I found this : http://plagatux.es/2012/12/qt-automoc-with-cmake/ 继续使用CMAKE_AUTOMOC,因为QT4_WRAP_CPP是旧的方法...我发现了这一点: http ://plagatux.es/2012/12/qt-automoc-with-cmake/

When I used old way, I got the same linkage error you had. 当我使用旧方法时,我遇到了与您相同的链接错误。 With AUTOMOC I get other linkage /compilation errors but think I can go further: 使用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)

You might add this to your includes: 您可以将其添加到您的包括:

 ${QT_QTCORE_INCLUDE_DIR}
 ${QT_INCLUDE_DIR}

Hoping you solved and could give us feedback so this helps other people. 希望您能解决并能给我们反馈,以便对他人有所帮助。

EDIT : in my case I solved the following errors to get a successful build: 编辑:就我而言,我解决了以下错误以获得成功的构建:

  • correctly check CMAKE_BUILD_TYPE against "Debug" string, not "DEBUG" as I read here and there. 正确地检查CMAKE_BUILD_TYPE与“调试”字符串,而不是“调试”,因为我在这里和那里读到。 RTFM. RTFM。
  • build my lib as STATIC, not SHARED. 将我的库构建为STATIC,而不是共享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM