Explain several ways of installing CMake in ubuntu in detail

  • 2021-08-21 22:04:37
  • OfStack

apt Install CMake


sudo apt install cmake

This method is easy to install, but the disadvantage is that if you want to cross-compile opencv on Android platform, you will be prompted that the version is too low, because the cmake version in ubuntu16.04 source is only 3.5. 1, while the Android cross-compiler tool chain android.toolchain.cmake requires the minimum version of cmake to be 3.6. 0

Download the source code to compile CMake

Download the latest cmake from cmake official website

https://cmake.org/download/

Unzip after downloading, and then enter the directory to execute:


./bootstrap
make -j8
sudo make install

Verify version


cmake --version

cmake version 3.9.0

CMake suite maintained and supported by Kitware (kitware.com/cmake).

This allows you to install the latest version of cmake, but if you want to cross-compile the third-party library for the Android platform, you will have compile-time problems because the cross-compile tool chain android. toolchain. cmake provided in Android Sdk does not yet support the latest version of cmake

CMake in Soft Link Android Sdk

Softlink cmake in Android Sdk to the/usr/local/bin directory


sudo ln -s /home/gavinandre/Android/Sdk/cmake/3.6.4111459/bin/cmake /usr/local/bin

Now you should have no problem compiling some third-party libraries by using the android cross-compilation tool chain android. toolchain. cmake

Write a simple example of Cmake

First we write an main. cpp file, a simple helloworld program


#include<iostream>
int main()
{
  std::cout<<"hello world!"<<std::endl;
  return 0;
}

Then write the CMakeLists. txt file


cmake_minimum_required(VERSION 2.8)
# Project name 
project(HELLOWORLD)
# Include the original program , That is, the source program in a given directory is copied to the variable DIR_SRC
# Saves the source file under the specified path in the specified variable 
aux_source_directory(./ DIR_SRC)
# Generator 
add_executable(helloworld ${DIR_SRC})

Compile


$mkdir build
$cd build
$cmake ..
$make
$./helloworld

Implementation results:

hello world!


Related articles: