VS Tools how to use a qt class library
-
I am trying to work on qt class libraries and i want to include a qt class library in a qt application. Platform is visual studio 2022, qt tools and qt 6.4.1. application and class library I created via project wizard.
I have included the class library via links and added the path to the header and library path in the QT project settings.
I can include the header file but nothing from the library is visible except the default constructor.VS Project:
QT Project settings
Testlib.h
#pragma once #include "testlib_global.h" class TESTLIB_EXPORT TestLib { public: TestLib(); void SayHello(); };
Testlib_global.h
#pragma once #include <QtCore/qglobal.h> #ifndef BUILD_STATIC # if defined(TESTLIB_LIB) # define TESTLIB_EXPORT Q_DECL_EXPORT # else # define TESTLIB_EXPORT Q_DECL_IMPORT # endif #else # define TESTLIB_EXPORT #endif
Testlib.ccp
#include "TestLib.h" TestLib::TestLib() { } void TestLib::SayHello() { }
main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QtCore/qglobal.h> #include "testlib.h" int main(int argc, char *argv[]) { #if defined(Q_OS_WIN) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication app(argc, argv); TestLib* lib = new TestLib(); lib->SayHello(); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
lib->SayHello() gives an error because nothing is found in lib except the constructor
-
@astoffregen
The expansion ofTESTLIB_EXPORT
whenTestlib.h
is read depends on the existence/definition ofTESTLIB_LIB
at the time it is read (unless you are building withBUILD_STATIC
which I imagine you are not). Isn't the idea that yourTestlib.cpp
(only this file) should have#define TESTLIB_LIB
(to say this is the implementation) somewhere before it goes#include "TestLib.h"
? Does that affect your situation? -
@JonB I still changed the library type to static, because I don't need a dynamic library. The method can now be selected in the main.cpp. but when compiling comes these errors:
Severity Code Description Project File Line Suppression State Error LNK2019 Reference to unresolved external symbol ""__declspec(dllimport) public: __cdecl TestLib::TestLib(void)" (__imp_??0TestLib@@QEAA@XZ)" in function "main". TestApp C:\Users\arne\workspace4\TestApp\main.obj 1
Severity Code Description Project File Line Suppression state Error LNK2019 Reference to unresolved external symbol ""__declspec(dllimport) public: void __cdecl TestLib::sayHello(void)" (__imp_?sayHello@TestLib@@QEAAXXZ)" in function "main". TestApp C:\Users\arne\workspace4\TestApp\main.obj 1
Two unresolved external
Probably this is a beginner's problem - I'm just switching from c# to c++ due to the project and it's much more complex.
Since the application itself is already very complex i wanted to at least create the structures correctly right at the beginning and for that i need several libraries
Thanks for any support
-
@astoffregen Looks to me like you're not linking against
TestLib.lib
.