在VSCODE MAC上使用VCPKG无法开源文件错误

发布于 2025-02-11 17:28:26 字数 1303 浏览 0 评论 0 原文

我正在尝试使用C ++使用外部库(特别是OpenCV),但我遇到了一些麻烦。似乎使用VCPKG是解决此问题的最简单方法。到目前为止,我已经遵循了这些教程: https://wwww.youtube.com/watch?v = b7sdgk7y510 https://www.youtube.com/www.youtube.com/watch?v=izek3ie5fz3ie5fz3ie5fz0 但是,当我尝试包含一个文件时,我会一直遇到这些错误:

无法开放源文件“ opencv2/core.hpp”

'opencv2/core.hpp'找不到

vscode提供的快速修复的vscode所提供的快速修复,以通过VCPKG,但我已经做到了。我根据教程将VCPKG链接起来,并在我的settings.json文件中包含了CMake工具链文件的路径。

这是我对设置的更改。json文件:

{
"cmake.configureSettings": {
    "CMAKE_TOOLCHAIN_FILE": "/Users/oliverpasquesi/coding/dev/vcpkg/scripts/buildsystems/vcpkg.cmake"
}

}

这是cmakelists.txt文件(与列出的第二个教程中的一个相同):

cmake_minimum_required(VERSION 3.0.0)
project(photoText.cpp VERSION 0.1.0)

add_executable(photoText.cpp main.cpp)

这是c_cpp_properties.json file的includepath部分(包括第二个路径对被抛出的错误没有任何影响):

"includePath": [
            "${default}",
            "/Users/oliverpasquesi/coding/dev/vcpkg/installed/x64-osx/include/opencv2/**"
        ]

如果有用,我将在运行Darwin X64 21.5.0的Mac上使用VSCODE 1.49.2。

感谢您的帮助!

I'm trying to use external libraries (specifically opencv) with C++ but I'm having some trouble. It seems like using vcpkg is the easiest way to go about this. So far I've followed these tutorials: https://www.youtube.com/watch?v=b7SdgK7Y510 and https://www.youtube.com/watch?v=iZeK3Ie5Fz0 but I keep getting these errors when I try to include a file:

cannot open source file "opencv2/core.hpp"

'opencv2/core.hpp' file not found

The quick fix provided by vscode says to install opencv through vcpkg but I've already done that. I've linked vcpkg according to the tutorials and included the path to the cmake toolchain file in my settings.json file.

Here is the change I made to my settings.json file:

{
"cmake.configureSettings": {
    "CMAKE_TOOLCHAIN_FILE": "/Users/oliverpasquesi/coding/dev/vcpkg/scripts/buildsystems/vcpkg.cmake"
}

}

Here is the CMakeLists.txt file (it is the same as the one from the 2nd tutorial listed):

cmake_minimum_required(VERSION 3.0.0)
project(photoText.cpp VERSION 0.1.0)

add_executable(photoText.cpp main.cpp)

Here is the includePath portion of the c_cpp_properties.json file (including the 2nd path doesn't make any difference in the errors being thrown):

"includePath": [
            "${default}",
            "/Users/oliverpasquesi/coding/dev/vcpkg/installed/x64-osx/include/opencv2/**"
        ]

If it's useful, I'm using vscode 1.49.2 on a mac running Darwin x64 21.5.0.

I appreciate the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满栀 2025-02-18 17:28:26

您还需要在cmakelists.txt文件中引用库,因为这是CMAKE用来为构建准备项目的文件。

一种现代的方法是添加两行:

  1. find_package()行以查找所使用的库。当您已经引用过VCPKG.CMAKE工具链文件时,CMAKE应该能够在添加该行时找到VCPKG已安装的任何库。参见 https://cmake.org/cmake.org/cmake/cmake/help/help/latest/command/command/command/command/find_pampack/find_pampagge- .html 用于文档。
  2. target_link_libraries()行列出了库以链接到您的项目。请参阅此处的CMAKE文档:

当您使用openCV库时,您可能还需要查看此问题的答案:使用cmake 链接openCV

特别是,尝试将这些行添加到您的cmakelists.txt.txt:

find_package(OpenCV REQUIRED)
target_link_libraries(photoText.cpp ${OpenCV_LIBS})

vcpkg文档上的cmake Integration(供其他参考): https:///vcppkg.io of cmake-integration.html

希望这会有所帮助!

免责声明:我从事VCPKG工具。

You need to reference your library in the CMakeLists.txt file as well, since that's the file CMake uses to prepare your project for a build.

A modern way to do this is to add two lines:

  1. A find_package() line to find the libraries you are using. As you already referenced the vcpkg.cmake toolchain file, CMake should be able to find any libraries already installed by vcpkg when you add that line. See https://cmake.org/cmake/help/latest/command/find_package.html for documentation.
  2. A target_link_libraries() line that lists libraries to link against your project. See the CMake documentation here: https://cmake.org/cmake/help/latest/command/target_link_libraries.html

As you are using the opencv library, you may want to look at the answers to this question as well: Linking Opencv in a project using cmake

Specifically, try adding these lines to your CMakeLists.txt:

find_package(OpenCV REQUIRED)
target_link_libraries(photoText.cpp ${OpenCV_LIBS})

vcpkg documentation on its CMake integration (for additional reference): https://vcpkg.io/en/docs/users/buildsystems/cmake-integration.html

Hopefully this helps!

Disclaimer: I work on the vcpkg tool.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文