Cannot link Qt libs to C++ project manually.
-
wrote on 10 Jun 2021, 19:54 last edited by Sirzhatina 6 Oct 2021, 20:09
I try to build simple code from begginer's manual using VSCode and Command Prompt for VS2019:
#include <QWidgets/QApplication> int main(int argc, char* argv[]) { QApplication a(argc, argv); return a.exec(); }
but something went wrong - I've got these linking erros:
*error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QApplication::QApplication(int &,char * ,int)" (_imp??0QApplication@@QAE@AAHPAPADH@Z) in function _main
error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall QApplication::~QApplication(void)" (_imp??1QApplication@@UAE@XZ) in function _main
error LNK2019: unresolved external symbol "__declspec(dllimport) public: static int __cdecl QApplication::exec(void)" (_imp?exec@QApplication@@SAHXZ) in function _mainWhat I pass to compiler:
/I C:\Qt\5.15.2\msvc2019_64\include (where QAplication (etc.) is)
/link /LIBPATH:C:\Qt\5.15.2\msvc2019_64\lib (where Qt5Widgets.lib (etc.) is)
include path of headers is added as well.
Could anyone tell me what I missed? And I know, it must be easier to build using QtCreator with its tools, but I'd like to do it this way intentionally - if it's possible.
Thanks in advance -
Hi and welcome to devnet,
You are not telling the linker to actually link to the QtWidgets library.
Why not use cmake or qmake to handle your project ?
Just in case, KDAB has blog posts like this one on the matter of using VS Code for Qt projects.
-
Hi and welcome to devnet,
You are not telling the linker to actually link to the QtWidgets library.
Why not use cmake or qmake to handle your project ?
Just in case, KDAB has blog posts like this one on the matter of using VS Code for Qt projects.
wrote on 10 Jun 2021, 20:30 last edited by Sirzhatina 6 Oct 2021, 20:31@SGaist thank you for response.
You are not telling the linker to actually link to the QtWidgets library.
Do you mean I should point to exact file Qt5Widget.lib? I thought it's enough to show directory
Why not use cmake or qmake to handle your project ?
Actually, it's not exactly manual building, I use json to pass compiler arguments. And I'm not confident building systems user yet
-
@SGaist thank you for response.
You are not telling the linker to actually link to the QtWidgets library.
Do you mean I should point to exact file Qt5Widget.lib? I thought it's enough to show directory
Why not use cmake or qmake to handle your project ?
Actually, it's not exactly manual building, I use json to pass compiler arguments. And I'm not confident building systems user yet
wrote on 10 Jun 2021, 20:44 last edited by JonB 6 Oct 2021, 20:47@Sirzhatina said in Cannot link Qt libs to C++ project manually.:
Do you mean I should point to exact file Qt5Widget.lib? I thought it's enough to show directory
No, the
/LIBPATH:C:\Qt\5.15.2\msvc2019_64\lib
does tell linker where to look but you do have to follow it with whichever.lib
files there your code needs it to pick up.Just like the
/I C:\Qt\5.15.2\msvc2019_64\include
tells the compiler where to look for include files, but you still have to specify which ones to#include
. -
@Sirzhatina said in Cannot link Qt libs to C++ project manually.:
Do you mean I should point to exact file Qt5Widget.lib? I thought it's enough to show directory
No, the
/LIBPATH:C:\Qt\5.15.2\msvc2019_64\lib
does tell linker where to look but you do have to follow it with whichever.lib
files there your code needs it to pick up.Just like the
/I C:\Qt\5.15.2\msvc2019_64\include
tells the compiler where to look for include files, but you still have to specify which ones to#include
.wrote on 10 Jun 2021, 20:54 last edited by@JonB I'm a little confused here. Yes, I tell the compiler where to look include files and I specify which one to include excplicitly, but how can I specify which lib to link? Doesn't it link automatically using pointing path?
-
@JonB I'm a little confused here. Yes, I tell the compiler where to look include files and I specify which one to include excplicitly, but how can I specify which lib to link? Doesn't it link automatically using pointing path?
wrote on 10 Jun 2021, 21:05 last edited by@Sirzhatina
Like I said, you have to tell it which.lib
files there you want it to link with, just as the case with includes.The syntax for
LINK
allows.lib
files to be named, I believe your example would require/link /LIBPATH:C:\Qt\5.15.2\msvc2019_64\lib Qt5Widgets.lib ...
The
...
is because you will need more than justQt5Widgets
library, you will need whatever it depends on.This is all why most people allow Creator or cmake/qmake to do the work.
-
@Sirzhatina
Like I said, you have to tell it which.lib
files there you want it to link with, just as the case with includes.The syntax for
LINK
allows.lib
files to be named, I believe your example would require/link /LIBPATH:C:\Qt\5.15.2\msvc2019_64\lib Qt5Widgets.lib ...
The
...
is because you will need more than justQt5Widgets
library, you will need whatever it depends on.This is all why most people allow Creator or cmake/qmake to do the work.
wrote on 10 Jun 2021, 21:14 last edited by Sirzhatina 6 Oct 2021, 21:19@JonB I got it, thank you. Honestly, it's surprising me, because I had no such problem when I was building Boost project - I mean, I just write /LIBPATH:... without necessity to list .lib files
UPD: Ive checked solution, it doesn't work - at least the same mistakes remain without new information -
@JonB I got it, thank you. Honestly, it's surprising me, because I had no such problem when I was building Boost project - I mean, I just write /LIBPATH:... without necessity to list .lib files
UPD: Ive checked solution, it doesn't work - at least the same mistakes remain without new informationwrote on 10 Jun 2021, 21:21 last edited by JonB 6 Oct 2021, 21:21@Sirzhatina
I don't know how that worked. I don't actually use MSVC :)You should test what I said in case I'm talking rubbish!
However, I seem to recall that there might be
cl
command line options which cause it to stamp generated.obj
files with what.lib
files they reference. And then use that information when linking. Could that be different in your compiling of Boost?This question https://stackoverflow.com/questions/24813060/how-do-i-link-boost-to-my-program/24813134 shows Boost needing linking with
gcc
(ld
) at least. It also states that most(?) of Boost is header-only. Could your usage happen to fall under this? -
wrote on 10 Jun 2021, 21:31 last edited by JoeCFD 6 Oct 2021, 21:31
https://download.qt.io/official_releases/vsaddin/2.7.1/
download this and add it into vs. Then create your qt project. -
@Sirzhatina
I don't know how that worked. I don't actually use MSVC :)You should test what I said in case I'm talking rubbish!
However, I seem to recall that there might be
cl
command line options which cause it to stamp generated.obj
files with what.lib
files they reference. And then use that information when linking. Could that be different in your compiling of Boost?This question https://stackoverflow.com/questions/24813060/how-do-i-link-boost-to-my-program/24813134 shows Boost needing linking with
gcc
(ld
) at least. It also states that most(?) of Boost is header-only. Could your usage happen to fall under this?wrote on 10 Jun 2021, 21:31 last edited by@JonB thank you for help, but I'm ok with Boost. It was just an example, that I didn't need to list .lib files excplicitly.
most(?) of Boost is header-only.
I meant exactly separately-compiled part:)
So, the problem isn't out yet -
wrote on 10 Jun 2021, 21:57 last edited by
Well if you promise only to use the MSVC compiler, then it's easy:
#include "C:/Qt/5.15.2/msvc2019/include/QtWidgets/qapplication.h" #include "C:/Qt/5.15.2/msvc2019/include/QtWidgets/qmessagebox.h" #pragma comment( lib, "C:/Qt/5.15.2/msvc2019/lib/qt5core" ) #pragma comment( lib, "C:/Qt/5.15.2/msvc2019/lib/qt5widgets" ) int main(int argc, char* argv[]) { QApplication a(argc, argv); QMessageBox::information(nullptr,"Look Ma no Qt Creator","Hello from the command line"); return a.exec(); }
-
https://download.qt.io/official_releases/vsaddin/2.7.1/
download this and add it into vs. Then create your qt project.wrote on 10 Jun 2021, 21:57 last edited by@JoeCFD links don't work
-
@JoeCFD links don't work
wrote on 10 Jun 2021, 22:05 last edited by JoeCFD 6 Oct 2021, 22:07@Sirzhatina Have you installed any qt vs addin? If not, you have problem to compile qt code with VS. The link is for you to download it for installation. The links works fine for me.
If you still can not open the link, look for: Qt vs addin download in google. Find a place to download it. -
Well if you promise only to use the MSVC compiler, then it's easy:
#include "C:/Qt/5.15.2/msvc2019/include/QtWidgets/qapplication.h" #include "C:/Qt/5.15.2/msvc2019/include/QtWidgets/qmessagebox.h" #pragma comment( lib, "C:/Qt/5.15.2/msvc2019/lib/qt5core" ) #pragma comment( lib, "C:/Qt/5.15.2/msvc2019/lib/qt5widgets" ) int main(int argc, char* argv[]) { QApplication a(argc, argv); QMessageBox::information(nullptr,"Look Ma no Qt Creator","Hello from the command line"); return a.exec(); }
wrote on 10 Jun 2021, 22:17 last edited by@hskoglund said in Cannot link Qt libs to C++ project manually.:
#pragma comment( lib, "C:/Qt/5.15.2/msvc2019/lib/qt5core" )
Thanks, that was the MSVC I was trying to think of when I said
which cause it to stamp generated
.obj
files with what.lib
files they reference -
@hskoglund said in Cannot link Qt libs to C++ project manually.:
#pragma comment( lib, "C:/Qt/5.15.2/msvc2019/lib/qt5core" )
Thanks, that was the MSVC I was trying to think of when I said
which cause it to stamp generated
.obj
files with what.lib
files they reference -
@JonB Haven't used that #pragma directive in a loooong time, it's probably fallen out of fashion nowadays. But it still works if you need it :-)
wrote on 11 Jun 2021, 06:51 last edited by JonB 6 Nov 2021, 06:51@hskoglund
It was just a thought. I was trying to think how come the OP claims that when he used Boost with MSVC he did not have to add anyboost....lib
onto his link line and yet it "worked". My experience/understanding is that you do have to add the.lib
s you want when using MSVC (e.g. for the Qt libraries), it is not enough to just specify/LIBPATH:...
and miraculously the linker knows to look in which files. Am I right? Please read my posts above and comment if I am, or if I have misled the OP? -
@JonB some of boost is header-only so there's nothing to link. He may have been lucky and only used these parts of boost.
-
@JonB some of boost is header-only so there's nothing to link. He may have been lucky and only used these parts of boost.
@SGaist Boost is also using this (imo) crappy msvc auto-link feature. It gaves me a lot of headaches due to static linking and the fact the the sources are also build on linux.
-
@JonB some of boost is header-only so there's nothing to link. He may have been lucky and only used these parts of boost.
wrote on 11 Jun 2021, 09:18 last edited by JonB 6 Nov 2021, 09:20@SGaist said in Cannot link Qt libs to C++ project manually.:
@JonB some of boost is header-only so there's nothing to link. He may have been lucky and only used these parts of boost.
Which is why I wrote earlier
It also states that most(?) of Boost is header-only. Could your usage happen to fall under this?
But the OP replied
I meant exactly separately-compiled part:)
So, the problem isn't out yet
Hence I don't know!
@Christian-Ehrlicher said in Cannot link Qt libs to C++ project manually.:
@SGaist Boost is also using this (imo) crappy msvc auto-link feature. It gaves me a lot of headaches due to static linking and the fact the the sources are also build on linux.
Which I also suggested to the OP might be why Boost but not Qt works without specifying the
.lib
s! :) -
@JonB some of boost is header-only so there's nothing to link. He may have been lucky and only used these parts of boost.
wrote on 11 Jun 2021, 09:31 last edited by@SGaist It's really interesting, so let's make it clear.
I build the next codeboost::asio::io_context io; boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5)); std::cout << "Hey "; t.wait(); std::cout << "lala ley ";
And without next command
/link /LIBPATH:C:\Program Files (x86)\Boost\boost_1_76_0\stage\lib
I get linker error (no date-time library and so on - it's separately-compiled).
And, of course, adding this command, I have the program built successfully. So, as you can see I have no need to link exact .lib file excplicitly.
1/23