Compiling Opencv Source on Host Platform

I am Compiling an test Opencv Program for my Colibri T20 System an I am stuck at the following.

Makefile
    SYSROOTS ?= /usr/local/oecore-x86_64/sysroots
    CC = ${SYSROOTS}/x86_64-angstromsdk-linux/usr/bin/arm-angstrom-linux-gnueabi/arm-angstro$
    INCLUDES = -I${SYSROOTS}/armv7at2hf-vfp-neon-angstrom-linux-gnueabi/usr/include
    LIB_PATH = -L${SYSROOTS}/armv7at2hf-vfp-neon-angstrom-linux-gnueabi/lib
    LIBS = -lpthread -lopencv_highgui -lopencv_core -lopencv_imgproc -lopencv_objdetect -lm
    CFLAGS = -O2 -g -Wall -mfloat-abi=hard --sysroot=${SYSROOTS}/armv7at2hf-vfp-neon-angstro$
    
    all:${CC} ${CFLAGS} ${INCLUDES} ${LIB_PATH} ${LIBS} test1.cpp -o test1
    
    clean:rm -rf test1

Source file-

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}    

Error I am getting are

abc@ubuntu:~/Desktop$ nano makefile
abc@ubuntu:~/Desktop$ make
make: *** No rule to make target '-O2', needed by 'all'.  Stop.
abc@ubuntu:~/Desktop$

Hi

Your Makefile has a bug.

all:${CC} ${CFLAGS} ${INCLUDES} ${LIB_PATH} ${LIBS} test1.cpp -o test1
clean:rm -rf test1

should be changed to something like:

all: test1

test1: test1.cpp
<TAB>${CC} ${CFLAGS} ${INCLUDES} ${LIB_PATH} ${LIBS} test1.cpp -o test1

clean:
<TAB>rm -rf test1

.PHONY : all clean

Consult e.g. this page about Makefiles.

Max

I have changed the Makefile and I think its is compiling it now.But there are various errors that came up,

   abc@ubuntu:~/Desktop$ make
    g++     test1.cpp   -o test1
    /tmp/cci8pxyb.o: In function `main':
    test1.cpp:(.text+0xa9): undefined reference to `cv::imread(cv::String const&, int)'
    test1.cpp:(.text+0x142): undefined reference to `cv::namedWindow(cv::String const&, int)'
    test1.cpp:(.text+0x199): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
    test1.cpp:(.text+0x1c1): undefined reference to `cv::waitKey(int)'
    /tmp/cci8pxyb.o: In function `cv::String::String(char const*)':
    test1.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to `cv::String::allocate(unsigned long)'
    /tmp/cci8pxyb.o: In function `cv::String::~String()':
    test1.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
    /tmp/cci8pxyb.o: In function `cv::Mat::~Mat()':
    test1.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
    /tmp/cci8pxyb.o: In function `cv::Mat::release()':
    test1.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()'
    /tmp/cci8pxyb.o: In function `cv::Mat::operator=(cv::Mat&&)':
    test1.cpp:(.text._ZN2cv3MataSEOS0_[_ZN2cv3MataSEOS0_]+0xd4): undefined reference to `cv::fastFree(void*)'
    collect2: error: ld returned 1 exit status
    <builtin>: recipe for targ

Which version of OpenCV you are using? Since OpenCV 3, the imread function is moved to opencv_imcodecs so you should add -lopencv_imcodecs to libs.

Hey @isaac thank for the reply,As specified by you I have added the -lopencv_imcodecs to the makefile
and recompiled the code.
and this is the result that i am getting.

abc@ubuntu:~/Desktop$ make
g++ test1.cpp -o test1
/tmp/ccPnIH4h.o: In function main': test1.cpp:(.text+0xa9): undefined reference to cv::imread(cv::String const&, int)’
test1.cpp:(.text+0x142): undefined reference to cv::namedWindow(cv::String const&, int)' test1.cpp:(.text+0x199): undefined reference to cv::imshow(cv::String const&, cv::_InputArray const&)’
test1.cpp:(.text+0x1c1): undefined reference to cv::waitKey(int)' /tmp/ccPnIH4h.o: In function cv::String::String(char const*)‘:
test1.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to cv::String::allocate(unsigned long)' /tmp/ccPnIH4h.o: In function cv::String::~String()’:
test1.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to cv::String::deallocate()' /tmp/ccPnIH4h.o: In function cv::Mat::~Mat()‘:
test1.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to cv::fastFree(void*)' /tmp/ccPnIH4h.o: In function cv::Mat::release()’:
test1.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to cv::Mat::deallocate()' /tmp/ccPnIH4h.o: In function cv::Mat::operator=(cv::Mat&&)‘:
test1.cpp:(.text.ZN2cv3MataSEOS0[ZN2cv3MataSEOS0]+0xd4): undefined reference to cv::fastFree(void*)' collect2: error: ld returned 1 exit status : recipe for target 'test1' failed make: *** [test1] Error 1 abc@ubuntu:~/Desktop$ make g++ test1.cpp -o test1 /tmp/ccPnIH4h.o: In function main’:
test1.cpp:(.text+0xa9): undefined reference to cv::imread(cv::String const&, int)' test1.cpp:(.text+0x142): undefined reference to cv::namedWindow(cv::String const&, int)’
test1.cpp:(.text+0x199): undefined reference to cv::imshow(cv::String const&, cv::_InputArray const&)' test1.cpp:(.text+0x1c1): undefined reference to cv::waitKey(int)’
/tmp/ccPnIH4h.o: In function cv::String::String(char const*)': test1.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to cv::String::allocate(unsigned long)’
/tmp/ccPnIH4h.o: In function cv::String::~String()': test1.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to cv::String::deallocate()’
/tmp/ccPnIH4h.o: In function cv::Mat::~Mat()': test1.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to cv::fastFree(void*)’
/tmp/ccPnIH4h.o: In function cv::Mat::release()': test1.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to cv::Mat::deallocate()’
/tmp/ccPnIH4h.o: In function cv::Mat::operator=(cv::Mat&&)': test1.cpp:(.text._ZN2cv3MataSEOS0_[_ZN2cv3MataSEOS0_]+0xd4): undefined reference to cv::fastFree(void*)’
collect2: error: ld returned 1 exit status
: recipe for target ‘test1’ failed
make: *** [test1] Error 1

Why does ${CC} get expanded to g++ when it should be something like

CC = ${SYSROOTS}/x86_64-angstromsdk-linux/usr/bin/arm-angstrom-linux-gnueabi/arm-angstro$

(I assume your copy paste operation does not show the end of the line, but there should be some kind of long path and not just g++ which will call the native x86 compiler)

Additionally there should be the sideeffects of ‘${CFLAGS} ${INCLUDES} ${LIB_PATH} ${LIBS}’…

Apparently that didn’t make any difference.

Are you sure that you have configured the environment variable to for cross-compilation by running source /usr/local/oecore-x86_64/environment-setup-... script? Have you built the SDK you are suing by bitbake <your-image-name> -c populate_sdk after adding opencv package to the configuration?

Well apparently my comment got deleted on your other duplicate post.

Are you sure you have run source /usr/local/oecore-x86_64/environment-setup-arm... script before running make?

Have you included opencv package in bitbake configuration before building the SDK by bitbake <your-image-name> -c populate_sdk ?

@Issac @max.tx You are correct I was using the different terminal for make and run, the environment script on another ,now it shows these errors

Now I changed the Terminal and reexecuted the Make

   abc@ubuntu:~/Desktop$ make
    arm-angstrom-linux-gnueabi-g++  -mfpu=vfpv3-d16 -march=armv7-a -mthumb -mfpu=vfp -mfloat-abi=hard --sysroot=/usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi  -O2 -pipe -g -feliminate-unused-debug-types   -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed  test1.cpp   -o test1
    /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/cvstd.hpp:601: error: undefined reference to 'cv::String::allocate(unsigned int)'
    /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/mat.inl.hpp:692: error: undefined reference to 'cv::fastFree(void*)'
    /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/mat.inl.hpp:804: error: undefined reference to 'cv::Mat::deallocate()'
    test1.cpp:17: error: undefined reference to 'cv::imread(cv::String const&, int)'
    /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/mat.inl.hpp:1336: error: undefined reference to 'cv::fastFree(void*)'
    /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/cvstd.hpp:647: error: undefined reference to 'cv::String::deallocate()'
    test1.cpp:25: error: undefined reference to 'cv::namedWindow(cv::String const&, int)'
    /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/cvstd.hpp:647: error: undefined reference to 'cv::String::deallocate()'
    test1.cpp:26: error: undefined reference to 'cv::imshow(cv::String const&, cv::_InputArray const&)'
    /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/cvstd.hpp:647: error: undefined reference to 'cv::String::deallocate()'
    test1.cpp:28: error: undefined reference to 'cv::waitKey(int)'
    /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/mat.inl.hpp:804: error: undefined reference to 'cv::Mat::deallocate()'
    /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/cvstd.hpp:647: error: undefined reference to 'cv::String::deallocate()'
    collect2: error: ld returned 1 exit status
    <builtin>: recipe for target 'test1' failed
    make: *** [test1] Error 1

Now errors look more promising and good

Linker still cannot find the OpenCV binaries. I suspect if the OpenCV libs are present in the SDK. Would you mind to run this command and post the output?

find /usr/local/oecore-x86_64/sysroots/ | grep -i opencv

Also, you can use CMake instead of Makefile since as it makes writing build files and resolving dependencies much simpler. A CMakeLists.txt for your code would be:

cmake_minimum_required(VERSION 3.1)
project(myApp)

add_executable(myApp test1.cpp)

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(myApp ${OpenCV_LIBS})

# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

@isaac As requested I have attached the following output.

abc@ubuntu:~$ sudo find /usr/local/oecore-x86_64/sysroots/ | grep -i opencv

The output is truncated. Write the output to a file by appending > log.txt to the command and attach the generated file in your response, (or use services like pastebin.com).

BTW, did you try the CMake solution?

link text

Here is the Log File .

abc@ubuntu:~/Desktop$ nano CMakeLists.txt
abc@ubuntu:~/Desktop$ cmake
-- The C compiler identification is GNU 7.2.0
-- The CXX compiler identification is GNU 7.2.0
-- Check for working C compiler: /usr/local/oecore-x86_64/sysroots/x86_64-angstromsdk-linux/usr/bin/arm-angstrom-linux-gnueabi/arm-angstrom-linux-gnueabi-gcc
-- Check for working C compiler: /usr/local/oecore-x86_64/sysroots/x86_64-angstromsdk-linux/usr/bin/arm-angstrom-linux-gnueabi/arm-angstrom-linux-gnueabi-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/local/oecore-x86_64/sysroots/x86_64-angstromsdk-linux/usr/bin/arm-angstrom-linux-gnueabi/arm-angstrom-linux-gnueabi-g++
-- Check for working CXX compiler: /usr/local/oecore-x86_64/sysroots/x86_64-angstromsdk-linux/usr/bin/arm-angstrom-linux-gnueabi/arm-angstrom-linux-gnueabi-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr (found version "3.3.0") 
-- OpenCV library status:
--     version: 3.3.0
--     libraries: opencv_calib3d;opencv_core;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_dpm;opencv_face;opencv_fuzzy;opencv_img_hash;opencv_line_descriptor;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_reg;opencv_rgbd;opencv_saliency;opencv_stereo;opencv_structured_light;opencv_surface_matching;opencv_tracking;opencv_xfeatures2d;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto
--     include path: /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include;/usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv
-- Configuring done
-- Generating done
-- Build files have been written to: /home/abc/Desktop

Based on the output you posted, it seems you have the necessary libraries.

After running cmake, did you run make to build the project? It should do the job and build the binary unless something really weird is happening in your SDK.

As a side-tip, it is a good practice to build out of source tree to avoid polluting your source code with generated files for build.

For this purpose, create a directory like build and run cmake inside it. For instance, when you are in your source directory run the following:

mkdir build
cd build
cmake ..
make -j4

You can later clean up things easily by just wiping the build directory.
More on this: Looking for a 'cmake clean' command to clear up CMake output - Stack Overflow

abc@ubuntu:~/Desktop/sources/prog1$ make
Scanning dependencies of target myApp
[ 50%] Building CXX object CMakeFiles/myApp.dir/prog1.cpp.o
In file included from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/c++/7.2.0/ext/string_conversions.h:41:0,
                 from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/c++/7.2.0/bits/basic_string.h:6159,
                 from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/c++/7.2.0/string:52,
                 from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/c++/7.2.0/stdexcept:39,
                 from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/c++/7.2.0/array:39,
                 from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/cvdef.h:424,
                 from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core.hpp:52,
                 from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/opencv.hpp:52,
                 from /home/abc/Desktop/sources/prog1/prog1.cpp:2:
/usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/c++/7.2.0/cstdlib:75:15: fatal error: stdlib.h: No such file or directory
 #include_next 
               ^~~~~~~~~~
compilation terminated.
CMakeFiles/myApp.dir/build.make:62: recipe for target 'CMakeFiles/myApp.dir/prog1.cpp.o' failed
make[2]: *** [CMakeFiles/myApp.dir/prog1.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/myApp.dir/all' failed
make[1]: *** [CMakeFiles/myApp.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

These are the errors that I am getting from Using Cmake ,I did find that in provious makefile I left the part " test1: test1.cpp" !

As Specified by you I have created the CMakeLists.txt file and copied your code in that. And Ran Cmake and Make after that.
The above Error are Generated as shown.

abc@ubuntu:/usr/local/oecore-x86_64$ ls
environment-setup-armv7at2hf-vfp-angstrom-linux-gnueabi
site-config-armv7at2hf-vfp-angstrom-linux-gnueabi
sysroots
version-armv7at2hf-vfp-angstrom-linux-gnueabi
abc@ubuntu:/usr/local/oecore-x86_64$ . environment-setup-armv7at2hf-vfp-angstrom-linux-gnueabi
abc@ubuntu:/usr/local/oecore-x86_64$ cd
abc@ubuntu:~$ cd Desktop
abc@ubuntu:~/Desktop$ cd sources
abc@ubuntu:~/Desktop/sources$ cmake -DENABLE_PRECOMPILED_HEADERS=OFF 
-- OpenCV library status:
--     version: 3.3.0
--     libraries: opencv_calib3d;opencv_core;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_dpm;opencv_face;opencv_fuzzy;opencv_img_hash;opencv_line_descriptor;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_reg;opencv_rgbd;opencv_saliency;opencv_stereo;opencv_structured_light;opencv_surface_matching;opencv_tracking;opencv_xfeatures2d;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto
--     include path: /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include;/usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv
-- Configuring done
-- Generating done
-- Build files have been written to: /home/abc/Desktop/sources
abc@ubuntu:~/Desktop/sources$ make
Scanning dependencies of target ImageProcessing
[ 50%] Building CXX object CMakeFiles/ImageProcessing.dir/one.cpp.o
In file included from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/c++/7.2.0/ext/string_conversions.h:41:0,
                 from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/c++/7.2.0/bits/basic_string.h:6159,
                 from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/c++/7.2.0/string:52,
                 from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/c++/7.2.0/stdexcept:39,
                 from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/c++/7.2.0/array:39,
                 from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/cvdef.h:424,
                 from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core.hpp:52,
                 from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/objdetect.hpp:47,
                 from /home/abc/Desktop/sources/one.cpp:1:
/usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/c++/7.2.0/cstdlib:75:15: fatal error: stdlib.h: No such file or directory
 #include_next <stdlib.h>
               ^~~~~~~~~~
compilation terminated.
CMakeFiles/ImageProcessing.dir/build.make:62: recipe for target 'CMakeFiles/ImageProcessing.dir/one.cpp.o' failed
make[2]: *** [CMakeFiles/ImageProcessing.dir/one.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/ImageProcessing.dir/all' failed
make[1]: *** [CMakeFiles/ImageProcessing.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
abc@ubuntu:~/Desktop/sources$ 

@max.tx @isaac I have Changed the Cmake According to the compilation error that I am receiving regarding the stdlib.h
Following is the errors that are received after that.
Read through here.

CMake is essentially a tool for generating build files (e.g. Makefile) automatically. It doesn’t do any magics. So you should be able to build the sources with a Makefile too. It is just easier to maintain and update a Cmake and it is more portable.
Did fixing the Makefile as you mentioned earlier made any difference?

There seems to be some issues with OpenCV in yocto. My colleagues also had some issues to get it working.

As for Cmake error:

  1. Make sure you have cleaned up the working directory by deleting any CMakeCache, CMakeFiles, cmake_install, etc. There is no clean command for cmake and you have to do it manually. After than, run cmake with -DENABLE_PRECOMPILED_HEADERS=OFF switch again.
  2. You probably should already have cmake support in your SDK, but just for being sure check this link: Yocto SDK with cmake toolchain file - Stack Overflow. You can verify whether this variable is set by running bitbake -e <your-image-name>.
  3. One last resort is to use Makefile and specify the libraries by absolute path (e.g. /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/lib/libopencv_highgui.so.3.3.0) and see how this works. This can could help to make sure the linker can find the libraries.

@isaac Thank for the detailed reply and helping me out,I Appreciate your help.
I have run the make command anf following errors persists.

   abc@ubuntu:~/Desktop/sources/prog1$ make
    /usr/local/oecore-x86_64/sysroots/x86_64-angstromsdk-linux/usr/bin/arm-angstrom-linux-gnueabi/arm-angstrom-linux-gnueabi-g++ -O2 -g -Wall -mfloat-abi=hard --sysroot=/usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-neon-angstrom-linux-gnueabi -I/usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-neon-angstrom-linux-gnueabi/usr/include -L/usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-neon-angstrom-linux-gnueabi/lib -lpthread -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_core -lopencv_imgproc -lopencv_objdetect -lm prog1.cpp -o prog1
    prog1.cpp:2:10: fatal error: opencv2/opencv.hpp: No such file or directory
     #include <opencv2/opencv.hpp>
              ^~~~~~~~~~~~~~~~~~~~
    compilation terminated.
    makefile:11: recipe for target 'prog1' failed
    make: *** [prog1] Error 1