Home > Article > System Tutorial > Install cmake using ubuntu and its simple usage
Preface
Recently, I suddenly wanted to transfer the development environment to Linux, and I was also preparing to read some open source codes on github. I found that open source projects are now generally managed with cmake. So I just tinkered with it on my own virtual machine. I didn't know what cmake was at first, but later I roughly understood its role through some fiddling. What it does is actually tell the compiler how to compile and link the source code. You may want to ask if there is no makefile, why do you need it? This involves cross-platform issues. Under the Windows platform, these are managed through project files. If cmake is not used, then we have to write corresponding project files and makefile files for window and Linux systems. This is undoubtedly a tedious thing, and we only need to write cmake once. , can be used on various platforms, and its syntax is simple. This is the so-called "write once, use everywhere".
Installation process
1. First go to the official website (https://www.php.cn/link/da97f65bd113e490a5fab20c4a69f586/) to download the installation package and select the "XX.tar.gz" source code installation package
2. Enter the following command
$tar -zxvf xx.tar.gz
$./bootstrap
$make
$make install
enter
Write simple cmake
To use cmake, you must first have a CMakeList.txt file. You need to write the configuration information in the file, and then process the file through cmake.
There will be the following main.cpp file
//main.cpp file
#include
using namespace std;
int main(){
cout
return 0;
}
At this time we can write the following CMakeList.txt file
#cmake minimum required version
cmake_minimum_required(VERSION 2.8)
#Project name
project(HELLOWORLD)
#Contains the original program, that is, copies the source program in the given directory to the variable DIR_SRC
aux_source_directory(DIR_SRC ./)
#Generate program
add_executable(helloworld ${DIR_SRC})
Then execute the following command
$mkdir build
$cd build
$cmake ..
$make
$./helloworld
This will compile the program and run it.
Add static library or dynamic library
Assuming that our program uses a static library libmy.a under /usr/lib, then we need to add the following two commands
#The location of the library
link_directories(/usr/lib)
#Link library when compiling the program
target_link_libraries(helloworld my)
The above is the detailed content of Install cmake using ubuntu and its simple usage. For more information, please follow other related articles on the PHP Chinese website!