今天开始长更《CMake那些事》系列
每一期的内容将会以:例子驱动、目录驱动的方式来Step by step方式学习,欢迎收藏转发。
生成可执行文件
假设我们只有一个.cc 文件。
- 根目录中创建一个CMakeLists.txt文件。
cmake_minimum_required(VERSION 2.8)
project(app_project)
add_executable(hello main.cc)
install(TARGETS hello DESTINATION bin)
执行步骤,见->
后面内容,先创建build目录,然后编译安装,最后便会安装到/usr/local/bin/hello,上面bin也可以替换为自己的目录。
➜ mkdir build
➜ cd build
➜ cmake ..
-- The C compiler identification is AppleClang 12.0.5.12050022
-- The CXX compiler identification is AppleClang 12.0.5.12050022
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /cmake/build
➜ make
[ 50%] Building CXX object CMakeFiles/hello.dir/main.cc.o
[100%] Linking CXX executable hello
[100%] Built target hello
➜ make install
Consolidate compiler generated dependencies of target hello
[100%] Built target hello
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/bin/hello
最后执行:
代码语言:javascript复制➜ /usr/local/bin/hello
hello world
接下来,我们将上面内容拆解:
第一步:cmake ..
生成Makefile
第二步:make
编译Makefile生成bin文件
第三步:make install
安装到指定的目录