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. How to link an .obj-File in the build process of Qt (qmake)
QtWS25 Last Chance

How to link an .obj-File in the build process of Qt (qmake)

Scheduled Pinned Locked Moved Solved General and Desktop
link errorlink .obj file
13 Posts 4 Posters 7.3k Views
  • 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.
  • M Offline
    M Offline
    MarekG
    wrote on last edited by
    #1

    Hello all,

    I have an issue while using an .obj-file.
    I have a header file that I included in my Qt Console Application. I implemented just one random function declared in the header file, to see, if it works. I added the .obj-file, I received together with the header file, but now, I don't know how to tell Qt, to use this file as well in the build process.
    Compiling it in the command.exe of windows without using Qt, it worked fine.

    #include <QCoreApplication>
    #include <windows.h> // Essential for ni488.h
    #include "ni488.h" // provided by a third party
    
    int main(int argc, char *argv[])
    {
        unsigned int test; //test variable
    
        QCoreApplication a(argc, argv); 
        test = ibclr(1); /*Just a random function, declared in the header file. This line creates the linker Error -> LNK2019: unresolved external symbol _ibclr@4 referenced in function _main*/
        return a.exec(); 
    }
    

    They also provided at .lib and a .obj file, but using these, it doesn't work as well. Might it be a problem that I use Qt-Creator for 64 bit but compile with MSVC2015 32bit?

    aha_1980A 1 Reply Last reply
    0
    • K Offline
      K Offline
      karlheinzreichel
      wrote on last edited by
      #2

      How does your project file looks like ?

      regards
      karl-heinz

      M 1 Reply Last reply
      0
      • M MarekG

        Hello all,

        I have an issue while using an .obj-file.
        I have a header file that I included in my Qt Console Application. I implemented just one random function declared in the header file, to see, if it works. I added the .obj-file, I received together with the header file, but now, I don't know how to tell Qt, to use this file as well in the build process.
        Compiling it in the command.exe of windows without using Qt, it worked fine.

        #include <QCoreApplication>
        #include <windows.h> // Essential for ni488.h
        #include "ni488.h" // provided by a third party
        
        int main(int argc, char *argv[])
        {
            unsigned int test; //test variable
        
            QCoreApplication a(argc, argv); 
            test = ibclr(1); /*Just a random function, declared in the header file. This line creates the linker Error -> LNK2019: unresolved external symbol _ibclr@4 referenced in function _main*/
            return a.exec(); 
        }
        

        They also provided at .lib and a .obj file, but using these, it doesn't work as well. Might it be a problem that I use Qt-Creator for 64 bit but compile with MSVC2015 32bit?

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by aha_1980
        #3

        Hi @MarekG, yes you need to link to ni4882.obj. If you use a 32-bit compiler then you need to link against a 32 bit object file.

        I have done this a long time ago, and I successfully used the following link command for MSVC in my project.pro file:

        QMAKE_LIBDIR += /path/to/objectfile
        LIBS += ni4882.obj
        

        However, this is not the recommended way, which would be:

        LIBS += -L/path/to/objectfile -lni4882
        

        To get this to link I had to rename ni4882.obj to ni4882.lib, and I have not tested this version yet.

        Edit: version 2 works.

        Qt has to stay free or it will die.

        M 1 Reply Last reply
        0
        • K karlheinzreichel

          How does your project file looks like ?

          regards
          karl-heinz

          M Offline
          M Offline
          MarekG
          wrote on last edited by
          #4

          @karlheinzreichel

          QT -= gui
          
          CONFIG += c++11 console
          CONFIG -= app_bundle
          
          # The following define makes your compiler emit warnings if you use
          # any feature of Qt which as been marked deprecated (the exact warnings
          # depend on your compiler). Please consult the documentation of the
          # deprecated API in order to know how to port your code away from it.
          DEFINES += QT_DEPRECATED_WARNINGS
          
          # You can also make your code fail to compile if you use deprecated APIs.
          # In order to do so, uncomment the following line.
          # You can also select to disable deprecated APIs only up to a certain version of Qt.
          #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
          
          SOURCES += main.cpp
          
          DISTFILES += \
              gpib-32.obj
          
          HEADERS += \
              ni488.h
          
          1 Reply Last reply
          0
          • aha_1980A aha_1980

            Hi @MarekG, yes you need to link to ni4882.obj. If you use a 32-bit compiler then you need to link against a 32 bit object file.

            I have done this a long time ago, and I successfully used the following link command for MSVC in my project.pro file:

            QMAKE_LIBDIR += /path/to/objectfile
            LIBS += ni4882.obj
            

            However, this is not the recommended way, which would be:

            LIBS += -L/path/to/objectfile -lni4882
            

            To get this to link I had to rename ni4882.obj to ni4882.lib, and I have not tested this version yet.

            Edit: version 2 works.

            M Offline
            M Offline
            MarekG
            wrote on last edited by
            #5

            @aha_1980
            Okay, again a bit slower.
            I have to rename my gpib-32.obj to gpib-32.lib and then add it as a library?
            That doesn't help.

            You talked about the ni4882.obj. I have that as well together with nisyscfg.lib. But changing ni4882.obj to ni4882.lib and adding this doesn't help as well. For that I created another project and included ni4882.h and used the same code.

            aha_1980A 1 Reply Last reply
            0
            • M MarekG

              @aha_1980
              Okay, again a bit slower.
              I have to rename my gpib-32.obj to gpib-32.lib and then add it as a library?
              That doesn't help.

              You talked about the ni4882.obj. I have that as well together with nisyscfg.lib. But changing ni4882.obj to ni4882.lib and adding this doesn't help as well. For that I created another project and included ni4882.h and used the same code.

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by aha_1980
              #6

              Hi @MarekG,

              @aha_1980
              Okay, again a bit slower.

              Well...

              I have to rename my gpib-32.obj to gpib-32.lib and then add it as a library?
              That doesn't help.

              I didn't use gpib-32 but used ni4882 instead. Don't know if there is much difference.

              You talked about the ni4882.obj. I have that as well together with nisyscfg.lib. But changing ni4882.obj to ni4882.lib and adding this doesn't help as well. For that I created another project and included ni4882.h and used the same code.

              How did you add it? When I added the line LIBS += -L/path/to/objectfile -lni4882 in my pro file it only worked for me if I renamed ni4882.obj to ni4882.lib.

              Which error do you get? (Or better: post the compiler and linker commands from the compile output).

              Qt has to stay free or it will die.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MarekG
                wrote on last edited by
                #7

                Hi @aha_1980 ,

                okay, I made a new Project with ni4882.h

                #include <QCoreApplication>
                #include <windows.h>
                #include "ni4882.h"
                
                int main(int argc, char *argv[])
                {
                    QCoreApplication a(argc, argv);
                    unsigned int test;
                    test = ibconfig (1,2,3);
                
                    return a.exec();
                }
                

                And my project file:

                QT -= gui
                
                CONFIG += c++11 console
                CONFIG -= app_bundle
                
                # The following define makes your compiler emit warnings if you use
                # any feature of Qt which as been marked deprecated (the exact warnings
                # depend on your compiler). Please consult the documentation of the
                # deprecated API in order to know how to port your code away from it.
                DEFINES += QT_DEPRECATED_WARNINGS
                
                # You can also make your code fail to compile if you use deprecated APIs.
                # In order to do so, uncomment the following line.
                # You can also select to disable deprecated APIs only up to a certain version of Qt.
                #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                
                SOURCES += main.cpp \
                    main.cpp
                
                HEADERS += \
                    ni4882.h
                
                LIBS += -L/. -lni4882
                

                I also tried to add the library by right click and "Add library". But it doesn't change anything.

                Still the same Linker Error: LNK2019 unresolved external symbol _ibconfig@12 referenced in function _main

                aha_1980A 1 Reply Last reply
                0
                • M MarekG

                  Hi @aha_1980 ,

                  okay, I made a new Project with ni4882.h

                  #include <QCoreApplication>
                  #include <windows.h>
                  #include "ni4882.h"
                  
                  int main(int argc, char *argv[])
                  {
                      QCoreApplication a(argc, argv);
                      unsigned int test;
                      test = ibconfig (1,2,3);
                  
                      return a.exec();
                  }
                  

                  And my project file:

                  QT -= gui
                  
                  CONFIG += c++11 console
                  CONFIG -= app_bundle
                  
                  # The following define makes your compiler emit warnings if you use
                  # any feature of Qt which as been marked deprecated (the exact warnings
                  # depend on your compiler). Please consult the documentation of the
                  # deprecated API in order to know how to port your code away from it.
                  DEFINES += QT_DEPRECATED_WARNINGS
                  
                  # You can also make your code fail to compile if you use deprecated APIs.
                  # In order to do so, uncomment the following line.
                  # You can also select to disable deprecated APIs only up to a certain version of Qt.
                  #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                  
                  SOURCES += main.cpp \
                      main.cpp
                  
                  HEADERS += \
                      ni4882.h
                  
                  LIBS += -L/. -lni4882
                  

                  I also tried to add the library by right click and "Add library". But it doesn't change anything.

                  Still the same Linker Error: LNK2019 unresolved external symbol _ibconfig@12 referenced in function _main

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by aha_1980
                  #8

                  @MarekG said in How to link an .obj-File in the build process of Qt (qmake):

                  LIBS += -L/. -lni4882

                  Where is your ni4882.lib located? In the source directory? That does not help for shadow builds :(

                  Try:

                  LIBS += -L$$PWD -lni4882

                  It must work :)

                  Qt has to stay free or it will die.

                  M 1 Reply Last reply
                  1
                  • aha_1980A aha_1980

                    @MarekG said in How to link an .obj-File in the build process of Qt (qmake):

                    LIBS += -L/. -lni4882

                    Where is your ni4882.lib located? In the source directory? That does not help for shadow builds :(

                    Try:

                    LIBS += -L$$PWD -lni4882

                    It must work :)

                    M Offline
                    M Offline
                    MarekG
                    wrote on last edited by
                    #9

                    @aha_1980 And again :(

                    I tried

                    LIBS += -L$$PWD -lni4882
                    

                    -> It didn't work.

                    I tried to copy the lib-file into the debug folder -> It didn't work.
                    I tried to add it by right click when it's in the debug folder:

                    win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/release/ -lni4882
                    else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/debug/ -lni4882
                    
                    INCLUDEPATH += $$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/debug
                    DEPENDPATH += $$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/debug
                    
                    win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/release/libni4882.a
                    else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/debug/libni4882.a
                    else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/release/ni4882.lib
                    else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/debug/ni4882.lib
                    

                    And when it's in the source directory:

                    win32: LIBS += -L$$PWD/./ -lni4882
                    
                    INCLUDEPATH += $$PWD/.
                    DEPENDPATH += $$PWD/.
                    
                    win32:!win32-g++: PRE_TARGETDEPS += $$PWD/./ni4882.lib
                    else:win32-g++: PRE_TARGETDEPS += $$PWD/./libni4882.a
                    

                    But nothing helps :(

                    aha_1980A 1 Reply Last reply
                    0
                    • M MarekG

                      @aha_1980 And again :(

                      I tried

                      LIBS += -L$$PWD -lni4882
                      

                      -> It didn't work.

                      I tried to copy the lib-file into the debug folder -> It didn't work.
                      I tried to add it by right click when it's in the debug folder:

                      win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/release/ -lni4882
                      else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/debug/ -lni4882
                      
                      INCLUDEPATH += $$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/debug
                      DEPENDPATH += $$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/debug
                      
                      win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/release/libni4882.a
                      else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/debug/libni4882.a
                      else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/release/ni4882.lib
                      else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../build-test_dg2030_1-Desktop_Qt_5_10_1_MSVC2015_32bit-Debug/debug/ni4882.lib
                      

                      And when it's in the source directory:

                      win32: LIBS += -L$$PWD/./ -lni4882
                      
                      INCLUDEPATH += $$PWD/.
                      DEPENDPATH += $$PWD/.
                      
                      win32:!win32-g++: PRE_TARGETDEPS += $$PWD/./ni4882.lib
                      else:win32-g++: PRE_TARGETDEPS += $$PWD/./libni4882.a
                      

                      But nothing helps :(

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @MarekG

                      No, much to complicated - it's really that simple as I posted.

                      Please post the compiler&linker output -> we need to know where the linker is searching for the additional files.

                      And one hint: avoid '/' at the end of a path -> it converts to a backslash in Windows and I have seen strange things happen.

                      I need to leave now. Good luck!

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        MarekG
                        wrote on last edited by
                        #11

                        @aha_1980

                        Okay, I don't know what happened, but I created another project and tried everything again. It worked now without any error :)

                        So to summarize: I included "ni4882.h" and added the library "ni4882.lib" (renamed from ni4882.obj)

                        Thank you everyone for your help ;)

                        1 Reply Last reply
                        3
                        • mranger90M Offline
                          mranger90M Offline
                          mranger90
                          wrote on last edited by
                          #12

                          Does adding something like this to your profile work ?

                          OBJECTS_DIR += $$PWD
                          OBJECTS += objtest.o
                          

                          Obviously I tried this on Linux, so drop the ".o" and you may need to replace it with ".obj"

                          aha_1980A 1 Reply Last reply
                          0
                          • mranger90M mranger90

                            Does adding something like this to your profile work ?

                            OBJECTS_DIR += $$PWD
                            OBJECTS += objtest.o
                            

                            Obviously I tried this on Linux, so drop the ".o" and you may need to replace it with ".obj"

                            aha_1980A Offline
                            aha_1980A Offline
                            aha_1980
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @mranger90 said in How to link an .obj-File in the build process of Qt (qmake):

                            Does adding something like this to your profile work ?

                            OBJECTS_DIR += $$PWD
                            OBJECTS += objtest.o
                            

                            Obviously I tried this on Linux, so drop the ".o" and you may need to replace it with ".obj"

                            Hi @mranger90,

                            ni4882.obj is not really a object, but a linker input file. The real functions are in ni4882.dll. So using LIBS is the correct way, the only strange thing is that the linker input file uses the extension .obj. But this can easily be fixed by renaming.

                            Regards.

                            Qt has to stay free or it will die.

                            1 Reply Last reply
                            1

                            • Login

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