Cannot get OpenCV to compile because of undefined references?
This is a linker issue. Try:
g++ -o test_1 test_1.cpp `pkg-config opencv --cflags --libs`
This should work to compile the source. However, if you recently compiled OpenCV from source, you will meet linking issue in run-time, the library will not be found.
In most cases, after compiling libraries from source, you need to do finally:
sudo ldconfig
Undefined reference to cv::Mat::Mat()
This works on my Linux:
g++ main.cpp -I/usr/include/opencv4/ -lopencv_core -lopencv_videoio -lopencv_highgui
While this is not portable, (using cmake
would do the trick, but you'd need to learn cmake first), I'll give you a hint how you can discover this yourself.
Whenever you see an error like Undefined reference to cv::Mat::Mat()
, go to the documentation at https://docs.opencv.org/ , chose the newest version (here: https://docs.opencv.org/4.5.5/ ), enter, in the "search" window, the name of a class/function the linker cannot find (here: Mat
), read the header that defines it (here: #include<opencv2/core/mat.hpp>
), then the missing library will have the name libopencv_core.*
or libopencv_mat.*
. Find whichever is in your machine (e.g. inside /user/lib
) and link it, omitting the extension and the beginning lib
in the name. In my case the library location, with the full path, is /usr/lib/libopencv_core.so
, so I link it with -lopencv_core
. Then you need to find the remaining libs in the same way.
Learn how to automatize this, e.g. via a Makefile
, CMakeLists.txt
or just a simple bash script.
OpenCV undefined reference to
Linking OpenCV
in CMakeLists.txt
is missing.
cmake_minimum_required(VERSION 3.9)
project(untitled)
set(CMAKE_CXX_STANDARD 11)
find_package( OpenCV REQUIRED ) # locate OpenCV in system
include_directories( ${OpenCV_INCLUDE_DIRS} ) # provide library headers
add_executable(untitled main.cpp)
target_link_libraries( untitled ${OpenCV_LIBS} /usr/lib/x86_64-linux-gnu/libopencv_highgui.so) # link OpenCV libraries , hightgui.so not found by cmake so this hack
MESSAGE("OpenCV_LIBS: " ${OpenCV_LIBS} ) #display opencv libs found
if your compiler finds OpenCV and after executing cmake
it should show OpenCV libraries found.
Cannot compile because of undefined references?
There is similar question already. Try adding
-lopencv_imgcodecs
to the list of linker flags.
OpenCV 2.4.1 undefined references
My guess is that you are missing some *-dev
packages. The tutorial lists a whole bunch of them right in the beginning, and in my experience usually those are the immediate causes of failed compilations.
Unfortunately you can get an indirect message from the compiler. I guess the error message what you mention is just the tail of the build output, which can be far form the real cause. What you have to do is redirect the build output (with tee
you can split it and see it on the console and get a build log too), or you already have a build log. Study that build log and look at the first or second error message which appears during the compilation. That can indicate the missing library or dev package better.
This is just a guess, guideline. Provide us the whole build log. If the output is that short, try to increase output verbosity. Also, I might be totally wrong.
I compared the Ubuntu 11.04 tutorial linked by @Gaurav Raj and your Ubuntu 12.04 tutorial.
I'd try to install these packages:
- libpng12-dev
- libpng++-dev
- libpng3
- libpnglite-dev
- libpngwriter0-dev
- zlib1g-dbg
- zlib1g-dev
- pngtools
- libjpeg8-dev
- libjpeg8-dbg
- libjpeg-progs
- ffmpeg
- libgstreamer0.xy-0-dbg (xy supposed to be a version for Ubuntu 12.04 this is probably newer version than 0.10 for Ubuntu 11.04)
- libgstreamer0.xy-dev
- libxine-dev
- libunicap2-dev
- swig
- libdc1394-utils
Before installing them, you can check if they installed by saying dpkg -l | grep ${pkg_name_to_check}
.
Related Topics
Why Can't I Open Avi Video in Opencv
How to Define a String Literal in Gcc Command Line
What Exactly Is an 'Aligned Pointer'
Is There Any Lame C++ Wrapper\Simplifier (Working on Linux MAC and Win from Pure Code)
Program Behaving Strangely on Online Ides
Determining Exception Type After the Exception Is Caught
Using Hierarchy in Findcontours () in Opencv
How to Create a Dll with Swig from Visual Studio 2010
What Is the Purpose of _Cxa_Pure_Virtual
C++11 Way to Index Tuple at Runtime Without Using Switch
Initializing Default Values in a Struct
Why Is Statically Linking Glibc Discouraged
How to Reduce Compile Time, and Linking Time for Visual C++ Projects (Native C++)
Difference Between Using #Include<Filename> and #Include<Filename.H> in C++
Casting Pointer to Array (Int* to Int[2])
Constructor Invocation Mechanism
C++ - Why Static Member Function Can't Be Created with 'Const' Qualifier