在Webots中使用OpenCV时如何解决链接器错误

问题描述

我必须在项目中使用opencv,然后编辑opencv的makefile。但是它给出了“未定义的引用”错误。如果我不使用任何opencv函数,则代码正在编译和链接。我正在使用opencv版本4.4。我提取了机械手代码以简化问题。这是我的源代码和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;
  
}

这是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

编辑:我正在使用Windows 10,并且已安装opencv已构建版本。 Visual Studio可以运行opencv,但即使我设置了库并正确包含文件,我们的机器人自己的环境也无法运行。

错误消息:

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,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,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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...