vscode编写c++文件——添加自定义库(ubuntu20.04)

vscode编写c++文件——添加自定义库

环境:ubuntu20.04、vscode 1.70、g++ 9.4.0
项目结构如下:add.h和add.cpp是自定义的库
在这里插入图片描述

  1. 把自定义的头文件放入c_cpp_properties.json 中的includePath中:
    在这里插入图片描述

  2. 将实现的库函数.cpp放入tasks.json,以便正常编译和调用(加在args里):
    在这里插入图片描述

  3. 此外,为了编译单个文件,一次运行一个源程序,需要在tasks.json的args下设置为file:
    在这里插入图片描述

  4. 运行结果:
    在这里插入图片描述
    在这里插入图片描述

附录:

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtConnect": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",  /* 与launch.json文件里的preLaunchTask的内容保持一致 */
            "command": "/usr/bin/g++",    /*specifies the program to run;*/
            "args": [
                "-std=c++11",
                "-g",
                "${file}",   /* 编译单个文件 */
                //"${fileDirname}/*.cpp",  /* 编译多个文件 */
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",  /* 输出文件路径 */
 
                /* 项目所需的头文件路径 */
                "-I","${workspaceFolder}/",
                "-I","/usr/local/include/",
                "-I","/usr/local/include/opencv4/",
                "-I","/usr/local/include/opencv4/opencv2",
                "-I","include",
                "./source/add.cpp",  /*库的实现,需要加,不然显示未定义*/
 
                /* 项目所需的库文件路径 */
                "-L", "/usr/local/lib",
 
                /* OpenCV的lib库 */
                "/usr/local/lib/libopencv_*",
 
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include/opencv4",
                "include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

main.cpp:

#include <opencv2/opencv.hpp>
#include <iostream>
#include "include/add.h"
 
using namespace std;
using namespace cv;
 
int main(int argc, char* argv[]) {
    // 测试自定义头文件
    int a = 5;
    int b = 6;
    cout << add(5, 6) << endl;
    

    // 测试opencv
    const char* imagename = "test.jpg";
    
    Mat img = imread(imagename, 1);
 
    if (img.empty()) {
        fprintf(stderr, "Can not load image %s\n", imagename);
        return -1;
    }
    imshow("image", img);
    
    waitKey();
    return 0;
}

如有不对的地方欢迎大佬们指出!

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wilbur11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值