致命错误LNK1120:5 个未解决的外部问题

fatal error LNK1120: 5 unresolved externals

提问人:Muhammad Ikhwan Perwira 提问时间:6/24/2022 最后编辑:Christoph RackwitzMuhammad Ikhwan Perwira 更新时间:6/24/2022 访问量:950

问:

我正在运行 c++ 简单的 opencv 程序,它给了我这样的错误

Starting build...
cl.exe /Zi /EHsc /nologo /FeC:\Users\User\Documents\KULIAH\LAB_SOFTWARE\Program_C\belajar_syntax\halo.exe C:\Users\User\Documents\KULIAH\LAB_SOFTWARE\Program_C\belajar_syntax\halo.cpp -I C:\opencv\build\include /link /LIBPATH:C:\opencv\build\x64\vc15\lib
halo.cpp
halo.obj : error LNK2019: unresolved external symbol "public: __thiscall cv::Mat::Mat(int,int,int,class cv::Scalar_<double> const &)" (??0Mat@cv@@QAE@HHHABV?$Scalar_@N@1@@Z) referenced in function _main
halo.obj : error LNK2019: unresolved external symbol "public: __thiscall cv::Mat::~Mat(void)" (??1Mat@cv@@QAE@XZ) referenced in function _main
halo.obj : error LNK2019: unresolved external symbol "void __cdecl cv::destroyAllWindows(void)" (?destroyAllWindows@cv@@YAXXZ) referenced in function _main
halo.obj : error LNK2019: unresolved external symbol "int __cdecl cv::waitKey(int)" (?waitKey@cv@@YAHH@Z) referenced in function _main
halo.obj : error LNK2019: unresolved external symbol "void __cdecl cv::imshow(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class cv::_InputArray const &)" (?imshow@cv@@YAXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV_InputArray@1@@Z) referenced in function _main
C:\Users\User\Documents\KULIAH\LAB_SOFTWARE\Program_C\belajar_syntax\halo.exe : fatal error LNK1120: 5 unresolved externals

Build finished with error(s).
The terminal process terminated with exit code: -1.

Terminal will be reused by tasks, press any key to close it.

> Executing task: C/C++: cl.exe build active file <

Starting build...
cl.exe /Zi /EHsc /nologo /FeC:\Users\User\Documents\KULIAH\LAB_SOFTWARE\Program_C\belajar_syntax\halo.exe C:\Users\User\Documents\KULIAH\LAB_SOFTWARE\Program_C\belajar_syntax\halo.cpp -I C:\opencv\build\include /link /LIBPATH:C:\opencv\build\x64\vc15\lib\**
halo.cpp
halo.obj : error LNK2019: unresolved external symbol "public: __thiscall cv::Mat::Mat(int,int,int,class cv::Scalar_<double> const &)" (??0Mat@cv@@QAE@HHHABV?$Scalar_@N@1@@Z) referenced in function _main
halo.obj : error LNK2019: unresolved external symbol "public: __thiscall cv::Mat::~Mat(void)" (??1Mat@cv@@QAE@XZ) referenced in function _main
halo.obj : error LNK2019: unresolved external symbol "void __cdecl cv::destroyAllWindows(void)" (?destroyAllWindows@cv@@YAXXZ) referenced in function _main
halo.obj : error LNK2019: unresolved external symbol "int __cdecl cv::waitKey(int)" (?waitKey@cv@@YAHH@Z) referenced in function _main
halo.obj : error LNK2019: unresolved external symbol "void __cdecl cv::imshow(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class cv::_InputArray const &)" (?imshow@cv@@YAXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV_InputArray@1@@Z) referenced in function _main
C:\Users\User\Documents\KULIAH\LAB_SOFTWARE\Program_C\belajar_syntax\halo.exe : fatal error LNK1120: 5 unresolved externals

我怀疑这是因为库链接器问题,这是我的tasks.json

{
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: cl.exe build active file",
      "command": "cl.exe",
      "args": [
        "/Zi",
        "/EHsc",
        "/nologo",
        "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
        "${file}",
        "-I",
        "C:\\opencv\\build\\include",
        "/link",
        "/LIBPATH:C:\\opencv\\build\\x64\\vc15\\lib\\"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": [
        "$msCompile"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "Task generated by Debugger."
    }
  ],
  "version": "2.0.0"
}

这是我运行的代码

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main() {
  // Create blue image
  Mat blueImage(10, 10, CV_8UC1, Scalar(0, 0, 255));

  // Display the image.
  imshow("grayscale image", blueImage);

  // Wait for a keystroke.
  waitKey(0);

  // Destroys all the windows created
  destroyAllWindows();
  return 0;
}

根据我用来编译程序。tasks.jsoncl.exe

在我从 python 转向学习 opencv 后,我是 C++ 环境的新手,因为我听说 C++ 很快。就像我说的,我有链接并包含在参数中。但仍然有错误。cl.exe

我也尝试更改仍然给我错误。vc15vc14

我没有从源代码安装 opencv,但我从发布官方网站下载了名称opencv-4.6.0-vc14_vc15.exe

这是里面的东西C:\opencv\build\x64\vc14\lib

C++ 可视化工作室 OpenCV 未解析的外部 CL

评论

0赞 jkb 6/24/2022
看起来您正在告诉链接器要查看哪个目录,但我没有看到您指定要链接的实际库文件的位置。
0赞 Muhammad Ikhwan Perwira 6/24/2022
我更新了我的问题,我已经提供了有关您所指目录的其他信息,那么要链接到您的意思是哪个库文件?
0赞 Muhammad Ikhwan Perwira 6/24/2022
这是否意味着我应该添加一些参数,如果为 true,我应该使用什么语法?/linkcl.exe
0赞 jkb 6/24/2022
我不是 100% 确定这一点(我通常使用 cmake),但您可以尝试将“opencv_world460.lib”行添加到 tasks.json 中的参数列表中
1赞 jkb 6/24/2022
看起来您正在编译 32 位代码,但链接到 64 位库。您可能希望将编译配置为 64 位。顺便说一句,我不确定如何为命令行执行此操作。是否通过 Visual Studio 命令行提示符之一打开 VS Code?

答: 暂无答案