Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot link Qt libs to C++ project manually.
Forum Updated to NodeBB v4.3 + New Features

Cannot link Qt libs to C++ project manually.

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 6 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Sirzhatina
    10 Jun 2021, 20:30

    @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

    J Offline
    J Offline
    JonB
    wrote on 10 Jun 2021, 20:44 last edited by JonB 6 Oct 2021, 20:47
    #4

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

    S 1 Reply Last reply 10 Jun 2021, 20:54
    0
    • J JonB
      10 Jun 2021, 20:44

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

      S Offline
      S Offline
      Sirzhatina
      wrote on 10 Jun 2021, 20:54 last edited by
      #5

      @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?

      J 1 Reply Last reply 10 Jun 2021, 21:05
      0
      • S Sirzhatina
        10 Jun 2021, 20:54

        @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?

        J Offline
        J Offline
        JonB
        wrote on 10 Jun 2021, 21:05 last edited by
        #6

        @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 just Qt5Widgets library, you will need whatever it depends on.

        This is all why most people allow Creator or cmake/qmake to do the work.

        S 1 Reply Last reply 10 Jun 2021, 21:14
        0
        • J JonB
          10 Jun 2021, 21:05

          @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 just Qt5Widgets library, you will need whatever it depends on.

          This is all why most people allow Creator or cmake/qmake to do the work.

          S Offline
          S Offline
          Sirzhatina
          wrote on 10 Jun 2021, 21:14 last edited by Sirzhatina 6 Oct 2021, 21:19
          #7

          @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

          J 1 Reply Last reply 10 Jun 2021, 21:21
          0
          • S Sirzhatina
            10 Jun 2021, 21:14

            @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

            J Offline
            J Offline
            JonB
            wrote on 10 Jun 2021, 21:21 last edited by JonB 6 Oct 2021, 21:21
            #8

            @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?

            S 1 Reply Last reply 10 Jun 2021, 21:31
            0
            • J Offline
              J Offline
              JoeCFD
              wrote on 10 Jun 2021, 21:31 last edited by JoeCFD 6 Oct 2021, 21:31
              #9

              https://download.qt.io/official_releases/vsaddin/2.7.1/
              download this and add it into vs. Then create your qt project.

              S 1 Reply Last reply 10 Jun 2021, 21:57
              0
              • J JonB
                10 Jun 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?

                S Offline
                S Offline
                Sirzhatina
                wrote on 10 Jun 2021, 21:31 last edited by
                #10

                @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

                1 Reply Last reply
                0
                • H Online
                  H Online
                  hskoglund
                  wrote on 10 Jun 2021, 21:57 last edited by
                  #11

                  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();
                  }
                  
                  
                  J 1 Reply Last reply 10 Jun 2021, 22:17
                  1
                  • J JoeCFD
                    10 Jun 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.

                    S Offline
                    S Offline
                    Sirzhatina
                    wrote on 10 Jun 2021, 21:57 last edited by
                    #12

                    @JoeCFD links don't work

                    J 1 Reply Last reply 10 Jun 2021, 22:05
                    0
                    • S Sirzhatina
                      10 Jun 2021, 21:57

                      @JoeCFD links don't work

                      J Offline
                      J Offline
                      JoeCFD
                      wrote on 10 Jun 2021, 22:05 last edited by JoeCFD 6 Oct 2021, 22:07
                      #13

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

                      1 Reply Last reply
                      0
                      • H hskoglund
                        10 Jun 2021, 21:57

                        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();
                        }
                        
                        
                        J Offline
                        J Offline
                        JonB
                        wrote on 10 Jun 2021, 22:17 last edited by
                        #14

                        @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

                        H 1 Reply Last reply 10 Jun 2021, 23:20
                        0
                        • J JonB
                          10 Jun 2021, 22:17

                          @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

                          H Online
                          H Online
                          hskoglund
                          wrote on 10 Jun 2021, 23:20 last edited by
                          #15

                          @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 :-)

                          J 1 Reply Last reply 11 Jun 2021, 06:51
                          0
                          • H hskoglund
                            10 Jun 2021, 23:20

                            @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 :-)

                            J Offline
                            J Offline
                            JonB
                            wrote on 11 Jun 2021, 06:51 last edited by JonB 6 Nov 2021, 06:51
                            #16

                            @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 any boost....lib onto his link line and yet it "worked". My experience/understanding is that you do have to add the .libs 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?

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 11 Jun 2021, 09:13 last edited by
                              #17

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

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              C J S 3 Replies Last reply 11 Jun 2021, 09:17
                              0
                              • S SGaist
                                11 Jun 2021, 09:13

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

                                C Offline
                                C Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on 11 Jun 2021, 09:17 last edited by
                                #18

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

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                1 Reply Last reply
                                0
                                • S SGaist
                                  11 Jun 2021, 09:13

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

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 11 Jun 2021, 09:18 last edited by JonB 6 Nov 2021, 09:20
                                  #19

                                  @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 .libs! :)

                                  1 Reply Last reply
                                  0
                                  • S SGaist
                                    11 Jun 2021, 09:13

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

                                    S Offline
                                    S Offline
                                    Sirzhatina
                                    wrote on 11 Jun 2021, 09:31 last edited by
                                    #20

                                    @SGaist It's really interesting, so let's make it clear.
                                    I build the next code

                                    boost::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.

                                    C 1 Reply Last reply 11 Jun 2021, 09:35
                                    0
                                    • S Sirzhatina
                                      11 Jun 2021, 09:31

                                      @SGaist It's really interesting, so let's make it clear.
                                      I build the next code

                                      boost::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.

                                      C Offline
                                      C Offline
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on 11 Jun 2021, 09:35 last edited by
                                      #21

                                      @Sirzhatina said in Cannot link Qt libs to C++ project manually.:

                                      So, as you can see I have no need to link exact .lib file excplicitly.

                                      We already explained why this works... but Qt does fortunately not support this.

                                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                      Visit the Qt Academy at https://academy.qt.io/catalog

                                      S 1 Reply Last reply 11 Jun 2021, 09:41
                                      2
                                      • C Christian Ehrlicher
                                        11 Jun 2021, 09:35

                                        @Sirzhatina said in Cannot link Qt libs to C++ project manually.:

                                        So, as you can see I have no need to link exact .lib file excplicitly.

                                        We already explained why this works... but Qt does fortunately not support this.

                                        S Offline
                                        S Offline
                                        Sirzhatina
                                        wrote on 11 Jun 2021, 09:41 last edited by Sirzhatina 6 Nov 2021, 09:45
                                        #22

                                        @Christian-Ehrlicher okay, thanks. But you said that's what msvc does. And I still use msvc to compile Qt libs, so what's the mystery here? I mean why does msvc auto-link one libraries and not auto-link others

                                        C 1 Reply Last reply 11 Jun 2021, 09:50
                                        0
                                        • S Sirzhatina
                                          11 Jun 2021, 09:41

                                          @Christian-Ehrlicher okay, thanks. But you said that's what msvc does. And I still use msvc to compile Qt libs, so what's the mystery here? I mean why does msvc auto-link one libraries and not auto-link others

                                          C Offline
                                          C Offline
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on 11 Jun 2021, 09:50 last edited by
                                          #23

                                          @Sirzhatina said in Cannot link Qt libs to C++ project manually.:

                                          I mean why does msvc auto-link one libraries and not auto-link others

                                          see the answer from @hskoglund - this is basically what the boost libs are doing and Qt not (because it's crappy, error-prone and non-portable)

                                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                          Visit the Qt Academy at https://academy.qt.io/catalog

                                          1 Reply Last reply
                                          2

                                          13/23

                                          10 Jun 2021, 22:05

                                          • Login

                                          • Login or register to search.
                                          13 out of 23
                                          • First post
                                            13/23
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved