71

I'm new to cmake, and I'm only using it to install opencv on my ubuntu linux. Here's the command I ran: cmake -DCMAKE_BUILD_TYPE=Release DCMAKE_INSTALL_PREFIX=/home/jinha/OCV/source

Then it returns the error:

FATAL: In-source builds are not allowed. You should create separate directory for build files.

My current directory, ~/OCV/build/opencv, does contain the CMakefiles.txt file, so that's not the problem. I tried to change the directory in my command, but they all raise the same error. I saw the other answers on this issue, so I erased CMakeFiles directory and CMakeCache.txt file every time before I ran the command, but none of them worked.
Thanks.

WannabeArchitect
  • 1,058
  • 2
  • 11
  • 22

2 Answers2

212

It wants you to create a separate build directory (anywhere), and run cmake there. For example:

mkdir my_build_dir
cd my_build_dir
rm ../CMakeCache.txt
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/home/jinha/OCV/source

Note the .. in this example telling cmake where to look for the source.

In case you didn't remove CMakeCache.txt before building again, it will still show this error. So, please remember to delete CMakeCache.txt first before running cmake.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Wow, thanks a lot! This was my first question. Thanks a lot! Totally worked. – WannabeArchitect Aug 05 '17 at 08:23
  • CMAKE is just being smart and preserving your sources as "*pristine*" by requiring you build in another directory to prevent dirtying your source with object and other build files... – David C. Rankin Aug 05 '17 at 08:31
  • @정진하: You're welcome. If this solved your problem you can "accept" the answer by clicking the checkmark on the left. – John Zwinck Aug 05 '17 at 12:53
  • 67
    Thanks for noting to remove `CMakeCache.txt`. – Johann Burgess Jan 05 '18 at 05:00
  • The source directory "/Users/user/build" does not appear to contain CMakeLists.txt. – Kairat May 28 '18 at 05:53
  • This solution did not work for me and I am not sure what CMAKE_INSTALL_PREFIX should point to. Is that the full path there the sources are located? – Shailen Jan 08 '19 at 15:57
  • Everybody comes here after trying to install opencv, then following *its instructions* and it still says that pesky error message, until you delete that file. Thanks for the tip and saving me time... :) – rogerdpack Jan 09 '19 at 14:44
  • Thanks again for noting to remove `CMakeCache.txt`; installation does not work otherwise. – Bilal Siddiqui Apr 27 '19 at 21:40
  • Why can't cmake tell me about the cache file? – Nick Oct 26 '19 at 16:32
  • @Nick: CMake is an open source project; if you want to improve its error messages, you can. https://gitlab.kitware.com/cmake/cmake – John Zwinck Oct 27 '19 at 00:06
  • Also note that if you ever used `cmake` in the source directory, it will fail with the same message again and again. You will need to delete the generated build or reclone the sources. – bem22 Nov 15 '19 at 10:50
  • Remember to do "make", "make install", and "make clean" after this. – Punnerud Mar 04 '21 at 09:21
  • So, if I change one file in a massive project and I just want to recompile it and its dependecies, is this the way to do it? Or will this cause the whole thing to have to recompile? – Buttle Butkus Feb 08 '22 at 05:37
14

After you have success downloaded and unzipped OpenCV sources from sources you need create simple command-file install.sh. For example, your working dir will be /home/user/myopencv

So /home/user/myopencv/install.sh will be contain next code:

#!/bin/bash

rm CMakeCache.txt
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local 
make
make install
make clean

Next

chmod 777 install.sh
./install.sh

And after the all you will get those executable files:

root@cartman:/usr/local/bin# ls -las | grep opencv
 32 -rwxr-xr-x  1 root root   29888 апр 20 18:10 opencv_annotation
244 -rwxr-xr-x  1 root root  247608 апр 20 18:10 opencv_createsamples
244 -rwxr-xr-x  1 root root  247504 апр 20 18:10 opencv_haartraining
 20 -rwxr-xr-x  1 root root   18600 апр 20 18:10 opencv_performance
288 -rwxr-xr-x  1 root root  294592 апр 20 18:10 opencv_traincascade
 16 -rwxr-xr-x  1 root root   14288 апр 20 18:10 opencv_version
 60 -rwxr-xr-x  1 root root   61040 апр 20 18:10 opencv_visualisation

Enjoy!

Orlov Const
  • 332
  • 3
  • 10