Ask Your Question

installed Opencv 4 from source, C++ headers not linked correctly

asked 2019-08-16 17:14:21 -0500

mjxlyons gravatar image

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

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:

#include <opencv4/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:

In file included from get_version.cpp:1:0:
/usr/local/include/opencv4/opencv2/core.hpp:52:10: fatal error: opencv2/core/cvdef.h: No such file or directory
 #include "opencv2/core/cvdef.h"
compilation terminated.

But, if I navigate to /usr/local/include/opencv4/opencv2/core/, I can see the file, cvdef.h, 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'm used to Python, so I'm I'm not used to having to deal with linking all these header files, so sorry if it's something obvious.

edit retag flag offensive close merge delete


how do you try to compile the c++code (please add that to your question, thank you)

berak gravatar imageberak ( 2019-08-17 00:29:43 -0500 )edit

Using a virtual environment for different setups can solve a lot of problems :D Or docker containers. Its an easy way to avoid conflicts between versions without breaking up the host system.

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-19 09:01:27 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-11-10 14:30:33 -0500

You can use CMAKE method for compiling the code. In CMakeLists.txt, under find_package command, you can specify the exact library version you want to use for compilation. For example.

cmake_minimum_required(VERSION 2.8)

project( DisplayImage )

find_package( OpenCV 4.1.2 REQUIRED )

include_directories( ${OpenCV_INCLUDE_DIRS} )

add_executable( DisplayImage main.cpp )

target_link_libraries( DisplayImage ${OpenCV_LIBS} )

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-08-16 17:14:21 -0500

Seen: 2,564 times

Last updated: Aug 16 '19