Reputation: 4991
Project builds fine on Linux however has problem linking in Windows.
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?
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?
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):
QMetaEnum myEnum = QMetaEnum::fromType<MyLibrary::MyEnumClass>();
namespace MyLibrary {
Q_NAMESPACE
enum class MYLIBRARYSHARED_EXPORT MyEnumClass {
...
};
Q_ENUM_NS(MyEnumClass)
...
} // namespace MyLibrary
And how to solve the 3rd one?
Upvotes: 2
Views: 1836
Reputation: 4991
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.
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?
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
Upvotes: 8