alan.sambol
alan.sambol

Reputation: 265

Copying precompiled libraries to build directory or adding them to PATH in a QBS project

I have a third party precompiled library (.lib+.dll) that I use in my Qt application.

In a regular (qmake) QtCreator project I have the following lines in my .pro file:

LIBS += -L$$PWD/lib/release -ltag
INCLUDEPATH += include/taglib

There is also an option in Projects tab -> Run -> "Add build library search path to PATH" which is by default ON. It ensures that LIBS path gets added to system PATH, so the dll can be found.

However, I can't find an equivalent in QBS. I have the following qbs file, which then gets included and added via Depends in my CppApplication file:

DynamicLibrary {
    name: "taglib"

    files: "lib/release/tag.dll"

    Export {
        Depends { name: "cpp" }
        cpp.includePaths: [".","include/taglib"]
        cpp.libraryPaths: ["lib/release"]
        cpp.dynamicLibraries: "tag"
    }

    Group {
        name: "taglib"
        fileTagsFilter: ["dynamicLibrary"]
        qbs.install: true
    }

}

The linker passes but the application can't find the DLL at runtime and crashes. Is it possible to add cpp.libraryPaths to system PATH at runtime?

Another option would be to copy the DLL file to build directory, but I can't figure out how to do that for precompiled libraries in QBS.

EDIT: I tried to use cpp.systemRunPaths which is documented here but it doesn't work.

Upvotes: 3

Views: 1753

Answers (2)

so2017
so2017

Reputation: 21

alan, your're on the right way. Just place

setupRunEnvironment: {
    var env;
    if (qbs.targetOS.contains('windows')) {
      env = new ModUtils.EnvironmentVariable("PATH", qbs.pathListSeparator, true);
      env.append(binPath);
      env.set();
    } 
}

in the DynamicLibrary {} block, below the last Group {}. Change binPath to point to the folder with your shared libraries. This works at least with Windows.

Maybe you need to move Depends { name: "cpp" } out of the Export block.

Upvotes: 2

alan.sambol
alan.sambol

Reputation: 265

I figured out how to copy prebuilt .dll files to build dir.

What was missing was FileTagger property since it seems QBS doesn't consider .dll files dynamic libraries.

FileTagger {
    patterns: ["*.dll"]
    fileTags: ["dynamicLibrary"]
}

The question still stands on how to add cpp.libraryPaths to system PATH on runtime. I found the following method in core.qbs:

setupRunEnvironment: {
    var env;
    if (qbs.targetOS.contains('windows')) {
        env = new ModUtils.EnvironmentVariable("PATH", qbs.pathListSeparator, true);
        env.append(binPath);
        env.set();
    }
    ...
}

I have no idea how to modify or invoke that method in my QBS files.

Upvotes: 2

Related Questions