Visual Studio Code - configure OpenCV libraries for C++ - c++

I've installed OpenCV On Ubuntu successfully and I managed to run a sample code as:
g++ main.cpp -o testoutput -std=c++11 `pkg-config --cflags --libs opencv`
I've tried to run it with Visual Studio Code, I've installed the extension of C/C++ and code runner and ran it with the following configuration:
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-std=c++11","`pkg-config","--cflags","--libs opencv`"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
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": ["-std=c++11","`pkg-config","--cflags","--libs opencv`"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"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"
}
]
}
I got the following error:
[Running] cd "/home/kfir/code/opencv_test/" && g++ main.cpp -o main && "/home/kfir/code/opencv_test/"main
main.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory
#include <opencv2/core.hpp>
^~~~~~~~~~~~~~~~~~
compilation terminated.
[Done] exited with code=1 in 0.032 seconds
Note: I'm using VSCode on mac and connect Ubuntu remote machine by ssh, the terminal works fine with the command of g++ above

Be sure to add the location to openCV header files in your includePath.
c_cpp_properties.json file :
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${default}",
"~/opencv4.5-custom/include/opencv4"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
Mine says "~/opencv4.5-custom/include/opencv4" but it could be "/usr/include/opencv4" or something else, depending on how and where you installed openCV.
You also need to modify task.json to add the arguments to the compiler as given by the pkg-config --cflags --libs opencv4 command, if you would run it in the terminal. You'll need to give the path to the shared object files.
Here's the content of my task.json file :
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I", "~/opencv4.5-custom/include/opencv4",
"-L", "~/opencv4.5-custom/lib",
"-l", "opencv_core",
"-l", "opencv_videoio",
"-l", "opencv_imgproc",
"-l", "opencv_highgui"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
You'll also need the change the paths depending to you setup.
Here I have only included the modules that are used by my program (with the lines "-l", "opencv_core" and so on...) so add or remove modules according to your needs.

Related

Errors: "#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit" and "cannot open source file"

I am getting the following errors after setting up C++ on VS Code and running starter code.
OS: MacOS Ventura 13.1 (Intel)
Editor: VS Code.
Compiler: clang++ (default)
#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit
cannot open source file "tr1/cwchar" (dependency of "iostream")
Below are my config files (c_cpp_properties, tasks, launch)
c_cpp_properties.json
{
"env": {
"myDefaultIncludePath": ["${workspaceFolder}", "${workspaceFolder}/include"],
"myCompilerPath": "/usr/local/bin/clang++"
},
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/Cellar/gcc/11.2.0_3/include/c++/11/tr1",
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1"
],
"defines": [],
"macFrameworkPath": [
"/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang++",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "macos-gcc-x64"
}
],
"version": 4}
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"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": "Task generated by Debugger."
}
]
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: clang++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang++ build active file"
}
]
}
The c++ code builds and runs. The error however persists. Anything regarding C++ installation might help.
PS: This is my first time setting up C++.

Cannot compile .cpp in vscode terminal

When i try to run my .cpp program in bash terminal in vscode, I receive this response(when I fisrstly compile --> then run from terminal, it runs without issues):
junte#anton MINGW64 /c/cpp_code
$ cd "C:\cpp_code\"
> cd "c:\cpp_code\" && g++ helloworld.cpp -o helloworld && "c:\cpp_code\"helloworld
>
The "Run in terminal" is on and compiler is in PATH.
I'm curoius where is the issue and how to fix it
Here is my tasks.json :
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe сборка активного файла",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Задача создана отладчиком."
}
],
"version": "2.0.0"
}
And 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++.exe - Сборка и отладка активного файла",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Включить автоматическое форматирование для gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe сборка активного файла"
}
]
P.S thanks for any help<3

How to Run gtk+ of cpp applicatiion In vscode?

I am working with gtk+ application in cpp ,I want Bastler Pylon integration with gtk and I found by lots of research Gtk+ application uses MinGw compiler and Pylon uses MSVC compiler My problem is to run a gtk+ code into vscode ide,I never used vscode before I usually use sublime text editor in ubuntu and I want above integration in windows....
my four file structure are below...
c_cpp_properties.json
{
"configurations": [
{
"name": "Gtk_dev",
"includePath": [
"${workspaceFolder}/**",
"C:/msys64/mingw64/include/**",
"C:/msys64/mingw64/lib/glib-2.0/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/msys64/mingw64/bin/gcc.exe",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x64",
"compilerArgs": [],
"browse": {
"limitSymbolsToIncludedHeaders": false,
"path": []
}
}
],
"version": 4
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "C:\\msys64\\mingw64\\bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: gcc.exe build active file",
"command": "g++",
"args": [
"-g",
"-pthread",
"-mms-bitfields",
"-IC:/msys64/mingw64/include/gtk-3.0",
"-IC:/msys64/mingw64/include/cairo",
"-IC:/msys64/mingw64/include",
"-IC:/msys64/mingw64/include/pango-1.0",
"-IC:/msys64/mingw64/include/fribidi",
"-IC:/msys64/mingw64/include/atk-1.0",
"-IC:/msys64/mingw64/include/lzo",
"-IC:/msys64/mingw64/include/freetype2",
"-IC:/msys64/mingw64/include/libpng16",
"-IC:/msys64/mingw64/include/harfbuzz",
"-IC:/msys64/mingw64/include/pixman-1",
"-IC:/msys64/mingw64/include/gdk-pixbuf-2.0",
"-IC:/msys64/mingw64/include/glib-2.0",
"-IC:/msys64/mingw64/lib/glib-2.0/include",
"${C:/msys64/mingw64/bin/g++}",
"-LC:/mingw64/lib",
"-lgtk-3",
"-lgdk-3",
"-lz",
"-lgdi32",
"-limm32",
"-lshell32",
"-lole32",
"-luuid",
"-lwinmm",
"-ldwmapi",
"-lsetupapi",
"-lcfgmgr32",
"-lpangowin32-1.0",
"-lpangocairo-1.0",
"-lpango-1.0",
"-lharfbuzz",
"-latk-1.0",
"-lcairo-gobject",
"-lcairo",
"-lgdk_pixbuf-2.0",
"-lgio-2.0",
"-lgobject-2.0",
"-lglib-2.0",
"-lintl",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "C:/msys64/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\msys64\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
}
]
}
Error showing.................by Pressing for compile and run(ctr+shift+B)..The major problem is there is no mention which file has an error
g++.exe: error: .0: No such file or directory
g++.exe: error: .0: No such file or directory
g++.exe: error: .0: No such file or directory
g++.exe: error: .0: No such file or directory
.....some included files output.....
I ran into the same error yesterday. The error message actually hints at what is wrong (.0). Some of the linked libs (-l) have .0 in their name which seems to be a problem (at least on windows). Putting the respective libs into single quotation marks does the trick:
tasks.json
[...]
"-lcfgmgr32",
"'-lpangowin32-1.0'",
"'-lpangocairo-1.0'",
"'-lpango-1.0'",
"-lharfbuzz",
"'-latk-1.0'",
"-lcairo-gobject",
"-lcairo",
"'-lgdk_pixbuf-2.0'",
"'-lgio-2.0'",
"'-lgobject-2.0'",
"'-lglib-2.0'",
"-lintl",
[...]

Include Path is not recognized in vscode

I've been reading at almost every question related to include paths in vscode but I just can't solve my issue.
My project folder's custom libraries is in the following path
"/home/gdl/DSS2-DEV/infra"
The infra folder is structured as such:
>Infra
>Include
... some header files
>lib1
>Include
... header files
>src
... cpp files
>lib2
>Include
... header files
>src
... cpp files
So, in order to use any of these libraries, I set up the vscode environment
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [ {"name": "LD_LIBRARY_PATH", "value": "/usr/local/lib:$LD_LIBRARY_PATH" }],
"externalConsole": false,
"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"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I",
"/home/gdl/DSS2-DEV/infra"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/home/gdl/DSS2-DEV/infra"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
and yet, not a single library can be included:
cpp file
#include <cstdio>
#include <libadm.h>
int main()
{
return 0;
}
Executing task: /usr/bin/g++ -g /home/gdl/DSS2-DEV/infra/parser.cpp -o /home/gdl/DSS2-DEV/infra/parser -I /home/gdl/DSS2-DEV/infra <
/home/gdl/DSS2-DEV/infra/parser.cpp:2:20: fatal error: libadm.h: No such file or directory
#include <libadm.h>
I tried using slashes but it doesn't change a thing. What drives me crazy is that if I CTRL-click the library it actually shows me the header file of that library, meaning that vscode is correctly linking files togheter.
I also tried using quotes instead of brackets for the include but nothing works

Running a cpp file on a Linux system creates a different file that does not execute?

I am trying to learn C++, but I am unable to get any code running.
#include <iostream>
int main()
{
std::cout << "Hello world!";
return 0;
}
When I run this(or another file), it creates a different unrecognized file. (https://imgur.com/a/07rpngl, picture 2) Then it gives the error at picture 1.
This is the 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": "cpp - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "cpp build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
And this is the task file:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "shell",
"label": "cpp build active file",
"command": "/usr/bin/cpp",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
The settings.json file is empty.
Both running without debugging and with it gives the same error. How can I fix this?
Ways that were tried:
1) Tried restarting both the application and the computer, doesn't work.
2) Tried using terminal to compile, run and debug the code. It works, but is there a way to do this over VS Code? (Should I open another question just for VS Code?)
# First, change your directory to the one that you want to use
# Run the following to compile in the same directory:
g++ -Wall -Wextra -pedantic -Wshadow -std=c++11 -g -o nameforexe source.cpp
# Change -std=c++11 to whatever version of c++ you want to use, and also the file names.
# To run it after this normally do this:
./nameforexe
# To debug, do:
gdb ./nameforexe
# After gdb opens, write "run" without " to run the file.
# If there is a problem, it will tell you. If there isn't it will run normally.
Thanks to David C. Rankin for this. There is more info in the comments for this question.
3) Using CMake(suggested by dboy). I wasn't able to do this. After it says generated in the output section, I wasn't able to find the file to run the HelloWorld application. (Image 3 shows the files)
The CMakeLists.txt file:
cmake_minimum_required(VERSION 3.10.2)
project("HelloWorld")
set(SOURCE Helloworld.cpp)
add_executable("HelloWorldBuild" HelloWorld.cpp)
It didn't show any problems while compiling. CMake might be to advanced for me for now.