Can't link opencv libraries via Cmake in Linux

asked 2018-03-25 11:39:50 -0500

nasserahmed96 gravatar image

I used to have OpenCV3 from the Arch Linux pckage manager(pacman) and it worked fine but when I removed it and install OpenCV 3.4.1 and OpenCV-Contrib from source code nothing worked even the old project when I tryied to recompile them. Here is the CMakeLists.txt file

cmake_minimum_required(VERSION 2.8)
cmake_policy(SET CMP0012 NEW)
find_package(OpenCV 3.4.0 REQUIRED)
message("OpenCV Version ${OpenCV_Version}")
set(SRC main.cpp)
add_executable(${PROJECT_NAME} ${SRC})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})

Here is the main.cpp file which contains

#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/face.hpp"
#include "opencv2/imgproc.hpp"

#include <stdio.h> 
#include <fstream>
#include <sstream>

using namespace cv;
using namespace cv::face;
using namespace std;

//Normalize image

static Mat norm_0_255(Mat _src){
     Mat src (_src);
     //The returned normalized image
    Mat dst;
    switch (src.channels()){
         case 1:
             normalize(src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
         case 3:
             normalize(src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
     return dst;

 //Read CSV which containts the paths to images
 static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';'){
     ifstream file(filename.c_str(), ifstream::in);
         string error_message = "No valid inout file was given\n";
         CV_Error(Error::StsBadArg, error_message);
     string line, path, classlabel;
     while(getline(file, line)){
         stringstream liness(line);
         getline(liness, path, separator);
         getline(liness, classlabel);
         if(!path.empty() && !path.empty()){
             images.push_back(imread(path, 0));

 int main(int argc,const char* argv[]){
     return 0;

Part of the Error The remainder errors are the same. Note:This code is from samples , and ofcourse this is part of it, other projects wouldn't work too although they are worked before I reinstalled it.

edit retag flag offensive close merge delete


find_package(OpenCV 3.4.0 REQUIRED) <-- 3.4.1, right ?

berak gravatar imageberak ( 2018-03-25 12:33:11 -0500 )edit

Actually, it says that it found it and also gave me the error, tht error in Linking phase I have tryied to change it to 3.4.1 and also it doesn't work

nasserahmed96 gravatar imagenasserahmed96 ( 2018-03-25 12:36:45 -0500 )edit