简体   繁体   English

如何在 Visual Studio Code 的 C++ 项目中使用外部库

[英]How to use external libraries in a C++ project in Visual Studio Code

I am very new to this so bear with.我对此很陌生,所以请耐心等待。 I have been trying to figure out the basics of starting a project in c++ in vs code but I cannot figure out how to use an external library.我一直试图弄清楚在 vs 代码中在 c++ 中启动项目的基础知识,但我不知道如何使用外部库。 In order to experiment I have got a simple setup with just a HelloWorld.cpp file as well as tasks.json and c_cpp_properties.json files.为了进行实验,我有一个简单的设置,只有一个 HelloWorld.cpp 文件以及 tasks.json 和 c_cpp_properties.json 文件。 I am running the file by creating a HelloWorld executable.我通过创建一个 HelloWorld 可执行文件来运行该文件。

I have looked into using vcpkg and I have got through all the steps to get it set up (including integration) but I can't seem to get it to work.我已经研究过使用 vcpkg 并且我已经完成了设置它的所有步骤(包括集成),但我似乎无法让它工作。 As a test I am trying to use the "opencv2" library, but if I try to use #include <opencv2/opencv2.hpp> at the top of the HelloWorld.cpp file I just get an error saying that the file could not be found.作为测试,我正在尝试使用“opencv2”库,但是如果我尝试在 HelloWorld.cpp 文件的顶部使用#include <opencv2/opencv2.hpp> ,我只会收到一条错误消息,指出该文件不能成立。

here's what my c_cpp_properties.json file looks like:这是我的 c_cpp_properties.json 文件的样子:

{
"configurations": [
    {
        "name": "Mac",
        "includePath": [
            "${default}"
        ],
        "defines": [],
        "macFrameworkPath": [],
        "compilerPath": "/usr/bin/clang++",
        "cStandard": "gnu17",
        "intelliSenseMode": "${default}"
    }
],
"version": 4
}

This is my tasks.json:这是我的任务。json:

{
"version": "2.0.0",
"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C++: clang++ build active file",
        "command": "/usr/bin/clang++",
        "args": [
            "-std=c++17",
            "-stdlib=libc++",
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "${workspaceFolder}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "compiler: /usr/bin/clang++"
    }
]
} 

and finally HelloWorld.cpp:最后是 HelloWorld.cpp:

#include <iostream>
#include <stdio.h>
#include <opencv2/opencv2.hpp>

using namespace std;

int main() {
    cout << "\n Hello World \n";
    return 0;
}

I have done a lot of experimenting with adding different includePaths etc, but no luck.我已经做了很多尝试添加不同的 includePaths 等,但没有运气。 I think that vcpkg claims, however, that by using user-wide integration it is not necessary to specify includePaths?但是,我认为 vcpkg 声称,通过使用用户范围的集成,不需要指定 includePaths? I think there must be something very simple that I'm missing but I can't seem to figure out what it is.我认为一定有一些非常简单的东西我错过了,但我似乎无法弄清楚它是什么。 Thanks for any help!谢谢你的帮助!

*Just edited the question as I realised the problem was slightly different to what I thought *刚刚编辑了问题,因为我意识到问题与我的想法略有不同

When you run the command to compile the code, you can pass the actual path to the library, to the compiler so that it can find the files needed.当您运行命令编译代码时,您可以将库的实际路径传递给编译器,以便它可以找到所需的文件。 For example, check out the following command.例如,查看以下命令。

clang++ HelloWorld.cpp -I/path to the opencv include folder/include -L/path to the opencv lib folder/lib -o output

Passing the paths explicitly like this is not really necessary if you had installed OpenCV properly using cmake by following their documentation since the compiler will be able to find it.如果您按照他们的文档正确使用 cmake 安装了 OpenCV,则不需要像这样显式传递路径,因为编译器将能够找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM