0

I am trying to use g++ (from within Visual Studio 2019, Windows 10) to compile a .cpp file that imports OpenCV header files.

When g++ main.cpp didn't work, I searched here and found people recommending the use of -I to designate the path to the include files.

I have tried -I/path/to/opencv/include as a flag to my g++ command, but still receive the same error.

Would really appreciate it if someone could help me figure out why g++ is not finding the .hpp files...


Minimal code:

#include <iostream> #include <opencv2/core.hpp> int main() { return 0; } 

Error received:

No such file or directory 2 | #include <opencv2/core.hpp> | ^~~~~~~~~~~~~~~~ compilation terminated. 

Edit:

@KonradRudolph advised that I should not have a / between the -I flag and my include path. Replacing it with a space now yields a different error:

In file included from C:/Users/USER/OpenCV/opencv/build/include/opencv2/core.hpp:3307, from main.cpp:2: C:/Users/USER/OpenCV/opencv/build/include/opencv2/core/utility.hpp:717:14: error: 'recursive_mutex' in namespace 'std' does not name a type 717 | typedef std::recursive_mutex Mutex; | ^~~~~~~~~~~~~~~ C:/Users/USER/OpenCV/opencv/build/include/opencv2/core/utility.hpp:63:1: note: 'std::recursive_mutex' is defined in header <mutex>'; did you forget to '#include <mutex>'? 62 | #include <mutex> // std::mutex, std::lock_guard +++ |+#include <mutex> 63 | #endif C:/Users/USER/OpenCV/opencv/build/include/opencv2/core/utility.hpp:718:29: error: 'Mutex' is not a member of 'cv' 718 | typedef std::lock_guard<cv::Mutex> AutoLock; | ^~~~~ C:/Users/USER/OpenCV/opencv/build/include/opencv2/core/utility.hpp:718:29: error: 'Mutex' is not a member of 'cv' C:/Users/USER/OpenCV/opencv/build/include/opencv2/core/utility.hpp:718:34: error: template argument 1 is invalid 718 | typedef std::lock_guard<cv::Mutex> AutoLock; | ^ 
14
  • Interesting that code that includes opencv2/core.hpp makes the compiler complain about opencv/core.hpp... ;) (well, not really, more likely you didn't check that your minimal code actually reproduces the problem) Commented Jun 2, 2021 at 9:38
  • @DanMašek I typed the error by hand because I'm working from a different computer, and I missed the "2". Fixed. And of course I checked if my minimal code produces the error. Commented Jun 2, 2021 at 9:40
  • @avi You can try "opencv2/core.hpp" if it works Commented Jun 2, 2021 at 9:42
  • Hi @nhatnq. I just tried -- receiving the same error (only now with quotation marks instead of carats). Commented Jun 2, 2021 at 9:44
  • 1
    @nhatnq Both \ and / work. I don’t think g++ needs / between -I and the path but I’m not a frequent user of mingw and it shouldn’t hurt. Commented Jun 2, 2021 at 9:56

1 Answer 1

1

The initial error was being caused because I had a / between the -I flag and my include path. Replacing this with a space led to another error...

In an attempt to solve the error involving "mutex" (see edit of original post), I attempted to follow this workaround. Note that step 4 is no longer relevant, as (at the time of this post) the download link leads to a .zip file.

In order to configure the "Threads" option, I downloaded the installer using this guide. This version of the installer gave the option to select "posix" under Threads, as the workaround suggested.

After installation (and adding /bin from the new mingw-w64 folder to PATH), I am now able to successfully compile my example code.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.