Qt+OpenCV项目编译报错:LNK2019未解析外部符号及mainwindow.obj缺失
Let's work through your problems step by step—since your other Qt-OpenCV projects compile fine, the issue is almost certainly tied to this specific project's configuration or build artifacts. Here's how to fix each problem:
1. Fixing LNK2019 & LNK1120 (Unresolved Symbols)
These errors mean your linker can't locate the compiled OpenCV code for cv::medianBlur and cv::GaussianBlur. The most likely culprit is a missing or mismatched library configuration in this project's .pro file:
- Check your
.profile for Release-specific OpenCV links: OpenCV 3.2 uses distinct library names for Debug (ends withd) and Release builds. Make sure your.profile explicitly handles both modes, like this:win32 { # Update this path to your OpenCV 3.2 build directory OPENCV_ROOT = C:/opencv-3.2.0/build INCLUDEPATH += $$OPENCV_ROOT/include LIBS += -L$$OPENCV_ROOT/x64/vc14/lib # Match your Qt's compiler (vc14 = MSVC 2015, which Qt 5.7 uses) # Link correct libraries for Debug/Release CONFIG(debug, debug|release) { LIBS += opencv_world320d.lib } else { LIBS += opencv_world320.lib } }- If you're linking individual modules instead of
opencv_world, ensure you addopencv_imgproc320.lib(Release) oropencv_imgproc320d.lib(Debug)—medianBlurandGaussianBlurlive in the imgproc module.
- If you're linking individual modules instead of
- Verify architecture match: Ensure you're building for x64 (if your OpenCV is compiled for x64) or x86, and that your Qt Creator kit matches this architecture. Mismatched 32/64-bit builds will trigger unresolved symbols.
2. Fixing "Cannot Find mainwindow.obj"
This typically points to corrupted build artifacts or a misconfigured project file:
- Delete the entire build directory manually: Qt Creator's "Clean" sometimes leaves leftover files. Navigate to your project folder, delete the
build-Hello_Qt_OpenCV-*folder (tied to your kit name), then restart Qt Creator. - Confirm mainwindow.cpp is included in the project: In Qt Creator's project pane, check that
mainwindow.cpphas a checkmark next to it—if it's unchecked, it won't be compiled, so no.objfile is generated. - Re-run qmake properly: After deleting the build folder, open your project, go to
Build > Run qmake, then do a full rebuild (Build > Rebuild All).
3. Quick Code Sanity Check
Just to rule out code-level oversights:
- Ensure
mainwindow.cppincludes the necessary OpenCV headers: Add#include <opencv2/opencv.hpp>or at minimum#include <opencv2/imgproc/imgproc.hpp>to access the blur functions. - Double-check that you're using the
cv::namespace correctly (your error log confirms this, but it's worth eliminating typos).
If you've tried all this and still have issues, sharing your .pro file and full build log will help narrow down remaining edge cases.
内容的提问来源于stack exchange,提问作者jwm

