How to add -pthread compiler option in Qt?



  • OS: Windows 7
    Compiler: Qt 5.11.2 MinGW 32-bit
    Program compiled in debug.

    I am using Botan library recently to create a ECDSA_PrivateKey object. But the compiler is complaining about the undefined reference to 'std::thread::_State::~_State()' in rsa.cpp and thread_pool.cpp, both from botan.

    0_1557852309035_Capture.PNG

    This is how I added the botan library:
    0_1557851716346_Capture.PNG

    Code:

    #include <QCoreApplication>
    #include <botan/pubkey.h>
    #include <botan/ec_group.h>
    #include <botan/ecdsa.h>
    #include <iostream>
    #include <botan/pkcs8.h>
    #include <string.h>
    
    using std::cout;
    using std::endl;
    
    
    int main(int argc, char *argv[])
    {
    
        QCoreApplication a(argc, argv);
    
        std::unique_ptr<Botan::RandomNumberGenerator> random(new Botan::System_RNG);
    
        std::unique_ptr<Botan::EC_Group> group(new Botan::EC_Group("secp256r1"));
    
        std::unique_ptr<Botan::ECDSA_PrivateKey> aKey(new Botan::ECDSA_PrivateKey(*random,*group));
    
        std::string encoded;
    
        // this line caused the error.
        encoded = Botan::PKCS8::PEM_encode(*aKey);
    
        return a.exec();
    }
    
    

    According to randombit: "Botan uses threads internally, you may have to add -pthread to gcc command line."

    Now I know -pthread is a compiler option but when I try to use the option in cmd like: qmake -pthread, it says ***unknown option -pthread

    Is -pthread really an option in Qt? And CONFIG += thread in .pro doesn't work. Need some help here, thanks in advance.


  • Lifetime Qt Champion

    Hi,

    Add LIBS += -lpthread to your .pro file.

    [Edit aha_1980: Fix typo]



  • It doesn't work, the compiler is still complaining the same undefined reference.

    .pro 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
    
    QMAKE_LFLAGS +=-fstack-protector
    
    LIBS +=-lpthread
    
    
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    unix|win32: LIBS += -L$$PWD/../../../Enzo/Library/botan-master/botan-master/ -lbotan-2
    
    INCLUDEPATH += $$PWD/../../../Enzo/Library/botan-master/botan-master/build/include
    DEPENDPATH += $$PWD/../../../Enzo/Library/botan-master/botan-master/build/include
    
    win32:!win32-g++: PRE_TARGETDEPS += $$PWD/../../../Enzo/Library/botan-master/botan-master/botan-2.lib
    else:unix|win32-g++: PRE_TARGETDEPS += $$PWD/../../../Enzo/Library/botan-master/botan-master/libbotan-2.a
    
    


  • I built the Botan library using my own MinGW 8.1.0 instead of Qt's MinGW, does that matter?


  • Qt Champions 2018

    @BadPistol97 said in How to add -pthread compiler option in Qt?:

    I built the Botan library using my own MinGW 8.1.0 instead of Qt's MinGW, does that matter?

    Why don't you just try building it with the same version?

    According to randombit: "Botan uses threads internally, you may have to add -pthread to gcc command line."

    I think you can add this by QMAKE_CXXFLAGS += -pthread (QMAKE_CLFLAGS for C files resp.) to your .pro file.

    However, I think you're just not linking to the correct library. Find out which one is exporting exactly the symbols you're missing.

    At least, this line: LIBS +=-lpthread just links against libpthread.so. Where do you link against botan?

    Regards



  • @aha_1980 I have rebuilt the botan library using Qt's own MinGW compiler and now it works.

    But the botan-test stuck when testing tls:
    0_1558338073284_Capture.PNG


  • Qt Champions 2018

    @BadPistol97 I'd say run it in a debugger, and when it crashes send the stack trace to the botan developers.

    They might be best telling you what goes wrong and if it is harmful.

    Regards



  • Thank you very much.


Log in to reply
 

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