Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

32 bit VC library with Qt



  • Hi:
    I have this compilation error:

    widget.obj:-1: error: LNK2019: unresolved external symbol "int __cdecl ReadSymbol(char const *,char const *,void *)" (?ReadSymbol@@YAHPEBD0PEAX@Z)
    referenced in function "private: void __cdecl Widget::on_pushButton_clicked(void)" (?on_pushButton_clicked@Widget@@AEAAXXZ)

    File not found : widget.obj

    Warning: LNK4272: library machine type 'X86' conflicts with target machine type 'x64'.

    Basically, i am converting a VS C# code to QT.
    here is the .pro file

    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = ttt
    TEMPLATE = app
    
    
    SOURCES += main.cpp\
            widget.cpp
    
    HEADERS  += widget.h
    
    FORMS    += widget.ui
    
    win32:LIBS += -L"$$_PRO_FILE_PWD_/3rdparty/libs/" -lProEasy
    

    here is the header

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    
    namespace Ui {
    class Widget;
    }
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Widget(QWidget *parent = 0);
        ~Widget();
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::Widget *ui;
    };
    
    #endif // WIDGET_H
    

    here is the main.cpp

    #include <Windows.h>
    #include "3rdparty/libs/ProEasy.h"
    
    #include "widget.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget w;
        w.show();
    
        return a.exec();
    }
    
    

    here is the widget cpp

    #include <Windows.h>
    #include "3rdparty/libs/ProEasy.h"
    #include <stdlib.h>
    #include "widget.h"
    #include "ui_widget.h"
    
    
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    void Widget::on_pushButton_clicked()
    {
        WORD wData[3];
    
    
    
       //ReadSymbol("PC1.#INTERNAL","TestQT_Text",wData);
    
        INT K;
        K= ReadSym("PC1.#INTERNAL","TestQT_Text",wData);
    }
    
    

    The library i am using is a VC library (3rd party)

    I had been web searching for a while, hope some one can provide some guidance.

    Thanks


  • Qt Champions 2017

    Hi,
    Please provide the link line from the building. It appears your library isn't linked, thus you get the unresolved external symbol. Why use $$_PRO_FILE_PWD_ at all? I'd recommend just a relative path, e.g: -L../3rdparty/libs

    Kind regards.



  • @kshegunov

    Hi:
    Even I use the absolute path, I am having the same problem as above. Any Idea?



    • was ProEasy compiled for 32 or 64 bit?
    • does ProEasy export ReadSym function?

  • Qt Champions 2017

    @Ongsk said in 32 bit VC library with QT:

    Any Idea?

    I don't see a link line. After you extract the linker command do just what @VRonin said: make sure the linked libraries (as per the link line) are compiled and linked with the same versions of the tools and are the same flavor (32bit vs 64 bits). After that make sure ReadSym is exported.



  • @VRonin

    I am Novice to C++, so please bear with me:

    I thought just by including the lib file, I am able to use the function? In fact the intellisense of QT Creator did show the function.

    Do I need to compiled it?

    By the way what is Link Line?

    Thanks


  • Qt Champions 2017

    @Ongsk said in 32 bit VC library with QT:

    I am Novice to C++, so please bear with me

    Oookay. I'll explain shortly and give you links to read further.

    The source code you write goes through several transformations before it becomes a program that is executed, namely: compiling[1, 2], linking[2, 3] and ultimately loading[4] (in that order). The compiler produces translation units (also called obj files) from your source code. On the next step those object files are "stitched" together by the linker to produce a binary image (either an executable file or a dynamic library). When you start the program the loader starts reading the binary and starts to assign addresses in memory to the symbols contained therein. Each function or a global variable will ultimately produce a symbol entry in the binary image (there are some conditions for this but they're unimportant here).

    So, each of these steps to arrive to a working program are performed by a program. Compilation is done with the help of a compiler. Linking is done by the linker. While building (the process of going through all the mentioned steps) the different tools are called with arguments. What we refer to as "compile line" is the command that's executed for compilation, the link line respectively is the command and arguments that is executed for the purposes of linking.

    For example this is a real-life compile line (from a project of mine):

    g++ -c -include .pch/Qt5Daemon -pipe --pedantic-errors -g -std=c++1z -fvisibility=hidden -fvisibility-inlines-hidden -fno-exceptions -Wall -W -D_REENTRANT -fPIC -DQT_NO_TSLIB -DQT_NO_FOREACH -DQT_NO_CAST_FROM_ASCII -DQT_NO_CAST_TO_ASCII -DQT_BUILD_DAEMON_LIB -DQT_BUILDING_QT -DQT_ASCII_CAST_WARNINGS -DQT_MOC_COMPAT -DQT_USE_QSTRINGBUILDER -DQT_DEPRECATED_WARNINGS -DQT_DISABLE_DEPRECATED_BEFORE=0x050000 -DQT_NO_EXCEPTIONS -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DQT_DBUS_LIB -DQT_CORE_LIB -I. -I. -I../../include -I../../include/QtDaemon -I../../include/QtDaemon/1.0.0 -I../../include/QtDaemon/1.0.0/QtDaemon -I/home/nye/Programming/C++/qt/5.6/include -I/home/nye/Programming/C++/qt/5.6/include/QtDBus -I/home/nye/Programming/C++/qt/5.6/include/QtCore -I.moc -I/home/nye/Programming/C++/qt/5.6/mkspecs/linux-g++ -o .obj/controllerbackend_linux.o controllerbackend_linux.cpp
    

    While this is the link line:

    g++ -Wl,--no-undefined -Wl,--version-script,QtDaemon.version -fuse-ld=gold -Wl,--enable-new-dtags -Wl,-z,origin -Wl,-rpath,\$ORIGIN -shared -Wl,-Bsymbolic-functions -Wl,-soname,libQt5Daemon.so.1 -o libQt5Daemon.so.1.0.0 .obj/qdaemonapplication.o .obj/qdaemonlog.o .obj/qdaemoncontroller.o .obj/qabstractdaemonbackend.o .obj/qdaemonstate.o .obj/qdaemoncontroller_linux.o .obj/qdaemondbusinterface.o .obj/qdaemon.o .obj/qdaemon_linux.o .obj/controllerbackend_linux.o .obj/daemonbackend_linux.o .obj/qrc_qdaemon.o .obj/moc_qdaemonapplication.o .obj/moc_qdaemoncontroller.o .obj/moc_qdaemondbusinterface_p.o .obj/moc_daemonbackend_linux.o  -L/home/nye/Programming/C++/qt/5.6/lib -lQt5DBus -lQt5Core -lpthread
    

    Both of these can be seen in Qt Creator's "Compile output" pane. The link line is the last g++ call (the linker may be called differently on different OSes), and I know it's linking because there is no -c flagged passed that'd signify compilation.

    Do I need to compiled it?

    Now, any external (dynamic) library you "link" needs to be compiled beforehand. So yes, you need to compile it or otherwise obtain the binary image for said library. Since you're using qmake as an intermediary for building (it's just a tool that simplifies the whole process described above) you have to make sure you have libProEasy.lib or ProEasy.lib located in $$_PRO_FILE_PWD_/3rdparty/libs (that's what win32:LIBS += -L"$$_PRO_FILE_PWD_/3rdparty/libs/" -lProEasy means).

    After that symbols and flavor (x86 vs x64) can be inspected with the help of yet another tool - dependency walker. But first thing's first - make sure you have the .lib file at the specified location.

    [1]: https://en.wikipedia.org/wiki/Compiler
    [2]: https://en.wikipedia.org/wiki/Translation_unit_(programming)
    [3]: https://en.wikipedia.org/wiki/Linker_(computing)
    [4]: https://en.wikipedia.org/wiki/Loader_(computing)



  • @kshegunov

    Hi:
    Before I continue to get myself into the "Reading Mess", ha ha.

    The said library is there, in fact there are 2, (import & export Lib) , also with C++ header :

    ![alt text](0_1475739662553_upload-1a0a3ce2-4d06-4310-af25-d541f378d819 image url)

    As you can see as above. Now If i am correct, I read some where in the forum stated that VC Library can be use by just specify the path and use the "Window.h"?

    ![alt text](0_1475739839699_upload-54481e21-097f-4c0b-8caa-c896ec44e00e image url)

    Qt Creator showed the function as above hence the link is there?

    Thanks



  • images don't work I'm afraid

    Qt Creator showed the function as above hence the link is there?

    Qt Creator only inspects the headers, it does not check if linking is valid until you try and build

    One further question, is ProEasy a static or dinamic library? (is there a .dll file?)



  • @VRonin

    Well, Every day I learn something new!

    there is no dll, only 2 lib files and c++ header. There is one dll but is for dotnet.


  • Qt Champions 2017

    First make sure you're compiling everything with the same compiler, then list the symbols (use dependency walker for both) and make sure you have the mentioned symbol there.



  • "First make sure you're compiling everything with the same compiler, then list the symbols (use dependency walker for both) and make sure you have the mentioned symbol there."

    Do you mean compile the above ccde? or Just the library?

    One Question: if compiling the library, how do i go about? Any guider?

    Thanks


  • Qt Champions 2017

    @Ongsk said in 32 bit VC library with Qt:

    Do you mean compile the above ccde? or Just the library?

    Both.

    @Ongsk said in 32 bit VC library with Qt:

    One Question: if compiling the library, how do i go about? Any guider?

    Depends on the library itself. Refer to the library documentation/site. Build instructions should be present there.



  • @kshegunov

    Ok:

    That mean compile compile the library in Visual C++ ?

    As the above codes are with error, aftre built, so I have to get the dll and link again in QT and try to build again?

    Thanks


Log in to reply