Ask Your Question

How do I install and use OpenCV on Arch Linux (C++)?

asked 2020-03-23 10:04:57 -0500

I've installed the opencv package from the Arch Repos but when I try to import opencv.hpp, I can only find opencv4/opencv2/opencv.hpp when importing (I am using Visual Studio Code with default settings).

I wrote a little program to test if this was even importing correctly and I get this error when compiling with g++

~/.../camera-software/src >>> g++ main.cpp
In file included from main.cpp:1:
/usr/include/opencv4/opencv2/opencv.hpp:48:10: fatal error: opencv2/opencv_modules.hpp: No such file or directory
   48 | #include "opencv2/opencv_modules.hpp"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

This is the program I wrote:

#include "opencv4/opencv2/opencv.hpp"
#include <iostream>

using namespace std;

int main() {
    printf("Hello World!\n");
    return 0;

I've tried including opencv4/opencv2/opencv_modules.hpp but I get the same error.

edit retag flag offensive close merge delete


include path for opencv4 should be



berak gravatar imageberak ( 2020-03-24 03:16:11 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2020-03-23 17:27:55 -0500

I've never used Visual Studio Code but, for your bare g++ example, you need to provide some arguments to tell it where to find OpenCV.

OpenCV should provide a pkg-config definition to make this easy, so try using locate opencv.pc to locate it (/usr/local/lib/pkgconfig/opencv.pc for my manual build) and then incorporating it into your g++ command like this:

gcc main.cpp `pkg-config --cflags --libs /usr/local/lib/pkgconfig/opencv.pc`

If that works, then you have a similar problem in Visual Studio Code and need to translate the flags echoed by pkg-config --cflags into project settings.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2020-03-23 10:04:57 -0500

Seen: 2,306 times

Last updated: Mar 23 '20