opencv c++ & code::blocks linking

I installed opencv 4.1.0 using the following modified installer it worked perfect (you only need to increase memory swap - I increased it to 8GB).
install_opencv4.1.0_Nano.sh (here is the following code for your use by the end of this post - without swap modification).

I made some modification in the original file, one of them is that i skipped installing python 2 binding if someone need it change the flag -D BUILD_opencv_python2=OFF to ON).

After installation, when running opencv c++ code using cmake and CMakeLists.txt (as recommended by opencv) everything works perfectly (i tested opencv version and get 4.1.0), I’ve tested cuda acceleration with opencv and it works, perfectly. i tested python 3 as well and everything works good.

I downloaded and install latest code::blocks IDE, and tested the hello world with native GCC compiler (default).

when trying to link opencv and use this IDE nothing works, no auto-complete, build errors etc.
i set the following:

  1. compiler search directories: /usr/local/include/opencv4/
  2. linker tab - usr/local/bin/
  3. tried list of libs.

I’ll appreciate if someone will try the installer below, and provide instructions (for dummies), how to link latest code:blocks IDE with this opencv installation that can be build and run\debug using it, with autocomplete.

many thanks

#!/bin/bash
#
# Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
#
# NVIDIA Corporation and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA Corporation is strictly prohibited.
#

if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <Install Folder>"
    exit
fi
folder="$1"
user="nvidia"
passwd="nvidia"

echo "** Remove OpenCV3.3 first"
sudo sudo apt-get purge *libopencv*

echo "** Install requirement"
sudo apt-get update
sudo apt-get install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y python2.7-dev python3.6-dev python-dev python-numpy python3-numpy
sudo apt-get install -y libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install -y libv4l-dev v4l-utils qv4l2 v4l2ucp
sudo apt-get install -y curl
sudo apt-get update

echo "** Download opencv-4.1.0"
cd $folder
curl -L https://github.com/opencv/opencv/archive/4.1.0.zip -o opencv-4.1.0.zip
curl -L https://github.com/opencv/opencv_contrib/archive/4.1.0.zip -o opencv_contrib-4.1.0.zip
unzip opencv-4.1.0.zip 
unzip opencv_contrib-4.1.0.zip 
cd opencv-4.1.0/

echo "** Building..."
mkdir release
cd release/
cmake -D WITH_CUDA=ON -D CUDA_ARCH_BIN="5.3" -D CUDA_ARCH_PTX="" -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.0/modules -D OPENCV_ENABLE_NONFREE=OFF -D WITH_GSTREAMER=ON -D WITH_LIBV4L=ON -D BUILD_opencv_python2=OFF -D BUILD_opencv_python3=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j3
sudo make install
s
echo "** Install opencv-4.1.0 successfully"
echo "** Bye :)"

It would be better if you could share what error does your IDE return.

in previous versions at search libraries (compiler) was /usr/local/include and now? i tried /usr/local/include/opencv4/ and it pass the build if i dont call for any other cv element only

#include <opencv2/opencv.hpp>

in previous versions at search libraries (linker) was /usr/local/bin and now?
in previous versions at linker additional libs was a list of opencv__ and now?

whatever i tried i get linking errors. i.e. most probably i do not link the correct libraries.

please advise.

Hi avihu.gamliel,

If the default library path of openCV changes, I think you should ask this question on opencv forum.

I’ve tried there and here as well, i think it will contribute to this (nvidia developers) community.

It might be easier if one of NVIDIA experts will test and try it himself and return with answers for us. I guess that when (and if) I’ll get the answers i’ll build a complete tutorial for “dummies” (like me).

meanwhile, can you test it. it is only few minutes testing it.

Looking at your opencv cmake options, you’ve set the opencv install dir into /usr/local (-D CMAKE_INSTALL_PREFIX=/usr/local), so you would set path to libs as:

/usr/local/lib

(The first ‘/’ is important. /usr/local/bin is a directory for executables, not for libs).
As mentioned by @WayneWWW, detail the actions you’re trying and the error messages for better advice.

Thanks Honey_Patouceul, modified it and get the following error:

-------------- Build: Release in test1 (compiler: GNU GCC Compiler)---------------

g++ -L/usr/local/lib/ -o bin/Release/test1 obj/Release/main.o  -s -Iopencv_cudev -Iopencv_core -Iopencv_cudaarithm -Iopencv_flann -Iopencv_imgproc -Iopencv_ml -Iopencv_phase_unwrapping -Iopencv_plot -Iopencv_quality -Iopencv_reg -Iopencv_surface_matching -Iopencv_cudafilters -Iopencv_cudaimgproc -Iopencv_cudawarping -Iopencv_dnn -Iopencv_freetype -Iopencv_fuzzy -Iopencv_gapi -Iopencv_hfs -Iopencv_img_hash -Iopencv_imgcodecs -Iopencv_photo -Iopencv_videoio -Iopencv_xphoto -Iopencv_cudacodec -Iopencv_highgui -Iopencv_bioinspired -Iopencv_dnn_objdetect -Iopencv_features2d -Iopencv_line_descriptor -Iopencv_saliency -Iopencv_text -Iopencv_calib3d -Iopencv_ccalib -Iopencv_cudafeatures2d -Iopencv_cudastereo -Iopencv_datasets -Iopencv_objdetect -Iopencv_rgbd -Iopencv_shape -Iopencv_structured_light -Iopencv_video -Iopencv_xfeatures2d -Iopencv_ximgproc -Iopencv_xobjdetect -Iopencv_aruco -Iopencv_bgsegm -Iopencv_cudabgsegm -Iopencv_cudalegacy -Iopencv_cudaobjdetect -Iopencv_dpm -Iopencv_face -Iopencv_optflow -Iopencv_stitching -Iopencv_tracking -Iopencv_cudaoptflow -Iopencv_stereo -Iopencv_superres -Iopencv_videostab  
obj/Release/main.o: In function `cv::Mat::~Mat()':
main.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x84): undefined reference to `cv::fastFree(void*)'
main.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x94): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
3 error(s), 0 warning(s) (0 minute(s), 0 second(s))

For specifying the libraries to be used at link time you would use ‘-l’ (lower case ‘L’).
Note that -I (upper case ‘i’) is used at compile time for adding a headers include path.

I’ll make the long story short.

use the above mentioned installation bash file (it will work with any opencv4.x only modify properly the names)

extract the swap to minimal 8GB and run the bash file.

install code::blocks on your system

compiler search directories: /usr/local/include/opencv4/
search directories ->linker tab - /usr/local/lib
linker tab - list of libs as above with -l (lower case of L).

thanks Honey :-)