用vscode运行c++程序:
只需配置三个.json
这部分详情可以参考Ubuntu20下 vscode配置OpenCV 4.5.5
-
c_cpp_properties.json
{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "/usr/local/include/opencv4" ], "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "gnu11", "cppStandard": "gnu++14", "intelliSenseMode": "linux-gcc-x64" } ], "version": 4 }
-
launch.json
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "g++ - Build and debug active file", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", //程序文件路径 "args": [], //程序运行需传入的参数 "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": true, //运行时是否显示控制台窗口 "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "C/C++: g++ build active file", "miDebuggerPath": "/usr/bin/gdb" } ] }
-
settings.json
{ "files.associations": { "cctype": "cpp", "clocale": "cpp", "cmath": "cpp", "cstdarg": "cpp", "cstddef": "cpp", "cstdio": "cpp", "cstdlib": "cpp", "cstring": "cpp", "ctime": "cpp", "cwchar": "cpp", "cwctype": "cpp", "array": "cpp", "atomic": "cpp", "strstream": "cpp", "*.tcc": "cpp", "bitset": "cpp", "chrono": "cpp", "complex": "cpp", "condition_variable": "cpp", "cstdint": "cpp", "deque": "cpp", "list": "cpp", "unordered_map": "cpp", "vector": "cpp", "exception": "cpp", "algorithm": "cpp", "functional": "cpp", "iterator": "cpp", "map": "cpp", "memory": "cpp", "memory_resource": "cpp", "numeric": "cpp", "optional": "cpp", "random": "cpp", "ratio": "cpp", "set": "cpp", "string": "cpp", "string_view": "cpp", "system_error": "cpp", "tuple": "cpp", "type_traits": "cpp", "utility": "cpp", "fstream": "cpp", "initializer_list": "cpp", "iomanip": "cpp", "iosfwd": "cpp", "iostream": "cpp", "istream": "cpp", "limits": "cpp", "mutex": "cpp", "new": "cpp", "ostream": "cpp", "sstream": "cpp", "stdexcept": "cpp", "streambuf": "cpp", "thread": "cpp", "cfenv": "cpp", "cinttypes": "cpp", "typeindex": "cpp", "typeinfo": "cpp", "variant": "cpp", "bit": "cpp" } }
-
tasks.json
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++ build active file", /* 与launch.json文件里的preLaunchTask的内容保持一致 */ "command": "/usr/bin/g++", "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", /* 项目所需的库文件路径 */ "-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" }
2.在ros中配置opencv
只需在工作包的CMakeLists.txt文件中加入以下配置:
- 通过
locate OpenCVConfig.cmake
找到Opencv库路径 - 在cmakelist.txt里手动添加
set(OpenCV_DIR 路径)
- 在find_package中加入
OpenCV
- 在include_directories中加入
{OPENCV_INCLUDE_DIRS}
- 为可执行文件设置链接
target_link_libraries(文件名 ${OpenCV_LIBS})
(后两项不必要)