I have to use opencv in my project and I edit my makefile for opencv. But it gives "undefined reference to " error. Code is compiling and linking if I don't use any opencv functions. I am using opencv version 4.4. I extract the robotic codes to simplify the problem. Here is my source code and makefile.
#include <math.h>
#include <iostream>
#include <webots/motor.hpp>
#include <webots/robot.hpp>
#include <opencv2/opencv.hpp>
#define TIME_STEP 64
#define NUM_PISTONS 6
using namespace webots;
using namespace cv;
// main program
int main(int argc, char **argv) {
// Testing if opencv works.
Mat img = imread("lena.png");
namedWindow("image", WINDOW_NORMAL);
imshow("image", img);
waitKey(0);
return 0;
}
Here is the makefile.
null :=
space := $(null) $(null)
WEBOTS_HOME_PATH=$(subst $(space),\ ,$(strip $(subst \,/,$(WEBOTS_HOME))))
include $(WEBOTS_HOME_PATH)/resources/Makefile.os.include
### USE_C_API = true
# define the opencv installation directory if not set in an environment variable
OPENCV_DIR = C:/opencv/build
ifneq ($(OPENCV_DIR),)
INCLUDE = -I$(OPENCV_DIR)/include
ifeq ($(OSTYPE),windows)
OPENCV_VERSION := $(subst .,,$(shell $(OPENCV_DIR)/x64/vc15/bin/opencv_version.exe))
LIBRARIES = -L$(OPENCV_DIR)/x64/vc15/lib -lopencv_world440 -lopencv_world440d
else
LIBRARIES = -L"$(OPENCV_DIR)/x64/vc15/lib" -lopencv_world440 -lopencv_world440d
endif
# Do not modify the following: this includes Webots global Makefile.include
include $(WEBOTS_HOME_PATH)/resources/Makefile.include
else
release debug profile clean:
@+echo "# opencv not installed, skipping vision controller."
endif
EDIT: I am using Windows 10 and I installed opencv already built version. Visual Studio can run opencv but webots' own environment can't eventhough I set the libraries and include files properly.
Error Messages:
C:/Program Files/Webots/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: build/release/bouncer2.o:bouncer2.cpp:(.text$_ZN2cv3MatD1Ev[_ZN2cv3MatD1Ev]+0x5e): undefined reference to `cv::fastFree(void*)'
C:/Program Files/Webots/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: build/release/bouncer2.o:bouncer2.cpp:(.text$_ZN2cv3MatD1Ev[_ZN2cv3MatD1Ev]+0x71): undefined reference to `cv::Mat::deallocate()'
C:/Program Files/Webots/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: build/release/bouncer2.o:bouncer2.cpp:(.text.startup+0x6b): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
C:/Program Files/Webots/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: build/release/bouncer2.o:bouncer2.cpp:(.text.startup+0x9e): undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
C:/Program Files/Webots/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: build/release/bouncer2.o:bouncer2.cpp:(.text.startup+0xe7): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
C:/Program Files/Webots/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: build/release/bouncer2.o:bouncer2.cpp:(.text.startup+0x100): undefined reference to `cv::waitKey(int
)'
collect2.exe: error: ld returned 1 exit status
make: *** [C:/Program Files/Webots/resources/Makefile.include:484: build/release/bouncer2.exe] Error 1