环境:ubuntu20.04、vscode 1.70、g++ 9.4.0 项目结构如下:add.h和add.cpp是自定义的库
-
把自定义的头文件放入c_cpp_properties.json 中的includePath中: -
将实现的库函数.cpp放入tasks.json,以便正常编译和调用(加在args里): -
此外,为了编译单个文件,一次运行一个源程序,需要在tasks.json的args下设置为file: -
运行结果:
附录:
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",
"command": "/usr/bin/g++",
"args": [
"-std=c++11",
"-g",
"${file}",
"-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",
"/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;
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;
}
如有不对的地方欢迎大佬们指出!
|