OpenCV Installation in Linux/Ubuntu - c++

I am doing this Tutorial
http://docs.opencv.org/doc/tutorials/introduction/linux_install/linux_install.html#linux-installation
But I got confused. I stopped at building the OpenCV from source.
I already created an File called Workspace where I made the cmake_binary_dir (named release). I downloaded the sourcefile (which is in my home directory and named: opencv-2.3.1), and now I want to run this
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
where I use:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/home/markus/opencv-2.3.1
But the Terminal keeps telling me, that this source directory does not exist!?
So what I am doing wrong?

CMAKE_INSTALL_PREFIX defines where to distribute the binary to after its compiled and linked, it defaults to good place (/usr/local/) so avoid defining it
You are leaving out the trailing .. in your cmake command which tells it where to get the source code hence the error message
Here are the typical steps when installing from source code any project which uses cmake
if you see a file :
CMakeLists.txt
in the src directory this indicates it wants you to use cmake
0 cd into dir where your expanded source code lives
1 mkdir build # make a build dir (initially empty)
2 cd build
3 cmake .. # NOTE those little .. which declares relative path to src dir
which says populate current dir (build) with compiled code
and get the source code and configs from parent directory (..)
4 examine the output, if it looks successful go to step 5
if it has errors you may need to install upstream dependent
libraries then try cmake again
5 make -j4 # compile source, -j speeds up by using multicore
6 sudo make install <-- only if above step 4 and 5 are OK
You can do everything cmake related from command line, yet its GUI can be quite handy especially with an unfamiliar project. In above instead of typing :
cmake ..
its GUI version is :
cmake-gui ..
in the GUI its easy to toggle on/off settings like to build examples or not ... the values column on the right is editable ... if you changed settings in the gui at bottom hit button Configure then when its done hit Generate to perform same as the normal cmake .. now return to step 4 above to do the compile

Related

Can someone expand what is meant by 'configure' and 'build' with CMake files

I am downloading this code from GitHub (subdivision-regression), and am getting stuck following the instructions:
To build doosabin_regression:
Run CMake with an out of source build.
Set COMMON_CPP_INCLUDE_DIR to the full path to rstebbing/common/cpp.
Set DOOSABIN_INCLUDE_DIR to the full path to rstebbing/subdivision/cpp/doosabin/include.
Set Ceres_DIR to the directory containing CeresConfig.cmake.
Set GFLAGS_INCLUDE_DIR, GFLAGS_LIBRARY and RAPID_JSON_INCLUDE_DIR. (Add -std=c++11 to CMAKE_CXX_FLAGS if compiling with gcc.)
Configure.
Build.
I have edited the CMakeLists.txt file to put the correct paths in. I then created a new directory called subdivision-regression-bin and ran:
cmake ../subdivision-regression/src
It completes this and displays:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/hert5584/RStebbing/subdivision-regression-bin
However, when I try and run the example code, it cannot find the files listed in CMakeLists.txt (I know they are the right paths as otherwise CMake does not run).
I have tried running:
sudo make install
But get the following error:
make: *** No rule to make target 'install'. Stop.
Any ideas why this isn't working? Have the above steps Configured and Built the files?
The ordered CMake idiom to understand is:
The Configure step
The Generate step (This is often subsumed in the Configure step, and not mentioned explicitly, as in this case.)
The Build step (in which you actually compile/link your code into libraries/executables)
Take a look at this resource for information about the configure and generate stages.
You didn't appear to perform the steps to set CMake cache variables. For these you have to use CMake command line options (-D specifically). So run CMake as something like this instead to set all six variables:
cmake -DCOMMON_CPP_INCLUDE_DIR=/rstebbing/common/cp -DDOOSABIN_INCLUDE_DIR=...[More CMake Cache variables]... ../subdivision-regression/src
For building, try just running make without sudo or install:
make

Why is this happening with cmake

I am working on a c++ project and I am using cmake as the build system, so my workflow here is make changes to code. then,
rm -r build
mkdir build
cd build
cmake -G "Unix Makefiles" ..
make
Now I added glew as a dependency to the project, so whenever I try to run make I get an error saying SDL.h not found(this was working before).After sometime I decided to check CMakeCache.txt.opened it using vim then :wq that's all I did now if I run make, my project is building successfully, I am not sure why this is happening, Can anyone tell me why?
ps: added gif of this event, check it out to get a clear picture
(the code i am working on is linked as well, this exact issue is in this commit "dd4452b45c733e0612bc5f3c632e9d1a08be8072")
link to gif
link to code
variables in cmake are limited to the scope of the directory they are in plus their subdirectories.
This, calling find_module() in the gamelib subdirectory does not find that module for use in the main directory.
The preferred way to propagate include directory dependencies is to add them to the target (in the gamelib directory), like this:
target_include_directories(gamelib BEFORE PRIVATE
$<BUILD_INTERFACE:${SDL2_INCLUDE_DIR}>
$<BUILD_INTERFACE:${GLEW_INCLUDE_DIR}>
)
target_include_directories(gamelib SYSTEM BEFORE PUBLIC
$<BUILD_INTERFACE:${SDL2_INCLUDE_DIR}>
$<BUILD_INTERFACE:${GLEW_INCLUDE_DIR}>
)
then you don't need to even mention them in any executable that uses gamelib.

Build opencv_contrib sample executables on Linux (OpenCV 3.0.0-alpha)

System: VM Ubuntu 14.04 x64
Summary:
I'm building OpenCV 3.0.0-dev with the contrib modules from https://github.com/Itseez/opencv_contrib but I can't get the executables from opencv_contrib.
I was trying to follow this tutorial about the tracking API: http://docs.opencv.org/trunk/modules/tracking/doc/tracking.html
Somewhere it says: To see how API works, try tracker demo: https://github.com/lenlen/opencv/blob/tracking_api/samples/cpp/tracker.cpp
But that's a forked old repository (by user lenlen), not the official repository. In the recent versions (with the new repos) it seems like that file is not in that location anymore but in:
https://github.com/Itseez/opencv_contrib/blob/master/modules/tracking/samples/tracker.cpp
The problem:
I'm being able to build OpenCV without errors, but I can't see how to build that executable file from opencv_contrib, or where is it getting located.
I've tried:
Setting up OPENCV_EXTRA_MODULES_PATH with: cmake -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..
Setting up INSTALL_C_EXAMPLES with: cmake -D INSTALL_C_EXAMPLES=ON ..
Entering in opencv_contrib, creating and entering into a "build" directory and from there executing: "cmake .." ...but that gives errors (no CMakeLists.txt).
Entering in "opencv_contrib/modules/tracking/", creating and entering into a "build" directory and executing "cmake .." (because there's a CMakeLists.txt inside of the tracking directory). But that also gives errors.
Entering in "opencv_contrib/modules/tracking/samples/" and executing "g++ tracker.cpp", but that also gives errors.
Thanks in advance!
I solved it. It was a silly mistake.
I was setting up OPENCV_EXTRA_MODULES_PATH as: cmake -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..
But my working directory was "opencv/build/" (I was in a directory "build" inside of opencv and the "opencv_contrib" was in the same directory as "opencv"), so that variable should have been: cmake -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..

cmake -D <var>:<type>=<value> what does the parameter "-D" mean

I'm trying to use cmake to install opencv. In the opencv instruction page, I find the following example:
cd ~/opencv
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
As I understand it, I should use cmake to generate Makefile in the new directory I created, which in this example should be ~/opencv/release.
But I don't quite understand the last line. In cmake help, I find:
cmake -D <var>:<type>=<value> = create a cmake cache entry
What does it mean? Especially this part: "<var>:<type>=<value>", I don't understand why the example gives "CMAKE_BUILD_TYPE=RELEASE" and"CMAKE_INSTALL_PREFIX=/usr/local .."
Your help is greatly appreciated!
From the CMake Documentation:
-D <var>:<type>=<value>: Create a cmake cache entry.
When cmake is first run in an empty build tree, it creates a CMakeCache.txt file and populates it with customizable settings for the project. This option may be used to specify a setting that takes priority over the project's default value. The option may be repeated for as many cache entries as desired.
The :<type> could be read as optional.
Maybe you can try:
cd ~/opencv
mkdir release
cd release
cmake -D'CMAKE_BUILD_TYPE=RELEASE' -D'CMAKE_INSTALL_PREFIX=/usr/local'
Just use ' ' surround the parameters and do not leave any blank between -D and ' and it can work.
I encountered some problems when I configure OpenCV with -D parameter.
And I think -D option just change some default parameters for compiling and installing the pkg.
Just as you inferred, CMAKE_BUILD_TYPE=RELEASE means you want to build a "Release" version of the opencv package, and CMAKE_INSTALL_PREFIX=/usr/local means you want to specify the install path of it while using make install command.
You might wonder why it's called -D. The D stands for define/definition. Using -DCMAKE_BUILD_TYPE=RELEASE defines CMAKE_BUILD_TYPE with value RELEASE in the CMake cache.
From the docs:
Create or update a CMake CACHE entry.
The main use of this to configure your build without changing the CMakeLists.txt file, for example, to switch between debug and release builds without editing files.
The reason it's not a variable, but a cache entry is for your convenience. Rerunning the command uses your previously set value.

How do I tell cmake where to output its build data?

How do I tell cmake where it should output its build data?
Let's say I have a dir with the source code called src/,
and then since cmake outputs a lot of files I would like him to put all of that in
a dir called build/.
BUT I would like him to put the generated Makefile in the project root,
so I don't have to go into the build dir to build the application.
Is this possible with cmake?
I have managed to get the cmake out put if I fun cmake in the build dir like this:
cd build/
cmake ../src/
make
./hello
But it would be nice to stay in the project root and type something like this
cmake
make
./hello
I guess that I need to put a CMakeList.txt in the project root with some magic commands telling him where he could put the object files and where he can find the source code.
Thanks
Update:
Since my question is a little bit vague.
After I have run the cmake commands this is how I would like my tree to look like:
src/CMakeLists.txt
src/hello.c
src/hello.h
build/CMakeCache.txt
build/CMakeFiles/
build/cmake_install.cmake
CMakeLists.txt
Makefile
So the question is how should the CMakeLists.txt look like in this setup.
CMakeLists.txt
src/CMakeLists.txt
But maybe that is not possible?
BUT I would like him to put the generated Makefile in the project root, so I don't have to go into the build
dir to build the application.
cmake not designed for that, as I know,
BUT you can stay in the project root and type:
make -C build
./hello
with custom build rules or set_target_properties,
you can force cmake to put result executable to
sources directory or you can use
./build/hello
Type "cd build && cmake .." you need only once,
after that make will automaticaly start cmake, if something
changed.
cmake wants you to have a fresh build directory.
Okay, i get what you want. I think, you can achieve this with some machinery in CMakeLists.txt. Of course, it's not option if you are not project developer.
In root CMakeLists.txt you can add file(WRITE ...) command, which would write Makefile into ${CMAKE_SOURCE_DIR}. This Makefile would contain these commands for every target:
<target>:
cd ${CMAKE_BUILD_DIR} && ${CMAKE_MAKE_PROGRAM} <target>
So, now you can run make from source dir and it will build your project in build dir.
Simply use
cmake .
make
in your src directory. The (.) dot on unix systems addresses the current directory. Keep in mind doing so is actually not recommended since there will be a lot of build files in your src directory you'll have to clean up afterwards or at release time.