Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

G++ returning errors after attempted compilation with openCV using G++

I'm trying to run a simple c++ file that includes openCV libraries and creates a simple Mat. Unfortunately, when I try and compile the file using g++, it returns a number of errors.

So far, I've tried removing the Mat definition all together and just included the openCV files, which worked! I'm new to the command line, so I'm not sure if it's an issue with the way I'm using g++. However, I have worked quite a bit on openCV in Xcode (though with Xcode, there's no need to work in the command line, as everything is neatly built for you at run time).

Also, this is my first time posting to Stack Overflow, so I may have made a mistake formatting.

This is my main.cpp file which I'm attempting to compile. As I mentioned before, if I remove the line "Mat test;", it compiles successfully and writes to console as it should.

#include <iostream>
#include <string>

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>


using namespace std;
using namespace cv;

int main() {
        Mat test;
        cout << "Mat Defined!" << endl;
        return(0);
}

This is the g++ command I am using:

g++ -o main.out main.cpp

and this is the error I get when I try and run it:

/usr/bin/ld: /tmp/ccbbhoNd.o: in function `cv::Mat::~Mat()':
main.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3c): undefined reference to `cv::fastFree(void*)'
/usr/bin/ld: /tmp/ccbbhoNd.o: in function `cv::Mat::release()':
main.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x68): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status

Any help is greatly appreciated. I also opted out of the verbose flag for g++, as it returned a ton of lines and I wasn't sure how people feel about 50+ lines of code. Thanks so much!

like image 988
Thomas Rimer Avatar asked Feb 01 '26 03:02

Thomas Rimer


2 Answers

You have problem with linking to OpenCV library. You need to pass to ld program information where to find all necessary lib's. In your case it will be:

g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -o main.out  main.cpp -lopencv_core

All OpenCV linker flags are:

-lopencv_calib3d
-lopencv_contrib
-lopencv_core
-lopencv_features2d
-lopencv_flann
-lopencv_highgui
-lopencv_imgproc
-lopencv_legacy
-lopencv_ml
-lopencv_nonfree
-lopencv_objdetect
-lopencv_photo
-lopencv_stitching
-lopencv_superres
-lopencv_ts
-lopencv_video
-lopencv_videostab

After all I suggest to jump in CMake build system for your C++ project's. With OpenCV it's much easier to compile program. Checkout doc's. CMake will prepare for you makefile with all needed dependencies.

Best Regards!

like image 141
GoRo3 Avatar answered Feb 02 '26 16:02

GoRo3


It fails because you didn't link the libraries in your command. During compilation, your operating system can't reach opencv libraries because you didn't address them. You need to compile your cpp file using the code below:

g++ -std=c++11 main.cpp `pkg-config --libs --cflags opencv` -o output
like image 22
Yunus Temurlenk Avatar answered Feb 02 '26 17:02

Yunus Temurlenk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!