1

I jusst installed Opencv 4.1.1 from source, with python 3 bindings. My Python installation seems to work correctly, because if I run:

import cv2
cv2.__version__

I get '4.1.1'

However, I also want to use some C++ code. I had a previous installation of 3.4.3 that I built from source, and unfortunately I had deleted the build folder before uninstalling it. So, when I run this C++ code, which just finds the version number Macros and prints them:

#include <opencv2/core.hpp>

using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
    //print opencv version
    printf("opencv version: %d.%d.%d\n",CV_VERSION_MAJOR,CV_VERSION_MINOR,CV_VERSION_REVISION);

    return 0;
}

When i compile with this command :

g++ -o get_version get_version.cpp -I/usr/local/include/opencv4/opencv2 -Lusr/local/lib -lopencv_core

It returned 3.4.3.

So, I wanted to fix this, so I found the old header files, and deleted them all (they were located in /usr/local/include/opencv2). But now when I try to compile my code, it gives me the error:

get_version.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory
#include <opencv2/core.hpp>
         ^~~~~~~~~~~~~~~~~~
compilation terminated.

But, if I navigate to /usr/local/include/opencv4/opencv2/, I can see the file, core.hpp, so I don't know why the compiler can't find it.

So, why are my headers not linking correctly? And how can I fix this. I thought the command -I/usr/local/include/opencv4/opencv2 would allow it to find it. I have also already run

sudo ldconfig

Although I'm not sure what that command exactly does. Also, for those that don't know, opencv 4 doesn't automatically include a pkg-config file, so that is not a solution to this problem.

3
  • Have you tried -I/usr/local/include/opencv4? You should not install and delete files on share header locations. Use your OS' package manager to install the opencv version you want. What OS are you on?
    – hanxue
    Aug 18 '19 at 5:48
  • Mods, I think this question is more appropriately asked in StackOverflow. Would you consider moving it over?
    – hanxue
    Aug 18 '19 at 5:48
  • @hanxue, Thanks. And I would have liked to use my package manager to delete the old header files, but I made a mistake, and I had deleted the user from my system who had done the 3.4.3 install, so I didn't have the old "build" files anymore, so I couldn't uninstall the "correct" way. I'm running Ubuntu 18.04, I'm the only person who uses this computer, but I try to use "users" to isolate larger projects I'm working on.
    – Matt Lyons
    Aug 19 '19 at 20:30
3

Fixed it, changed my "include" argument was wrong. When I compile with the command below:

g++ -o get_version get_version.cpp -I/usr/local/include/opencv4 -L/usr/local/lib -lopencv_core

It works. I had an extra "opencv2" at the end of the argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.