How to set opencv release mode with cmake - opencv

It seems very easy when compiling opencv library for the release mode:
cmake -D CMAKE_BUILD_TYPE=RELEASE ../
or cmake DCMAKE_BUILD_TYPE:string=Release ../
However, the created project is still in Debug mode, and I was wondering how I could do in order to make the project in a release mode. Thanks.

The configuration name is case-sensitive, and the CMake command line arg needs a - at the front. The arg type (i.e. string in your example) is optional, so the command should be:
cmake -DCMAKE_BUILD_TYPE=Release ..

if(DEFINED CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING “Choose the type of building”)
else()
set(CMAKE_BUILD_TYPE Debug CACHE STRING “Choose the type of build”)
endif()
set(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE STRING “” FORCE)
cmake -DCMAKE_BUILD_TYPE=Release ..

Related

mkDerivation use cmake in buildPhase

I'm trying to create a derivation using nix files, and I'm a little stuck. A package I'm trying to install has a sh file in its repo to build it, and this sh file is running CMake with some arguments.
More specifically, this package is vcpkg.
Here's my vcpkg.nix file:
{ gcc11Stdenv, fetchFromGitHub, ninja, cmake, bash }:
gcc11Stdenv.mkDerivation {
name = "vcpkg-2021.05.12";
src = fetchFromGitHub {
owner = "microsoft";
repo = "vcpkg";
rev = "2021.05.12";
sha256 = "0290fp9nvmbqpgr33rnchn5ngsq4fdll2psvh0bqf0324w2qpsjw";
};
buildPhase = ''
./bootstrap-vcpkg.sh -useSystemBinaries
'';
}
When running it with nix-shell -p 'with (import <nixpkgs> {}); callPackage ./vcpkg.nix {}', I get this error:
configuring
no configure script, doing nothing
building
Could not find cmake. Please install it (and other dependencies) with:
sudo apt-get install cmake ninja-build
error: builder for '/nix/store/riq6vjdhv4z3xvzp8g597xjgwf2rvm03-vcpkg-2021.05.12.drv' failed with exit code 1;
last 9 log lines:
> unpacking sources
> unpacking source archive /nix/store/ycfd6vbgh3s1vy11hfb17b8x33rqj7aw-source
> source root is source
> patching sources
> configuring
> no configure script, doing nothing
> building
> Could not find cmake. Please install it (and other dependencies) with:
> sudo apt-get install cmake ninja-build
For full logs, run 'nix log /nix/store/riq6vjdhv4z3xvzp8g597xjgwf2rvm03-vcpkg-2021.05.12.drv'.
Then, I thought of making cmake and ninja available to buildPhase so the script can use those binaries by adding buildInputs = [cmake ninja];, but I then get this error:
configuring
fixing cmake files...
cmake flags: -DCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY=OFF -DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_SKIP_BUILD_RPATH=ON -DBUILD_TESTING=OFF -DCMAKE_INSTALL_LOCALEDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/share/locale -DCMAKE_INSTALL_LIBEXECDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/libexec -DCMAKE_INSTALL_LIBDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/lib -DCMAKE_INSTALL_DOCDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/share/doc/vcpkg -DCMAKE_INSTALL_INFODIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/share/info -DCMAKE_INSTALL_MANDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/share/man -DCMAKE_INSTALL_OLDINCLUDEDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/include -DCMAKE_INSTALL_INCLUDEDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/include -DCMAKE_INSTALL_SBINDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/sbin -DCMAKE_INSTALL_BINDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/bin -DCMAKE_INSTALL_NAME_DIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/lib -DCMAKE_POLICY_DEFAULT_CMP0025=NEW -DCMAKE_OSX_SYSROOT= -DCMAKE_FIND_FRAMEWORK=LAST -DCMAKE_STRIP=/nix/store/854jyvxrvpdpbfn2zaba1v2qgqkxipyh-cctools-binutils-darwin-949.0.1/bin/strip -DCMAKE_RANLIB=/nix/store/854jyvxrvpdpbfn2zaba1v2qgqkxipyh-cctools-binutils-darwin-949.0.1/bin/ranlib -DCMAKE_AR=/nix/store/854jyvxrvpdpbfn2zaba1v2qgqkxipyh-cctools-binutils-darwin-949.0.1/bin/ar -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_INSTALL_PREFIX=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12
CMake Error: The source directory "/tmp/nix-build-vcpkg-2021.05.12.drv-0/source" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
error: builder for '/nix/store/76djky7f3xy6ym6v3qlmy941z0bjb8xw-vcpkg-2021.05.12.drv' failed with exit code 1;
last 9 log lines:
> unpacking sources
> unpacking source archive /nix/store/ycfd6vbgh3s1vy11hfb17b8x33rqj7aw-source
> source root is source
> patching sources
> configuring
> fixing cmake files...
> cmake flags: -DCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY=OFF -DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_SKIP_BUILD_RPATH=ON -DBUILD_TESTING=OFF -DCMAKE_INSTALL_LOCALEDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/share/locale -DCMAKE_INSTALL_LIBEXECDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/libexec -DCMAKE_INSTALL_LIBDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/lib -DCMAKE_INSTALL_DOCDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/share/doc/vcpkg -DCMAKE_INSTALL_INFODIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/share/info -DCMAKE_INSTALL_MANDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/share/man -DCMAKE_INSTALL_OLDINCLUDEDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/include -DCMAKE_INSTALL_INCLUDEDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/include -DCMAKE_INSTALL_SBINDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/sbin -DCMAKE_INSTALL_BINDIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/bin -DCMAKE_INSTALL_NAME_DIR=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12/lib -DCMAKE_POLICY_DEFAULT_CMP0025=NEW -DCMAKE_OSX_SYSROOT= -DCMAKE_FIND_FRAMEWORK=LAST -DCMAKE_STRIP=/nix/store/854jyvxrvpdpbfn2zaba1v2qgqkxipyh-cctools-binutils-darwin-949.0.1/bin/strip -DCMAKE_RANLIB=/nix/store/854jyvxrvpdpbfn2zaba1v2qgqkxipyh-cctools-binutils-darwin-949.0.1/bin/ranlib -DCMAKE_AR=/nix/store/854jyvxrvpdpbfn2zaba1v2qgqkxipyh-cctools-binutils-darwin-949.0.1/bin/ar -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_INSTALL_PREFIX=/nix/store/mp38jl4fkv0gqnqhz7a3agx4flwda59n-vcpkg-2021.05.12
> CMake Error: The source directory "/tmp/nix-build-vcpkg-2021.05.12.drv-0/source" does not appear to contain CMakeLists.txt.
> Specify --help for usage, or press the help button on the CMake GUI.
For full logs, run 'nix log /nix/store/76djky7f3xy6ym6v3qlmy941z0bjb8xw-vcpkg-2021.05.12.drv'.
It seems that adding cmake to buildInputs makes nix try to configure the project using cmake, but this is not what I'm trying to do since vcpkg don't simply have a CMakeLists.txt file in its repo.
Adding inherit cmake ninja; didn't seem to help.
You can prevent cmake from changing the configure phase by setting dontUseCmakeConfigure=true in your derivation.
From the nixpkgs manual:
6.7.29. cmake
Overrides the default configure phase to run the CMake command. By default, we use the Make generator of CMake. In addition, dependencies are added automatically to CMAKE_PREFIX_PATH so that packages are correctly detected by CMake. Some additional flags are passed in to give similar behavior to configure-based packages. You can disable this hook’s behavior by setting configurePhase to a custom value, or by setting dontUseCmakeConfigure. cmakeFlags controls flags passed only to CMake. By default, parallel building is enabled as CMake supports parallel building almost everywhere. When Ninja is also in use, CMake will detect that and use the ninja generator.

How to build opencv samples

I am new to cmake though not to make. This question is different from Could not build OpenCV Android sample project since that other question is about a single project and this one is looking at the overall CMakeLists.txt.
Speaking of which: consider the CMakeLists.txt in ${OPENCVDIR}/samples :
I followed basic process for cmake:
cd "${OPENCVDIR}/samples"
mkdir build
cd build
cmake ..
But at the last step I have:
$ cmake ..
CMake Error at CMakeLists.txt:72 (find_package):
Could not find a package configuration file provided by "OpenCV" with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
See also "/git/opencv/samples/CMakeFiles/CMakeOutput.log".
Line 72 has this: find_package(OpenCV REQUIRED PATHS "..")
I looked at the error log and it was not informative.
Compilation of the CXX compiler identification source "CMakeCXXCompilerId.cpp" produced "a.out"
The CXX compiler identification is GNU, found in "/git/opencv/samples/CMakeFiles/3.13.4/CompilerIdCXX/a.out"
Determining if the C compiler works passed with the following output:
Change Dir: /git/opencv/samples/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_26f76/fast"
/usr/bin/make -f CMakeFiles/cmTC_26f76.dir/build.make CMakeFiles/cmTC_26f76.dir/build
make[1]: Entering directory '/git/opencv/samples/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_26f76.dir/testCCompiler.c.o
/usr/bin/cc -o CMakeFiles/cmTC_26f76.dir/testCCompiler.c.o -c /git/opencv/samples/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTC_26f76
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_26f76.dir/link.txt --verbose=1
/usr/bin/cc -rdynamic CMakeFiles/cmTC_26f76.dir/testCCompiler.c.o -o cmTC_26f76
make[1]: Leaving directory '/git/opencv/samples/CMakeFiles/CMakeTmp'
Detecting C compiler ABI info compiled with the following output:
"/git/opencv/samples/CMakeFiles/CMakeOutput.log" 706 lines, 48095 characters
Feature record: CXX_FEATURE:0cxx_defaulted_move_initializers
Feature record: CXX_FEATURE:0cxx_delegating_constructors
Feature record: CXX_FEATURE:0cxx_deleted_functions
Feature record: CXX_FEATURE:0cxx_digit_separators
Feature record: CXX_FEATURE:0cxx_enum_forward_declarations
Feature record: CXX_FEATURE:0cxx_explicit_conversions
Feature record: CXX_FEATURE:0cxx_extended_friend_declar
etc ..
What is the correct way to build these examples - hopefully using the CMakeLists.txt already provided?
It seems the installation directory of OpenCV couldn't be found by cmake. Try to provide the value through the argument:
cmake -DCMAKE_PREFIX_PATH=/home/someone/src/opencv/install ..
Ff it works, you could define this in the top-level CMakeLitst.txt:
list(APPEND CMAKE_PREFIX_PATH /home/someone/src/opencv/install)
This should provide CMake the place where it should look to.
$ opencv_version
3.4.16
$ cd OpenCV/samples/
$ cmake -B build
$ cmake --build build
JPEG display
$ build/cpp/example_cpp_image data/lena.jpg
USB camera capture
$ build/cpp/example_cpp_videocapture_basic
$ build/cpp/example_cpp_videocapture_camera
Recognition by AI
$ build/tapi/example_tapi_hog

where is clang executables after building LLVM?

Environment: Ubuntu 18.04 bionic.
after git clone llvm-project from https://github.com/llvm/llvm-project.
I generated build credentials using Cmake commandline like this:
cmake -DLLVM_TARGETS_TO_BUILD=X86 \
-DLLVM_TARGET_ARCH=X86 \
-DCMAKE_BUILD_TYPE="Release" \
-DLLVM_BUILD_EXAMPLES=1 \
-DCLANG_BUILD_EXAMPLES=1 \
-G "Unix Makefiles" \
../llvm/
after makefile was generated, I then use make to build the project make -j8. somehow after build completion, clang and clang++ etc. are nowhere to be found in /build/bin/.
To my knowledge, if I didn't specify LLVM_ENABLE_PROJECTs, it will build LLVM and clang both by default. why are clang executables missing?
You need to explicitly specify LLVM_ENABLE_PROJECT and put the source code folder of clang in the same level of llvm source folder. The folder name must match the value of LLVM_ENABLE_PROJECT

Compiling opencv samples: unknown cmake command ocv_check_dependencies

I am trying to build the OpenCV samples which come with the source package and I get the following:
CMake Error at CMakeLists.txt:10 (ocv_check_dependencies):
Unknown CMake command "ocv_check_dependencies".
I did install OpenCV using
cmake .
make
sudo make install
and I got a tutorial snippet working (thus I suppose it is installed correctly as a library). However, compiling the samples does not work.
I guess I have to somehow configure CMake to have “ocv_check_dependencies” - but how? I am lost!
Actually for OpenCV 2.4.4 beta the root CMakeList.txt file says:
OCV_OPTION(BUILD_EXAMPLES "Build all examples"
-DBUILD_EXAMPLES=ON worked just fine for me.
I got it.
In order to build the samples one has to change the default configuration for cmake by providing it via -D. What I did wrong was that I tried to execute cmake from within the samples directory.
The proper way to build the samples is invoking cmake like so (from within the root directory of the unpacked archive):
cmake -DBUILD_SAMPLES .
which will turn samples ON. One can proceed using make, make install than. The samples can be found in bin after building.
See also FAQ
How to compile OpenCV sample code ?
# For OpenCV 3
cd /path/to/opencv/samples/cpp/
#Compile
g++ -ggdb `pkg-config --cflags --libs opencv` facedetect.cpp -o facedetect
#run
./facedetect
Works for me.
googled from this link
mydragonisland's build instructions almost worked for me; with a minor reordering and including accents:
g++ facedetect.cpp -o facedetect `pkg-config --libs opencv`
The macro 'ocv_check_dependencies' is defined in: your_path_to/opencv/cmake/OpenCVModule.cmake
# ensures that all passed modules are available
# sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
macro(ocv_check_dependencies)
set(OCV_DEPENDENCIES_FOUND TRUE)
foreach(d ${ARGN})
if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
set(OCV_DEPENDENCIES_FOUND FALSE)
break()
endif()
endforeach()
endmacro()
The top level CMakeLists.txt contains 'include' commands for files from opencv/cmake/ . Which is why the macro is available when you compile by calling cmake from the root of the opencv sources.
Reason
error message context:
CMake Error at CMakeLists.txt:10 (ocv_check_dependencies):
Unknown CMake command "ocv_check_dependencies".
This error message happens because cmake can't find the definition of ocv_check_dependencies
That's why the console said Unknown CMake command
Solution
If cmake cannot find where ocv_check_dependencies is defined
Just like #Nick Hockings Said:
ocv_check_dependencies is a macro defined in Your/OpenCV/path/OpenCVModule.cmake
macro(ocv_check_dependencies)
set(OCV_DEPENDENCIES_FOUND TRUE)
foreach(d ${ARGN})
if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
set(OCV_DEPENDENCIES_FOUND FALSE)
break()
endif()
endforeach()
endmacro()
The fastest way is to copy this snippet above to your CMakeList.txt file right above where ocv_check_dependencies is
Therefore, cmake can finally understand what it is
That should do the trick, i hope no one else will bother with this question in the future
I got similar errors. My approach is as following:
1) cd xxx/samples 2) mkdir build 3) cd build 4) cmake .. 5) make
Now it works. We could not build individual project under their source files.
Following steps works for me.
Export toolchain path.
cd opencv-3.3.0/samples
cross_cmake &&
cross_make
cd opencv-3.3.0/samples/cpp/

How do you set CMAKE_C_COMPILER and CMAKE_CXX_COMPILER for building Assimp for iOS?

When I try to build Assimp by running build_ios.sh, it tells me:
CMake Error: your C compiler: "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc" was not found. Please set CMAKE_C_COMPILER to a valid compiler path or name.
CMake Error: your CXX compiler: "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-g++" was not found. Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
What I need the path to be is:
/Applications/XCode.app/Contents/Developer/Platforms/...
I've tried changing DEVROOT in build_ios.sh and IPHONE_xxxx_TOOLCHAIN.cmake, because that's what CMAKE_C_COMPILER etc seem to get generated from, but it still gives me the same errors.
Option 1:
You can set CMake variables at command line like this:
cmake -D CMAKE_C_COMPILER="/path/to/your/c/compiler/executable" -D CMAKE_CXX_COMPILER "/path/to/your/cpp/compiler/executable" /path/to/directory/containing/CMakeLists.txt
See this to learn how to create a CMake cache entry.
Option 2:
In your shell script build_ios.sh you can set environment variables CC and CXX to point to your C and C++ compiler executable respectively, example:
export CC=/path/to/your/c/compiler/executable
export CXX=/path/to/your/cpp/compiler/executable
cmake /path/to/directory/containing/CMakeLists.txt
Option 3:
Edit the CMakeLists.txt file of "Assimp": Add these lines at the top (must be added before you use project() or enable_language() command)
set(CMAKE_C_COMPILER "/path/to/your/c/compiler/executable")
set(CMAKE_CXX_COMPILER "/path/to/your/cpp/compiler/executable")
See this to learn how to use set command in CMake. Also this is a useful resource for understanding use of some of the common CMake variables.
Here is the relevant entry from the official FAQ: https://gitlab.kitware.com/cmake/community/wikis/FAQ#how-do-i-use-a-different-compiler
The cc and cxx is located inside /Applications/Xcode.app. This should find the right paths
export CXX=`xcrun -find c++`
export CC=`xcrun -find cc`
SOLUTIONS
Sometimes the project is created before installing g++. So install g++ first and then recreate your project. This worked for me.
Paste the following line in CMakeCache.txt:
CMAKE_CXX_COMPILER:FILEPATH=/usr/bin/c++
Note the path to g++ depends on OS. I have used my fedora path obtained using which g++

Resources