简体   繁体   中英

Qt library link errors in Windows

Project builds fine on Linux however has problem linking in Windows.

1st issue:

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

.pro file contains:

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. Building with Qt 5.10.1 and 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 . 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? 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:

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

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:

#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.


2nd issue fix:

Possible solution - add to .pro file line:

win32:CONFIG += skip_target_version_ext

or

win32:TARGET_EXT = .dll 

to set the output filename without the major version number on Windows. However I see, for example, Qt Creator plugins link libraries with major version number without a problem. How to do this?


3rd issue fix:

Need to prepend Q_NAMESPACE declaration with MYLIBRARYSHARED_EXPORT as well:

namespace MyLibrary {

MYLIBRARYSHARED_EXPORT Q_NAMESPACE

enum class MYLIBRARYSHARED_EXPORT MyEnumClass {
...
};

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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