简体   繁体   English

Windows中的Qt库链接错误

[英]Qt library link errors in Windows

Project builds fine on Linux however has problem linking in Windows. 项目在Linux上构建良好,但是在Windows中链接存在问题。

1st issue: 第一期:

LNK2019: unresolved external symbol ...
LNK1120: 21 unresolved externals

.pro file contains: .pro文件包含:

isEmpty(IDE_BUILD_TREE): IDE_BUILD_TREE = ../../qt-creator-debug
LIBS += -L$${IDE_BUILD_TREE}/lib/qtcreator/plugins -lMyLibrary

Note. 注意。 MyLibrary deployed to $${IDE_BUILD_TREE}/lib/qtcreator/plugins before build. 在构建之前,将MyLibrary部署到$${IDE_BUILD_TREE}/lib/qtcreator/plugins Building with Qt 5.10.1 and MSVC 2015. 使用Qt 5.10.1和MSVC 2015构建。

What is the problem/trick here? 这是什么问题/技巧? How to solve? 怎么解决?


2nd issue: 第二期:

In the library .pro file VERSION variable is defined and resulting library has name MyLibrary1.lib . 在库.pro文件中,定义了VERSION变量,并且生成的库具有名称MyLibrary1.lib Thereafter I get error: 此后我得到错误:

:-1: error: LNK1181: cannot open input file 'MyLibrary.lib'

What is better way to solve the problem here: remove VERSION or fix .pro file? 在这里解决问题的更好方法是什么:删除VERSION或修复.pro文件? How? 怎么样?


3rd issue: 第三期:

Another link error: 另一个链接错误:

mydialog.obj:-1: error: LNK2001: unresolved external symbol 
"struct QMetaObject const MyLibrary::staticMetaObject" 
(?staticMetaObject@MyLibrary@@3UQMetaObject@@B)

Error happen because of the following line in code (disappears when commented out): 由于代码中的以下行而发生错误(注释掉时消失):

mydialog.cpp: mydialog.cpp:

    QMetaEnum myEnum = QMetaEnum::fromType<MyLibrary::MyEnumClass>();

mylibrary.h: mylibrary.h:

namespace MyLibrary {

Q_NAMESPACE

enum class MYLIBRARYSHARED_EXPORT MyEnumClass {
...
};

Q_ENUM_NS(MyEnumClass)
...
} // namespace MyLibrary

And how to solve the 3rd one? 以及如何解决第三个问题?

1st issue fix: 第一期修复:

My bad: error caused by missed MYLIBRARYSHARED_EXPORT in some classes' declarations, which defined in global header as: 我的坏处:由某些类的声明中缺少MYLIBRARYSHARED_EXPORT引起的错误,在全局标头中将其定义为:

#if defined(MYLIBRARY_LIBRARY)
#  define MYLIBRARYSHARED_EXPORT Q_DECL_EXPORT
#else
#  define MYLIBRARYSHARED_EXPORT Q_DECL_IMPORT
#endif

Without MYLIBRARYSHARED_EXPORT builds fine in Linux and Mac but fails in Windows. 没有MYLIBRARYSHARED_EXPORT可以在Linux和Mac上MYLIBRARYSHARED_EXPORT构建,但是在Windows上无法构建。


2nd issue fix: 第二期修复:

Possible solution - add to .pro file line: 可能的解决方案-添加到.pro文件行:

win32:CONFIG += skip_target_version_ext

or 要么

win32:TARGET_EXT = .dll 

to set the output filename without the major version number on Windows. 在Windows上设置没有主版本号的输出文件名。 However I see, for example, Qt Creator plugins link libraries with major version number without a problem. 但是,例如,我看到Qt Creator插件将库的主要版本号链接在一起没有问题。 How to do this? 这个怎么做?


3rd issue fix: 第三期修复:

Need to prepend Q_NAMESPACE declaration with MYLIBRARYSHARED_EXPORT as well: 还需要在Q_NAMESPACE声明前加上MYLIBRARYSHARED_EXPORT

namespace MyLibrary {

MYLIBRARYSHARED_EXPORT Q_NAMESPACE

enum class MYLIBRARYSHARED_EXPORT MyEnumClass {
...
};

Q_ENUM_NS(MyEnumClass)
...
} // namespace MyLibrary

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

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