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

Importing libraries and including them still gives undefined references



  • I'm quite new to c++ and Qt in general, so I might have made some obvious mistake here, but I spent the last couple hours trying to figure that out and still no luck. He're the code in question, from the bit7z library:

    try {
            bit7z::Bit7zLibrary lib( L"7za.dll" );
            bit7z::BitExtractor extractor( lib, bit7z::BitFormat::Zip );
            //Trimming extension
            QString subDirectory = "sub/" +
                    QString::fromStdString(fileName
                               .toStdString().substr(0,
                                           fileName.toStdString().find_last_of(".")));
    
            CreateDirectoryA(subDirectory.toLocal8Bit().data(), nullptr);
            extractor.extract(L"sub/" + fileName.toStdWString(), subDirectory.toStdWString());
    
        }catch (const bit7z::BitException& ex ) {
            qDebug() << ex.what();
        }
    

    Getting undefined references on every instance of bit7z... I know the string manipulation are probably not good, but I'm used to python where everything "" is a string, while here you have char* char[] std::string QString wstring wchar* and such... Every function takes a different type of string and none of theses are compatible...
    The .pro file:

    ...
    ...
    DISTFILES +=
    
    win32:CONFIG(release, debug|release): LIBS += -L$$PWD/bit7z/lib/ -lbit7z64
    else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/bit7z/lib/ -lbit7z64_d
    else:unix: LIBS += -L$$PWD/bit7z/lib/ -lbit7z64
    
    INCLUDEPATH += $$PWD/bit7z/include
    DEPENDPATH += $$PWD/bit7z/include
    

    What am I missing here? I don't see any error in the IDE until compilation. I've added the library with the "Add library" option when you right click and "external library".


  • Lifetime Qt Champion

    So what error do you get? Since it's a C++ library - is Qt and the library compiled with the same compiler? You can't mix msvc and mingw here.


  • Lifetime Qt Champion

    Hi @RekTekk249,

    Just one addition to @Christian-Ehrlicher: Your program needs the same archticture (bitness) as your library, please check that too.

    Regarding the second point: indeed string handling is horrible in C and only partly better in C++; but with QString you have a mighty and locale aware toolbox.

       QString subDirectory = "sub/" +
                QString::fromStdString(fileName
                           .toStdString().substr(0,
                                       fileName.toStdString().find_last_of(".")));
    

    So this can be done better (uncompiled + untested):

    const QString subDirectory = "sub/" + fileName.mid(0, fileName.lastIndexOf('.'));

    CreateDirectoryA(subDirectory.toLocal8Bit().data(), nullptr);

    Note that is is platform specific (Windows) API and only works for ASCII filenames. A bit better would be:

    CreateDirectoryW(reinterpret_cast<const wchar_t *>(subDirectory.utf16()), nullptr);

    extractor.extract(L"sub/" + fileName.toStdWString(), subDirectory.toStdWString());

    And here it depends on which parameters extract() expects.

    Hope that helps.

    Regards


  • Lifetime Qt Champion

    Hi,

    To add to @aha_1980 additions, QDir has QDir::mkdir which is cross-platform so you don't have to play with native API for that kind of action.



  • Yes, thanks for that, I wasn't aware that compilers weren't compatible... The library is open source, I'll just recompile it. Also, I heard of Qt::mkdir, but apparently it's doesn't have good performances, but I got this from a year old SO thread, it might have been better now.. I'll give it a try. I know CreateDirectoryW is a windows api function, but it was the simplest to use, and I had my fileName filtered with regex to be letters/numbers only for other reasons. For the strings, I was totally unaware of the QString.mid() function... I was looking on the docs for sub subs sub_str substring and stuff like that.. Thanks for that, I got it working now! That's the first time I encounter a language with multiple compilers like that... C and its variants seem to be the only one. Honestly though it feels like Qt is it's own language, more than a framework...


  • Lifetime Qt Champion

    @RekTekk249 said in Importing libraries and including them still gives undefined references:

    Also, I heard of Qt::mkdir, but apparently it's doesn't have good performances

    micro-optimization - you call it once... it's just not worth the trouble.


Log in to reply