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



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



  • How does your project file looks like ?

    regards
    karl-heinz


  • Qt Champions 2017

    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.



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


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


  • Qt Champions 2017

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



  • 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


  • Qt Champions 2017

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



  • @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 :(


  • Qt Champions 2017

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



  • @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 ;)



  • 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"


  • Qt Champions 2017

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.