Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 521
Aleksey Kontsevich Avatar asked Dec 21 '25 10:12

Aleksey Kontsevich


1 Answers

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
like image 167
Aleksey Kontsevich Avatar answered Dec 23 '25 23:12

Aleksey Kontsevich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!