Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VSCode is unable to compile OpenCV simple example even after adding the path to configuration #7662

Closed
wbadry opened this issue Jun 8, 2021 · 1 comment

Comments

@wbadry
Copy link

wbadry commented Jun 8, 2021

Bug type: Language Service

Describe the bug

  • OS and Version: 20.04.2 LTS
  • VS Code Version: 1.56.2
  • C/C++ Extension Version: 1..4.0
  • Other extensions you installed (and if the issue persists after disabling them): ROS 0.6.8

I tried to use VSCode to compile a simple check for OpenCV. I already have the latest version of OpenCV

$ apt show libopencv-dev
Package: libopencv-dev
Version: 4.2.0+dfsg-5
Priority: optional
Section: universe/libdevel
Source: opencv
Origin: Ubuntu 

The library is installed as expected in /usr/include/opencv4

Screenshot from 2021-06-08 16-21-03

I made a simple C++ program

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

int main()
{
    cv::Mat img = cv::imread("../images/starry_night.jpg", cv::IMREAD_COLOR);

    if(img.empty())
    {
        std::cout << "Could not read the image ! " << std::endl;
        return 1;
    }

    imshow("Display window", img);
    int k = cv::waitKey(0); // Wait for a keystroke in the window
    
    if(k == 's')
    {
        cv::imwrite("starry_night.png", img);
    }
    //! [imsave]


    return 0;
}

The extension was able to detect the library location, and it was added to c_cpp_properties.json :

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

Screenshot from 2021-06-08 16-21-03

I then configured a building task.json (tried cpp, g++, same result)

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

The problem

The compiler is unable to detect the OpenCV library and reported it is not found.

> Executing task: C/C++: g++ build active file <

Starting build...
/usr/bin/g++ -g /home/wbadry/Desktop/simplecv/testing.cpp -o /home/wbadry/Desktop/simplecv/testing
/home/wbadry/Desktop/simplecv/testing.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory
    1 | #include <opencv2/core.hpp>
      |          ^~~~~~~~~~~~~~~~~~
compilation terminated.

Build finished with error(s).
The terminal process failed to launch (exit code: -1).

I would appreciate it if there is an extra step to be done as I expected the VSCode extension to compile it with no problem. I believe I miss some flags.

Your help is appreciated. Thanks

image

@wbadry
Copy link
Author

wbadry commented Jun 8, 2021

After googling for a day,

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: g++ build active file",
			"command": "/usr/bin/g++",
			"args": [
				"-g",
				"${file}",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}",
				"`pkg-config", "--cflags", "--libs" ,"opencv4`"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "compiler: /usr/bin/g++"
		}
	]
}

adding "pkg-config", "--cflags", "--libs" ,"opencv4" was the key to allow linker to find the library.

@wbadry wbadry closed this as completed Jun 8, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jul 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant