1

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
Kamil Kaya
  • 47
  • 5
  • Which OS are you using? Did you set OPENCV_DIR? If not where is opencv installed? – David Mansolino Aug 17 '20 at 05:48
  • 1
    I would recommend you to either recompile OpenCV yourself or use the [pre-compiled version for MSYS2](https://packages.msys2.org/package/mingw-w64-x86_64-opencv) as MSYS2 is used to compile the `libController.dll` used by your robot controllers. – Olivier Michel Aug 17 '20 at 06:55
  • @DavidMansolino I am using Windows 10. I installed it C\opencv. I didn't install opencv with repository. I can run with visual studio but I cant do it with webots own environment. I did nothing different from visual studio than the makefile. I included the same paths with visual studio. But I couldn't solve it why the error is happening. – Kamil Kaya Aug 17 '20 at 08:43
  • @OlivierMichel So the problem is webots can't use the libraries that I didn't compile myself. But Visual Studio can. I set everything correctly in visual studio and it worked and I did nothing different with the makefile. What's causing this actually? – Kamil Kaya Aug 17 '20 at 08:51
  • *it gives "undefined reference to"* what exactly? Please post **unedited** error messages. – n. m. could be an AI Aug 17 '20 at 09:03
  • @n.'pronouns'm. I editted the subject. You can see the exact errors. – Kamil Kaya Aug 17 '20 at 09:22
  • GCC and MSVC are incompatible, you cannot mix and match C++ libraries between the two. (It's OK to mix C libraries), You are trying to link with the MSVC version of OpenCV libraries, but the executable is built with gcc. It cannot work. – n. m. could be an AI Aug 17 '20 at 09:50
  • @n.'pronouns'm. So you are implying that there is an incompetibility with gcc and opencv that I use. So what do you suggest? How can I built the opencv so that webots can link the files properly? – Kamil Kaya Aug 18 '20 at 09:19
  • You can download a prebuilt package from the link above. – n. m. could be an AI Aug 18 '20 at 10:36
  • @KamilKaya did you resolved this problem. I'm also facing this same problem – Chamod Feb 05 '22 at 05:24
  • @Chamod I resolved it. But I can't remember how I solved it. But I linked the libraries of opencv in Visual Studio environment and used Visual Studio editor for coding and building. I think there was a section for how to use Visual Studio in Webots in Webots' own documentation. – Kamil Kaya Feb 27 '22 at 15:42

0 Answers0