Compiling Opencv Source on Host Platform

It is failing earlier than previous posts. Did you run environment-setup-armv7at2hf-vfp-angstrom-linux-gnueabi inside the terminal before running make?

Also I’v noticed the paths in your Makefile to the include and lib directories are not correct. Based on the log.txt you posted, the sysroot is at /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi while the Makefile paths are pointing elsewhere.

Yes I have run the environment variable script.I have manually check the directories that Makefile includes,And Its path are coming correctly.

The output of running make has armv7at2hf-vfp-neon-angstrom-linux-gnueabi in directory paths while in the log.txt you posted before the sysroot is located in armv7at2hf-vfp-angstrom-linux-gnueabi. Note extra neon in the Makefile paths.

hi askhar, did the compilation work?

@Isaac @jaski.tx You are correct,The path that Makefile pointing to is wrong,I have changed it to the correct past in which the sdk and toolchain is Installed,But after changing the path ,The same problem persist!

The error on #include <opencv2/opencv.hpp> must be resolved now. Y
ou have OpenCV header files in the SDK, so any issue related to headers indicates a mis-configuration in Makefile or C++ program.

Now to make sure we are on the same page, in a new terminal run /usr/local/oecore-x86_64/environment-setup-... script and run make on a clean source directory.

Post the Makefile and its output and I try to figure out why it is failing.

If rebuilding the SDK is possible, I highly recommend enabling CMake support. It can make finding/adding dependencies easier and much less error prone compared to a Makefile. It is also compatible with classic tools like pkgconfig.

Here are the instructions: Yocto SDK with cmake toolchain file - Stack Overflow

If you don’t have a customised image.bb file, you can simply add the following line to your local.conf (note the prepending and appending space):

TOOLCHAIN_HOST_TASK_append = " nativesdk-cmake "

I made up a new Directory and placed Face detect code (Toradex Sample code)
and Run the whole sequence now with Following output.
abc@ubuntu:~$ cd /usr/local/oecore-x86_64
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$ make
make: *** No targets specified and no makefile found. Stop.
abc@ubuntu:/usr/local/oecore-x86_64$ clear

abc@ubuntu:/usr/local/oecore-x86_64$ . environment-setup-armv7at2hf-vfp-angstrom-linux-gnueabi
abc@ubuntu:/usr/local/oecore-x86_64$ cd
abc@ubuntu:~$ mkdir new1 && cd new1
abc@ubuntu:~/new1$ nano new_prog1.cpp
abc@ubuntu:~/new1$ nano Makefile
abc@ubuntu:~/new1$ nano Makefile
abc@ubuntu:~/new1$ make
Makefile:14: *** missing separator.  Stop.
abc@ubuntu:~/new1$ nano Makefile
abc@ubuntu:~/new1$ 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-angstrom-linux-gnueabi/usr/include -L/usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/lib -lpthread -lopencv_highgui -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -lopencv_objdetect -lm new_prog1.cpp -o new_prog1
In file included from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/cvdef.h:91:0,
                 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 /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/objdetect/objdetect.hpp:48,
                 from new_prog1.cpp:1:
/usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/hal/interface.h:15:10: fatal error: cstddef: No such file or directory
 #include <cstddef>
          ^~~~~~~~~
compilation terminated.
Makefile:11: recipe for target 'new_prog1' failed
make: *** [new_prog1] Error 1

Code:-

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
 
#include <iostream>
#include "stdio.h"
 
using namespace std;
using namespace cv;
 
CascadeClassifier face_cascade;
string window_name = "Face Detection Demo";
String face_cascade_name = "/home/root/haarcascade_frontalface_alt2.xml";
const int BORDER = 8;  /* order between GUI elements to the edge of the image */
 
template <typename T> string toString(T t)
{
    ostringstream out;
    out << t;
 
    return out.str();
}
 
/*
 * Draw text into an image. Defaults to top-left-justified text,
 * but you can give negative x coords for right-justified text,
 * and/or negative y coords for bottom-justified text
 * Returns the bounding rect around the drawn text
*/
Rect drawString(Mat img, string text, Point coord, Scalar color,
        float fontScale = 0.6f, int thickness = 1, int fontFace = FONT_HERSHEY_COMPLEX)
{
    /* Get the text size & baseline */
    int baseline = 0;
    Size textSize = getTextSize(text, fontFace, fontScale, thickness, &baseline);
    baseline += thickness;
 
    /* Adjust the coords for left/right-justified or top/bottom-justified */
    if (coord.y >= 0) {
        /*
         * Coordinates are for the top-left corner of the text
         * from the top-left of the image, so move down by one row.
         */
        coord.y += textSize.height;
    } else {
        /*
         * Coordinates are for the bottom-left corner of the text
         * from the bottom-left of the image, so come up from the bottom
         */
            coord.y += img.rows - baseline + 1;
    }
 
    /* Become right-justified if desired */
    if (coord.x < 0) {
        coord.x += img.cols - textSize.width + 1;
    }
 
    /* Get the bounding box around the text */
    Rect boundingRect = Rect(coord.x, coord.y - textSize.height, textSize.width, baseline + textSize.height);
 
    /* Draw anti-aliased text */
    putText(img, text, coord, fontFace, fontScale, color, thickness, CV_AA);
 
    /* Let the user know how big their text is, in case they want to arrange things */
    return boundingRect;
}
 
int main(int argc, const char** argv)
{
    VideoCapture capture;
    Mat frame;
    std::vector<Rect> faces;
    Mat frame_gray;
 
 
    if (!face_cascade.load( face_cascade_name ) ) {
        printf("--(!)Error loading training file: haarcascade_frontalface_alt2.xml\n");
        return -1;
    };
 
    try {
        capture.open("v4l2:///dev/video3");
        capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
        capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
    }
    catch (cv::Exception &e)
    {
        const char *err_msg = e.what();
        cout << "Exception caught: " << err_msg << endl;
    }
 
    if ( !capture.isOpened() ) {
        cout << "ERROR: Could not access the camera!" << endl;
        exit(1);
    }
 
    while(true) {
        capture >> frame;
 
        if (!frame.empty()) {
 
            cvtColor(frame, frame_gray, CV_BGR2GRAY);
            equalizeHist(frame_gray, frame_gray);
 
            face_cascade.detectMultiScale(frame_gray, faces, 1.2, 3, CV_HAAR_DO_CANNY_PRUNING, Size(80, 80));
 
            for (size_t i = 0; i < faces.size(); i++) {
                CvPoint pt1 = { faces[i].x, faces[i].y };
                CvPoint pt2 = { faces[i].x + faces[i].width, faces[i].y + faces[i].height };
                rectangle(frame, pt1, pt2, CV_RGB(0, 255, 0), 3, 4, 0);
 
                Mat faceROI = frame_gray(faces[i]);
            }
            string stringToDisplay = "Number Of Faces: " + toString(faces.size());
 
            drawString(frame, stringToDisplay, Point(BORDER, -BORDER - 2 - 50), CV_RGB(0, 0, 0));
            drawString(frame, stringToDisplay, Point(BORDER + 1, -BORDER - 1 - 50), CV_RGB(0, 255, 0));
 
            imshow(window_name, frame);
        } else {
            printf(" --(!) No captured frame");
        }
 
        int c = waitKey(1);
        if ((char)c == 27) {
            break;
        }
    }
 
    return 0;
}

MakeFile-

SYSROOTS ?= /usr/local/oecore-x86_64/sysroots
CC = ${SYSROOTS}/x86_64-angstromsdk-linux/usr/bin/arm-angstrom-linux-gnueabi/arm-angstrom-linux-gnueabi-g++
INCLUDES = -I${SYSROOTS}/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include
LIB_PATH = -L${SYSROOTS}/armv7at2hf-vfp-angstrom-linux-gnueabi/lib
LIBS = -lpthread -lopencv_highgui -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -lopencv_objdetect -lm
CFLAGS = -O2 -g -Wall -mfloat-abi=hard --sysroot=${SYSROOTS}/armv7at2hf-vfp-neon-angstrom-linux-gnueabi

all: new_prog1

new_prog1: new_prog1.cpp
        ${CC} ${CFLAGS} ${INCLUDES} ${LIB_PATH} ${LIBS} new_prog1.cpp -o new_prog1

clean:
        rm -rf new_prog1

.PHONY : all clean

Errors-

bc@ubuntu:~/new1$ 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-angstrom-linux-gnueabi/usr/include -L/usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/lib -lpthread -lopencv_highgui -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -lopencv_objdetect -lm new_prog1.cpp -o new_prog1
In file included from /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/cvdef.h:91:0,
                 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 /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/objdetect/objdetect.hpp:48,
                 from new_prog1.cpp:1:
/usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/hal/interface.h:15:10: fatal error: cstddef: No such file or directory
 #include <cstddef>
          ^~~~~~~~~
compilation terminated.
Makefile:11: recipe for target 'new_prog1' failed
make: *** [new_prog1] Error 1
  1. You still got neon for --sysroot path in Makefile. You should fix that first.

  2. Search for cstddef to make sure it exists in SDK:

    find /usr/local/oecore-x86_64/sysroots/ -name cstddef*

  3. Please use a simple code for build to make sure you get the correct setup.
    For instance: Hello World in OpenCV · GitHub

  1. Corrected the --sysroot path by removing neon .

  2. Run the cstddef- Output is

    abc@ubuntu:~/new1$ sudo find /usr/local/oecore-x86_64/sysroots/ -name cstddef* [sudo] password for abc: /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/c++/7.2.0/cstddef /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/src/debug/gcc-runtime/7.2.0-r0/gcc-7.2.0/build.arm-angstrom-linux-gnueabi.arm-angstrom-linux-gnueabi/arm-angstrom-linux-gnueabi/libstdc++-v3/include/cstddef

    3)Used the code specified by you
    Output is-

    abc@ubuntu:~/new1$ 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 new_prog.cpp -o new_prog
    /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()’
    /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()’
    /usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv2/core/cvstd.hpp:655: error: undefined reference to ‘cv::String::deallocate()’
    /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()’
    /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: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: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()’
    /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/mat.inl.hpp:804: error: undefined reference to ‘cv::Mat::deallocate()’
    new_prog.cpp:9: 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()’
    new_prog.cpp:12: error: undefined reference to ‘cv::Mat::zeros(int, int, int)’
    new_prog.cpp:15: error: undefined reference to ‘cv::putText(cv::InputOutputArray const&, cv::String const&, cv::Point, int, double, cv::Scalar_, int, int, bool)’
    new_prog.cpp:24: error: undefined reference to ‘cv::imshow(cv::String const&, cv::_InputArray const&)’
    new_prog.cpp:27: error: undefined reference to ‘cv::waitKey(int)’
    collect2: error: ld returned 1 exit status
    : recipe for target ‘new_prog’ failed
    make: *** [new_prog] Error 1

Well we are back to the same place.

The undefined reference errors means linker cannot find the symbols (in this case opencv functions). This is either due to not providing the required libraries, or providing incorrect libraries during (static vs dynamic).

One other thing I forgot to mention is that you should avoid manual configuration for cross-compilication in the Makefile (and specially in CMake) when environemnt-setup... script is provided. Your Makefile should be platform agnostic and look almost the same whether you are building for x86 or ARM. The environemnt-setup... script take care of setting up any configuration for cross-compiling.

Therefore, try this reference Makefile instead:

You just have to rename example.o based on your source file (e.g. new_prog.o). Also, try removing the first line (CXX ?= g++) if there were any errors but I doubt if will have any effects.

Hey @isaac

 abc@ubuntu:~/new1$ 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 new_prog.cpp -o new_prog.o  -O2 -pipe -g -feliminate-unused-debug-types  -c -Wall -I/usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/include/opencv
    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 new_prog.o -o new_prog -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -L/usr/local/oecore-x86_64/sysroots/armv7at2hf-vfp-angstrom-linux-gnueabi/usr/share/OpenCV/3rdparty/lib -lopencv_ml -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_photo -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dpm -lopencv_face -lopencv_fuzzy -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_flann -lopencv_xobjdetect -lopencv_imgcodecs -lopencv_objdetect -lopencv_xphoto -lopencv_imgproc -lopencv_core -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgthread-2.0 -ljpeg -lwebp -lpng -lz -ltiff -lgstbase-1.0 -lgstreamer-1.0 -lgobject-2.0 -lglib-2.0 -lgstvideo-1.0 -lgstapp-1.0 -lgstriff-1.0 -lgstpbutils-1.0 -lv4l1 -lv4l2 -lavcodec -lavformat -lavutil -lswscale -lgphoto2 -lgphoto2_port -lexif -ldl -lm -lpthread -lrt -ltbb
    abc@ubuntu:~/new1$ ls
    Makefile  new_prog  new_prog.cpp  new_prog.o
    abc@ubuntu:~/new1$ ./new_prog
    bash: ./new_prog: cannot execute binary file: Exec format error

I think the Makefile link did the job.

Yes. Now you should be able to execute the binary on the target platform.

There were probably some issues in the linker flags. Using pkg-config or CMake for specifying dependencies helps to avoid such issues and also simplifies build scripts.

@isaac,
Many Many Thanks to You Isaac,Your Guidance made it possible, You guided me in the right direction and made me understand each and every point through the build.

Now Lets see what target execution has to say about the binaries.