我在Linux VS Code上尝试运行OpenCV时遇到了问题。我按照教程的指示操作,但编译时仍然失败,并显示如下错误:
/tmp/ccEkxgny.o: 在函数‘cv::String::~String()’中:
cam_usb.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): 对‘cv::String::deallocate()’未定义的引用
测试代码很简单:
#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main()
{
cout << "Test" << endl;
}
只要移除OpenCV头文件,代码就能正常执行。
我已经在c_cpp_properties.json中设置了正确的包含目录,指向OpenCV安装目录:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"/home/XXXX/opencv/include/opencv2/**",
"${workspaceFolder}/**",
"/usr/local/include/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
同时,在tasks.json中也链接了依赖库:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lopencv_core",
"-lopencv_imgproc",
"-lopencv_highgui"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
尽管如此,错误仍然存在。很遗憾,我找不到进一步的解决方案。如果有任何帮助,我将非常感激。提前感谢您的关注和帮助。(抱歉,我是初次在Linux上使用cpp和opencv... 🙂)