Ubuntu16.04下Qt环境编译带OpenCV支持的Darknet报错求助
Hey, I’ve dealt with this exact OpenCV linking problem with Darknet on Ubuntu 16.04 before—since you’re hitting the same issue across Darknet, standalone OpenCV test projects, and Qt, it’s almost certainly a misconfigured library path, missing pkg-config setup, or version compatibility mismatch. Let’s work through solutions step by step:
1. Verify Your OpenCV Installation First
First, confirm how you installed OpenCV and where it lives—this is critical for fixing links:
- If you used
sudo apt-get install libopencv-dev, libraries are in/usr/lib/x86_64-linux-gnu/and headers in/usr/include/opencv/(Ubuntu 16.04’s repo has OpenCV 2.x/3.x). - If you compiled OpenCV from source, default paths are
/usr/local/lib/and/usr/local/include/opencv4/(adjust for your specific version).
Run these commands to check version and path details:
# Check OpenCV version pkg-config --modversion opencv # Check compiler flags for OpenCV pkg-config --cflags opencv # Check linker flags for OpenCV pkg-config --libs opencv
If pkg-config throws an error, your OpenCV isn’t registered with pkg-config—this is a common root cause.
2. Fix Darknet’s Makefile Configuration
You already set OPENCV=1, but let’s tweak the OpenCV-related lines in Darknet’s Makefile to ensure it’s pointing to the right paths:
Option A: Fix pkg-config Path (for source-compiled OpenCV)
If you built OpenCV from source, its opencv.pc file is probably in /usr/local/lib/pkgconfig/, which isn’t in the default pkg-config search path. Update the Makefile lines to explicitly reference this path:
ifeq ($(OPENCV), 1) COMMON+= -DOPENCV CFLAGS+= -DOPENCV # Explicitly set pkg-config path for source-built OpenCV LDFLAGS+= PKG_CONFIG_PATH=/usr/local/lib/pkgconfig pkg-config --libs opencv -lstdc++ COMMON+= PKG_CONFIG_PATH=/usr/local/lib/pkgconfig pkg-config --cflags opencv endif
Option B: Hardcode Paths (if pkg-config fails)
If pkg-config still doesn’t work, manually specify the header and library paths. Replace with your actual OpenCV paths:
ifeq ($(OPENCV), 1) COMMON+= -DOPENCV -I/usr/local/include/opencv4 CFLAGS+= -DOPENCV # List all required OpenCV libraries (adjust for your version) LDFLAGS+= -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -lstdc++ endif
Use ls /usr/local/lib | grep opencv to check exact library names (e.g., OpenCV 4 uses opencv_core4 instead of opencv_core in some cases).
3. Update System-Wide Library Cache
Sometimes the system doesn’t detect new OpenCV libraries. Run these commands to refresh the dynamic linker cache:
# Add OpenCV library path to system config (if source-compiled) echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/opencv.conf # Refresh cache sudo ldconfig
4. Test OpenCV Standalone to Isolate the Issue
Let’s rule out Darknet-specific issues with a simple OpenCV test program. Create test_opencv.cpp:
#include <opencv2/opencv.hpp> using namespace cv; int main() { Mat img = imread("test.jpg"); // Use any image you have imshow("OpenCV Test", img); waitKey(0); return 0; }
Compile it with explicit OpenCV flags:
g++ test_opencv.cpp -o test_opencv `pkg-config --cflags --libs opencv`
If this fails, the error message will tell you exactly which library or path is missing—use that to fix your system setup before going back to Darknet.
5. Fix Qt Project Configuration
For Qt projects, update your .pro file to correctly link OpenCV:
Using pkg-config (simplest if it works):
CONFIG += link_pkgconfig PKGCONFIG += opencv
Manual Paths (if pkg-config fails):
INCLUDEPATH += /usr/local/include/opencv4 LIBS += -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs
Again, adjust paths and library names to match your OpenCV version.
One last note: Ubuntu 16.04 uses GCC 5.x by default. If you compiled OpenCV with a newer GCC version, ensure Darknet is compiled with the same compiler to avoid compatibility issues—you can specify the compiler in Darknet’s Makefile by setting CC=gcc-7 (or your desired version) at the top.
内容的提问来源于stack exchange,提问作者raggot

