cmake error 'the source does not appear to contain CMakeLists.txt'

Question:

I’m installing opencv in ubuntu 16.04. After installing the necessary prerequisites I used the following command:-

kvs@Hunter:~/opencv_contrib$ mkdir build
kvs@Hunter:~/opencv_contrib$ cd build
kvs@Hunter:~/opencv_contrib/build$ 
kvs@Hunter:~/opencv_contrib/build$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX+/usr/local -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D BUILD_EXAMPLES=ON ..

but it produced an error:-

CMake Error: The source directory "/home/kvs/opencv_contrib" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.

I used the command provided in the folder ‘module’ documentation. How do I solve it?
I tried the answers here at stack-overflow and a few other question but still can’t figure it out.

Project Git repository here.

Asked By: KVS

||

Answers:

You should do mkdir build and cd build while inside opencv folder, not the opencv-contrib folder. The CMakeLists.txt is there.

Answered By: Quang Hoang

Since you add .. after cmake, it will jump up and up (just like cd ..) in the directory. But if you want to run cmake under the same folder with CMakeLists.txt, please use . instead of ...

Answered By: K. Symbol

This reply may be late but it may help users having a similar problem.
The opencv-contrib contains extra modules but the build procedure has to be done from core OpenCV modules.

Follow the below steps (assuming you are building it using CMake GUI)

  1. Download OpenCV (from here) and unzip it somewhere on your computer. Create a build folder inside it

  2. Download extra modules from OpenCV (from here). Ensure you download the same version.

  3. Unzip the folder.

  4. Open CMake

  5. Click Browse Source and navigate to your OpenCV folder.

  6. Click Browse Build and navigate to your build folder.

  7. Click the configure button. You will be asked how you would like to generate the files. Choose Unix-Makefile from the drop-down menu and Click OK. CMake will perform some tests and return a set of red boxes that appear in the CMake Window.

  8. Search for "OPENCV_EXTRA_MODULES_PATH" and provide the path to the modules folder (e.g. /Users/purushottam_d/Programs/OpenCV3_4_5_contrib/modules)

  9. Click Configure again, then Click Generate.

  10. Go to the build folder

# cd build
# make
# sudo make install
  1. This will install the OpenCV libraries on your computer.
Answered By: puru

An easier way to build OpenCV from source in a step by step fashion as given in this reference: Installing OpenCV from the Source
is to,

step 1: install dependencies,

 sudo apt install build-essential cmake git pkg-config libgtk- 
   3-dev libavcodec-dev libavformat-dev libswscale-dev 
   libv4l-dev libxvidcore-dev libx264-dev libjpeg-dev 
   libpng-dev libtiff-dev gfortran openexr libatlas-base- 
   dev python3-dev python3-numpy libtbb2 libtbb-dev 
   libdc1394-22-dev

Step 2: create a directory opencv_build and Clone the necessary repositories as shown below,

mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

step 3: cd into opencv directory, inside create another directory called build and cd into it,

cd ~/opencv_build/opencv
mkdir build && cd build

step 4: evoke Cmake to build OpenCV,

cmake -D CMAKE_BUILD_TYPE=RELEASE 
-D CMAKE_INSTALL_PREFIX=/usr/local 
-D INSTALL_C_EXAMPLES=ON 
-D INSTALL_PYTHON_EXAMPLES=ON 
-D OPENCV_GENERATE_PKGCONFIG=ON  
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules 
-D BUILD_EXAMPLES=ON ..

If step 4 completes successfully you should see the following line at the end of the terminal, the build has been written to the directory created in step 3, along with the following lines above this line,

configuration done
generating done

step 5: To start the compilation process where -j
is a flag for the number of the processor inside your machine, for example -j6 means we have 6 processors available. to verify the number of processors type nproc on the terminal then use this number after -j. To start this process, we use the following command:

make -j6 

step 6: install OpenCV, We use,

sudo make install

then check the version Of OpenCV to verify the installation:

pkg-config --modversion opencv4
Answered By: Tirelo SHIKWAMABANA

I had a similar problem with another package and neither operating from a clean directory, to build from out, nor copy/paste the CMakeLists.txt file from the source to the clean directory worked.
I simply solved installing by conda

Answered By: Lucas

file structure :
example-app/

                CMakeLists.txt

                example-app.cpp

commands

                mkdir build

                cd build

                cmake .. -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch..

                cmake --build . --config Release

execution of the command along with the code

file structure

Answered By: rajkamal
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.