编译Darknet(YOLO目标检测)时遭遇OpenCV相关编译致命错误
Hey there, let's work through this Darknet compilation issue with your OpenCV setup. First, let's look at the error you ran into:
gcc -Iinclude/ -Isrc/ -DOPENCV
pkg-config --cflags opencv-DGPU -I/usr/local/cuda-9.1/include/ -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -DGPU -c ./src/gemm.c -o obj/gemm.o
In file included from /usr/local/include/opencv2/core/types_c.h:59:0, from /usr/local/include/opencv2/core/core_c.h:48, from /usr...
This points to a header file resolution issue between Darknet and your OpenCV 3.4.0 installation. Here's how to fix it, plus key notes for OpenCV 4.x+ users:
For OpenCV 3.4.0 Users
Verify pkg-config is targeting the right OpenCV version
Sometimes system-wide pkg-config might be pointing to an older OpenCV install by mistake. Run this to check:pkg-config --modversion opencvIf it doesn't return 3.4.0, force it to use your compiled version by setting the pkg-config path:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig(Adjust the path if you installed OpenCV to a custom directory.)
Tweak the Darknet Makefile
Open theMakefilein your Darknet root folder and make these changes:- Ensure
OPENCV=1is uncommented (it looks like you have-DOPENCVenabled, but double-check the Makefile's top-level config). - Replace the
pkg-config --cflags opencvcall with eitherpkg-config --cflags opencv3(some systems name OpenCV 3's config this way) or manually specify the header paths:CPPFLAGS+= -I/usr/local/include/opencv2 -I/usr/local/include - Update the linker flags to explicitly link OpenCV libraries:
LDFLAGS+= -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs
- Ensure
Clean and recompile
Wipe old build artifacts first to avoid conflicts:make cleanThen start the compile again:
make
Critical Notes for OpenCV 4.x+ Users
If you ever upgrade to OpenCV 4 or later, C++11 support is mandatory—skip this and you'll hit fatal compilation errors. Here's how to add it:
- Edit the Darknet Makefile to add
-std=c++11(or higher, like-std=c++14) to your compiler flags:CFLAGS+= -std=c++11 - Also, note that OpenCV 4's pkg-config config is usually named
opencv4, so update the Makefile'spkg-configcalls fromopencvtoopencv4to pull the correct headers and libraries.
内容的提问来源于stack exchange,提问作者FlavienRJ

