13

I just compiled and installed the last version of OpenCV 3.4.0 and I would like to compile darknet (for yolo object detection), but at compilation, I have

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/local/include/opencv2/highgui/highgui_c.h:45,
                 from include/darknet.h:25,
                 from ./src/utils.h:5,
                 from ./src/gemm.c:2:
/usr/local/include/opencv2/core/cvdef.h:421:4: error: #error "OpenCV 4.x+ requires enabled C++11 support"
 #  error "OpenCV 4.x+ requires enabled C++11 support"
    ^
compilation terminated due to -Wfatal-errors.
Makefile:86: recipe for target 'obj/gemm.o' failed
make: *** [obj/gemm.o] Error 1

How it is possible, because in Makefile (below), I compile C

GPU=1
CUDNN=0
OPENCV=1
OPENMP=0
DEBUG=0

ARCH= -gencode arch=compute_30,code=sm_30 \
      -gencode arch=compute_35,code=sm_35 \
      -gencode arch=compute_50,code=[sm_50,compute_50] \
      -gencode arch=compute_52,code=[sm_52,compute_52]
#      -gencode arch=compute_20,code=[sm_20,sm_21] \ This one is deprecated?

# This is what I use, uncomment if you know your arch and want to specify
# ARCH= -gencode arch=compute_52,code=compute_52

VPATH=./src/:./examples
SLIB=libdarknet.so
ALIB=libdarknet.a
EXEC=darknet
OBJDIR=./obj/

CC=gcc
NVCC=nvcc 
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
AR=ar
ARFLAGS=rcs
OPTS=-Ofast
LDFLAGS= -lm -pthread 
COMMON= -Iinclude/ -Isrc/
CFLAGS=-Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC

ifeq ($(OPENMP), 1) 
CFLAGS+= -fopenmp
endif

ifeq ($(DEBUG), 1) 
OPTS=-O0 -g
endif

CFLAGS+=$(OPTS)

ifeq ($(OPENCV), 1) 
COMMON+= -DOPENCV
CFLAGS+= -DOPENCV 
LDFLAGS+= `pkg-config --libs opencv` 
COMMON+= `pkg-config --cflags opencv` 
endif

ifeq ($(GPU), 1) 
COMMON+= -DGPU -I/usr/local/cuda-9.1/include/
CFLAGS+= -DGPU 
LDFLAGS+= -L/usr/local/cuda-9.1/lib64 -lcudart -lcublas -lcurand
endif

ifeq ($(CUDNN), 1) 
COMMON+= -DCUDNN 
CFLAGS+= -DCUDNN 
LDFLAGS+= -lcudnn
endif

OBJ=gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o upsample_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o logistic_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o  lstm_layer.o l2norm_layer.o yolo_layer.o
EXECOBJA=captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o darknet.o
ifeq ($(GPU), 1) 
LDFLAGS+= -lstdc++ 
OBJ+=convolutional_kernels.o deconvolutional_kernels.o activation_kernels.o im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o avgpool_layer_kernels.o
endif

EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA))
OBJS = $(addprefix $(OBJDIR), $(OBJ))
DEPS = $(wildcard src/*.h) Makefile include/darknet.h

#all: obj backup results $(SLIB) $(ALIB) $(EXEC)
all: obj  results $(SLIB) $(ALIB) $(EXEC)


$(EXEC): $(EXECOBJ) $(ALIB)
    $(CC) $(COMMON) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(ALIB)

$(ALIB): $(OBJS)
    $(AR) $(ARFLAGS) $@ $^

$(SLIB): $(OBJS)
    $(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS)

$(OBJDIR)%.o: %.c $(DEPS)
    $(CC) $(COMMON) $(CFLAGS) -c $< -o $@

$(OBJDIR)%.o: %.cu $(DEPS)
    $(NVCC) $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -o $@

obj:
    mkdir -p obj
backup:
    mkdir -p backup
results:
    mkdir -p results

.PHONY: clean

clean:
    rm -rf $(OBJS) $(SLIB) $(ALIB) $(EXEC) $(EXECOBJ) $(OBJDIR)

And what's the solution for this error ? How I can enable C++11 support ?

Thank for your help !

FlavienRJ
  • 191
  • 1
  • 1
  • 7
  • The error is pretty clear. That version of OpenCV requires C++11 in its headers. Enable C++11 by reading the documentation of your C++ compiler and learning which option to pass it. – underscore_d Apr 20 '18 at 13:56
  • [Might be relevant](https://github.com/opencv/opencv/issues/11291). – Norrius Apr 20 '18 at 13:57
  • I try to add CFLAGS = -std=c11 and CFLAGS = -std=c++11 but the same error, I do not understand where to specify the version by default. I use standards gcc and g++ (version 5.4.0) – FlavienRJ Apr 20 '18 at 14:38
  • "last version of OpenCV 3.4.0" ... where from? That `#error` directive is not present in tag 3.4.0, neither in 3.4.1, only in master. Unless you want to do some development on the OpenCV code itself, it's probably not a good idea to use that. – Dan Mašek Apr 20 '18 at 15:50
  • I just ran into the same issue. Rebuilt OpenCV because of https://github.com/opencv/opencv/pull/11322 for YoloV3 support, now getting this error trying to build darknet. If you figure it out please let me know. Build this version of opencv: https://github.com/opencv/opencv/tree/3.4 – Austin Apr 20 '18 at 17:31
  • maybe have to cmake opencv with -DCMAKE_CXX_STANDARD set, but I'm not sure how – Austin Apr 20 '18 at 17:47
  • FlavienRJ did you manage to fix this error? I am still stuck and not sure where to add the changes in the make file – Bizmate Aug 11 '20 at 04:04

6 Answers6

13

Well, There is no need to Un-install and Re-Install OpenCV Just add -std=c++11 in your compile line . Say your File is main, and you want the object file to be output .

g++ -std=c++11 main.cpp -o output `pkg-config --cflags --libs opencv`

And to view the output , type ->

./output
KnightRider
  • 161
  • 5
6

No, you don't set that c++11 flag, the cmake script is checking that, and that error is coming from here. That means most likely you're using an old gcc version. Test that with this g++ -dM -E -x c++ -std=c++11 /dev/null | grep plus and you should see #define __cplusplus 201103L, if not then need to install a newer version of gcc.

fireant
  • 14,080
  • 4
  • 39
  • 48
  • When I test this command I have this result (g++ -dM -E -x c++ -std=c++11 /dev/null | grep plus #define __cplusplus 201103L ) , then why it doesn't use c++11 ? – FlavienRJ Apr 23 '18 at 06:54
6

I've resolved the problem.

Uninstall opencv completely.

Download OpenCV Release 3.4.0 https://github.com/opencv/opencv/releases/tag/3.4.0 and not 3.4.1

With Cmake-gui (or not), ENABLE_CXX11 = 1

Recompile make -j4, install make install, sudo ldconfig and finally it works !

Conclusion : Never use the last version of anything

FlavienRJ
  • 191
  • 1
  • 1
  • 7
  • where did you clone the OpenCV code in the darknet folder so that Yolo could find and use it during the make and make install – Bizmate Aug 11 '20 at 04:07
4

add this line to your CMake file:

set (CMAKE_CXX_STANDARD 11)
user3041840
  • 352
  • 4
  • 8
1

I added -std=c++11 to the g++ line in YOLO like: g++ -std=c++11 -Iinclude/ -Isrc/ -DOPENCVpkg-config --cflags opencv4-Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -c ./src/image_opencv.cpp -o obj/image_opencv.o

and it resolves that error.

Adam Leary
  • 101
  • 1
  • 4
0

The same case here, I tried compiling OpenCv 4.0.0 and faced countless errors and warnings. Switched to 3.0 for no reason but everything worked perfectly. Just tried Version 3.4.0 and everything worked with -D ENABLE_CXX=1 Flag.

cd to_working_dir

mingw32-make -j 8 -D ENABLE_CXX=1 # for 3.4 or above.

You don't need any flag for 3.0. It just works with

mingw32-make

**Note: This is only tested on Windows 10 with mingw. **

Mujeeb Ishaque
  • 2,259
  • 24
  • 16