I'm using opencv in my qt project, and I want to link it. But th qbs for my project does not find my opencv module. Here is my file structure:
MyProject/
├── build/
├── opencv/
│ ├── debug/
│ │ └── include/, lib/
│ ├── release/
│ │ └── include/, lib/
│ └── opencv.qbs
├── src/
│ └── main.cpp
└── MyProject.qbs <-- Main project file
Here is my opencv.qbs file :
import qbs.FileInfo
Module {
name: "opencv"
Export {
Depends { name: "cpp" }
cpp.includePaths: qbs.buildVariant == "release" ? "release/include" : "debug/include"
cpp.libraryPaths: qbs.buildVariant == "release" ? "release/lib" : "debug/lib"
cpp.dynamicLibraries: qbs.buildVariant == "release" ? [...files...] : [...filesd...]
}
}
And here is MyProject.qbs:
import qbs.FileInfo
QtApplication {
Depends { name: "Qt.widgets" }
Depends { name: "opencv" }
cpp.defines: [
// You can make your code fail to compile if it uses deprecated APIs.
// In order to do so, uncomment the following line.
//"QT_DISABLE_DEPRECATED_BEFORE=0x060000" // disables all the APIs deprecated before Qt 6.0.0
]
files: [
"src/**",
]
references: [ "opencv/opencv.qbs" ]
install: true
installDir: qbs.targetOS.contains("qnx") ? FileInfo.joinPaths("/tmp", name, "bin") : base
}
the line Depends { name: "opencv" } is flagged Dependency 'opencv' not found for product 'deconv'.
2 Answers 2
The two problems here are:
You do not pull in modules via "references" like products; instead, they are looked up via the module search paths; see https://doc.qt.io/qbs/custom-modules.html.
Modules cannot contain Export items. All the property bindings in a module are "exported" automatically to the depending product.
You don't set a module's name explicitly; it is derived from the location (see above).
In fact, referencing a module item this way triggers an error:
ERROR: Item type should be 'Product' or 'Project', but is 'Module'.
So I suspect your actual project doesn't quite look like what you pasted here.
Note that alternatively, you can use your layout as is by changing the Module to a Product (the Export item also creates a module).
2 Comments
As it turns out, and it's not made clearly in the documentation, if you want to link or add an external library, you should pack the importing application into a Project and reference your library from that Project and not from the application:
Project {
references: [ "opencv/opencv.qbs" ] // OK
QtApplication {
Depends { name: "opencv" }
// references: [ "opencv/opencv.qbs" ] // WRONG
}
}
If you want to import external resources, these should be placed in a Product with the exported include/binaries/lib files:
Product {
name: "opencv"
Export {
Depends { name: "cpp" }
cpp.includePaths: ...
cpp.libraryPaths: ...
cpp.dynamicLibraries: ...
}
}
Or from my understanding, use a Module only if it is globally shared (e.g needs to be in your %appdata% folder in Windows) or locally, see Christian Kandeler's answer for more information.