cmake is not working in opencv c++ project

15,942

Solution 1

You're probably gonna have to add the following piece of code in the CMakeLists.txt before the line FIND_PACKAGE(OpenCV Required) (in my case):

SET("OpenCV_DIR" "PATH/TO/CMake/build")

In my installation it solved the problem since the mentioned files are located in the build folder.

Solution 2

So, this is what fixed this for me.

Firstly, if in doubt - always remember to clear out your CMake "cache" before trying adding some-other path/value/mod. Seems a bit weird I know, but the legacy of previous attempts to fix things may be obsuring the true problem. Simplest way to do this is to nuke your out-of-source "build" directory from orbit (only way to be sure) and try again...

Secondly, and more fundamentally, what is your target compiler & architecture here and what is provided by the version of OpenCV you've downloaded? For example, as of the time of writing the OpenCV 3.1 pre-built installer I downloaded provides ~\build\x64\vc12 & vc14 - which were of course for building against VisualStudio 2012 & 2014.

I was using Mingw and targeting x86 - so I downloaded the source (via github release as a *.zip file) and did these steps from within the \opencv folder:

mkdir build
cd build
cmake -G  "MinGW Makefiles" ..
cmake --build .
cmake --build . --target install

You can of course control the build by various switches and also dictate where an install should go.

Doing the "install" step - by which by default is to copy the final libraries etc. into a folder called "install" - is key as it correctly puts all the resulting libs and files in the right place and includes the magical OpenCVConfig.cmake file at the top.

Thus, I could finally point my project CMakeLists.txt file to resolve OpenCV with:

set("OpenCV_DIR" "C:/Code/opencv/build/install")

find_package( OpenCV REQUIRED )

And, making sure the resulting executable was aware of the *.dll's with:

PATH=%PATH%;C:\Code\opencv\build\install\x86\mingw\bin

Solution 3

On Linux I did the following:

$ git clone https://github.com/royshil/FoodcamClassifier.git
$ cd FoodcamClassifier/
$ mkdir build
$ cd build/
$ cmake ../

-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
/usr/local/share/OpenCV
-- Configuring done
-- Generating done
-- Build files have been written to: /home/cog/workspace/opencv/FoodcamClassifier/build

$ make

Scanning dependencies of target build-vocabulary
[  8%] Building CXX object CMakeFiles/build-vocabulary.dir/build_vocabolary.cpp.o
Linking CXX executable build-vocabulary
[  8%] Built target build-vocabulary
Scanning dependencies of target foodcam-predict
[ 16%] Building CXX object CMakeFiles/foodcam-predict.dir/foodcam_predict.cpp.o
[ 25%] Building CXX object CMakeFiles/foodcam-predict.dir/predict_common.cpp.o
Linking CXX executable foodcam-predict
[ 25%] Built target foodcam-predict
Scanning dependencies of target kmeans-trainer
[ 33%] Building CXX object CMakeFiles/kmeans-trainer.dir/kmeans_trainer.cpp.o
Linking CXX executable kmeans-trainer
[ 33%] Built target kmeans-trainer
Scanning dependencies of target make-test-background
[ 41%] Building CXX object CMakeFiles/make-test-background.dir/make_test_background_image.cpp.o
Linking CXX executable make-test-background
[ 41%] Built target make-test-background
Scanning dependencies of target manual-classifier
[ 50%] Building CXX object CMakeFiles/manual-classifier.dir/manual_classifier.cpp.o
Linking CXX executable manual-classifier
[ 50%] Built target manual-classifier
Scanning dependencies of target test-classifiers
[ 58%] Building CXX object CMakeFiles/test-classifiers.dir/test_classifiers.cpp.o
[ 66%] Building CXX object CMakeFiles/test-classifiers.dir/predict_common.cpp.o
Linking CXX executable test-classifiers
[ 66%] Built target test-classifiers
Scanning dependencies of target train-SVM-alone
[ 75%] Building CXX object CMakeFiles/train-SVM-alone.dir/train_SVM_alone.cpp.o
[ 83%] Building CXX object CMakeFiles/train-SVM-alone.dir/training_common.cpp.o
Linking CXX executable train-SVM-alone
[ 83%] Built target train-SVM-alone
Scanning dependencies of target train-bovw
[ 91%] Building CXX object CMakeFiles/train-bovw.dir/train_bovw.cpp.o
[100%] Building CXX object CMakeFiles/train-bovw.dir/training_common.cpp.o
Linking CXX executable train-bovw
[100%] Built target train-bovw
Share:
15,942
SafeY
Author by

SafeY

I'm a fresh graduated :) Information Technology Engineer , Artificial intelligence specialist .

Updated on June 11, 2022

Comments

  • SafeY
    SafeY almost 2 years

    I need your help ! I have this C++ code in this link

    [link] https://github.com/royshil/FoodcamClassifier

    and I've been trying since two days to compaile it , and I've failed they say that I have to use cmake , I've tried the "GUI version and it gave me errors realted to the cmake itself . so I took the cpp and header files and made a new project but I have now 100 errors related to the opencv library and I swear to god I'm sure of the include folders and the libs of it in my project ! don't know what's the matter with it !

    any idea ?

    Here's the errors :

    'CMake Error: Unable to open cache file for save. C:/Program Files/CMake 2.8/bin/CMakeCache.txt
    CMake Error at CMakeLists.txt:4 (FIND_PACKAGE):
      Could not find module FindOpenCV.cmake or a configuration file for package
      OpenCV.
    
      Adjust CMAKE_MODULE_PATH to find FindOpenCV.cmake or set OpenCV_DIR to the
      directory containing a CMake configuration file for OpenCV.  The file will
      have one of the following names:
    
        OpenCVConfig.cmake
        opencv-config.cmake
    
    
    
    OpenCV_DIR-NOTFOUND
    Configuring incomplete, errors occurred!
    CMake Error: Unable to open cache file for save. C:/Program Files/CMake 2.8/bin/CMakeCache.txt
    CMake Error: : System Error: Permission denied
    CMake Error: : System Error: Permission denied '
    
  • SafeY
    SafeY about 12 years
    actually I don't have Linux on my device and I tried to use cmake with windows as I've mentioned above , but I failed :( would you please upload the result compiled code for me ? I'll be so thankful !!
  • karlphillip
    karlphillip about 12 years
    It won't work. Applications compiled on Linux will only run on Linux.
  • karlphillip
    karlphillip about 12 years
    You are not willing to help yourself, I can't do miracles :) I already told you before, if you don't paste the errors you are observing it's going to be very difficult to solve your problem. My crystal ball is broken, I'm sorry.