serial port in dll

  • Lifetime Qt Champion

    Can you show the code where you pass that integer ?

  • @SGaist

    In the dll i specified the following function:
    void Test(int a);

    qDebug()<< a;

    in the Test program:
    int b=1;
    MyLib c ;

    The exact error message is:
    main.obj:-1: Error: LNK2019: unresolved symbol ""__declspec(dllimport) public: void __cdecl MyLib::Test(int)" (_imp?Test@MyLib@@QEAAXH@Z)" in function "main".

  • Lifetime Qt Champion

    You are not exporting Test properly

  • @SGaist
    How to do so ?
    Where is my fault ?

  • Lifetime Qt Champion

    You have a nice explanation in the Creating shared libraries chapter of Qt's documentation

  • @SGaist
    I red this chapter already and my header files as well as the pro file are as specified in the explanation:

    #include "messwert_zuordnung_dll_global.h"
    class MESSWERT_ZUORDNUNG_DLLSHARED_EXPORT Messwert_zuordnung_dll
        void Test(int a);


    #include <QtCore/qglobal.h>

    I cant see a big difference.

  • Lifetime Qt Champion

    Following your various piece of code, you don't have any class called MyLib containing Test(int) nor any namespace containing such a function

  • @SGaist
    I changed the name, because the actual is a bit long. My class is Messwert_zuordnung_dll.
    The function for the export is Test.

  • Lifetime Qt Champion

    Ok so your

    MyLib c;

    should rather be:

    Messwert_zuordnung_dll mzd;

    correct ?

    So, when you are building your dll, do you get both the dll and corresponding lib file ?

  • @SGaist
    Yes that´s correct. Sorry at this point for the confusion with the names.

    Yes the build creates a .dll/.lib and .obj file.
    At the beginning I had some trouble with the link I set as dependent path and libs in the pro file because there where a space in it. The error said "could not open .obj" so maybe it the program tries to open the .obj instead of the .dll ?

  • Lifetime Qt Champion

    No, you should link to the lib file. However having space in your path on Windows is generally a Bad Thing (™) so just avoid them altogether

  • @SGaist
    Yes I realized that this is a problem and renamed the folder. But this was just at the beginning, now it has to be an other problem

  • Lifetime Qt Champion

    It still can't find it ? Did you implement Test(int a) ?

  • @SGaist
    What exactly do you mean with implement ?
    Under the exportet class in "public" I added "void Test(int a)" and right clicked refractor and added the function to the cpp file.

  • Lifetime Qt Champion

    Then it's been implemented

  • @SGaist
    I created an other time a new project and did all step by step. After all the dll works. I wrote the dll and the test program exactly as in the files before. I don´t know where the difference is.

    I am able to pass an integer through the function of my dll.
    In the next step I would like to establish an serial port and send the passed integer to my mikrocontroller.
    The code whrer i implement the serial port looks like this:

    QSerialPort *serial;
    void Messwert_zuordnung_dll::Test(int a){
        serial = new QSerialPort();
        // COM-Port, checked in device manager!
        serial -> write(c);

    The connection can´t be opened and in the console of the test program I get the following error:
    "QObject::start timer: Timers can only be used with threads started with QThread"

    What means this? In my GUI i did not start a Thread seperatly. How can I solve this error?

  • Lifetime Qt Champion

    Are you calling that function from your application main thread ?

    Note that you have a memory leak since you don't delete serial

  • @SGaist
    Ok I will delet the serial object.

    I call the dll function in the main.cpp of my test program.
    I tried to create the QSerialPort *serial and the object serial=new QSerialPort in the main.cpp of my test program and pass it to the dll function, so that in my dll the object created in the test program is used. But the connection can´t be opened.

  • Lifetime Qt Champion

    What do you mean by "connection can't be opened" ? What error did you get ?

  • @SGaist
    If i pass the object(QSerialPort) through the function to the dll, the test program stuck and there is no error written in the console.

    If i create the object (QSerialPort) in the dll the following error is shown:
    "QObject::start timer: Timers can only be used with threads started with QThread"

Log in to reply