I’m building a simple test program using OpenCV on ubuntu 20.04 with gcc 9.4.0. I downloaded and built OpenCV 4.6.0, but my test program refuses to link to OpenCV shared libraries.
main.cpp:(.text+0x70): undefined reference to `cv::Mat::Mat(int, int, int)'
INCLUDE := -I../../libs/opencv/opencv-4.6.0/include
INCLUDE += -I../../libs/opencv/opencv-4.6.0/build
INCLUDE += -I../../libs/opencv/opencv-4.6.0/modules/core/include
INCLUDE += -I../../libs/opencv/opencv-4.6.0/modules/imgcodecs/include
LINK := -lstdc++
LINK += -lm
LINK += -L../../libs/opencv/opencv-4.6.0/build/lib
LINK += -lopencv_core
CFLAGS := -Wno-narrowing
default:
g++ $(CFLAGS) $(INCLUDE) $(LINK) src/*.cpp
And this is the result:
$ make
g++ -Wno-narrowing -I../../libs/opencv/opencv-4.6.0/include -I../../libs/opencv/opencv-4.6.0/build -I../../libs/opencv/opencv-4.6.0/modules/core/include -I../../libs/opencv/opencv-4.6.0/modules/imgcodecs/include -lstdc++ -lm -L../../libs/opencv/opencv-4.6.0/build/lib -lopencv_core src/*.cpp
/usr/bin/ld: /tmp/cc6wAumR.o: in function `Draw(int)':
main.cpp:(.text+0x61): undefined reference to `cv::Mat::Mat(int, int, int)'
/usr/bin/ld: main.cpp:(.text+0x6f0): undefined reference to `cv::imwrite(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
/usr/bin/ld: main.cpp:(.text+0x738): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: main.cpp:(.text+0x848): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit status
make: *** [makefile:14: default] Error 1
If i change the lib folder to something else it claims “cannot find -lopencv_core”, just as expected. So it finds the so, but for some strange reason cannot find the exported symbols??
There are no *.a and *.lib files as output, only *.so. Tried with different versions, one and the same result, can’t find the symbols in the final *.so. I tried OpenCV under Windows with latest Visual Studio, it took me only 15 minutes, the lib was linked in one click build, under linux i’m trying for 2 days now…
I’m using Windows and Visual Studio for decades, now it is the first time i try gcc and linux, and my side rhetoric question is, is there anythin, just anything, that is working under linux at all???
from the opencv build folder, after building the libs.
this will copy all required headers to
/usr/local/include/opencv4
and the libs to
/usr/local/lib
you should not depend on the src tree for any of it later (it might no more be there !)
also, linux never uses .lib files, but .dll.a or .a files for libraries
$ nm libopencv_imgcodecs.so | grep imwrite
000000000001c9c0 T _ZN2cv7imwriteERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_11_InputArrayERKSt6vectorIiSaIiEE
000000000000ff27 t _ZN2cv7imwriteERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_11_InputArrayERKSt6vectorIiSaIiEE.cold
0000000000060720 d _ZZN2cv7imwriteERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_11_InputArrayERKSt6vectorIiSaIiEEE25__cv_trace_location_fn799
0000000000063920 b _ZZN2cv7imwriteERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_11_InputArrayERKSt6vectorIiSaIiEEE31__cv_trace_location_extra_fn799
if this is the correct export of imwrite(), why g++ can not find it???