[cmake快速入门]初始cmake

1.准备工作

代码main.cpp

#include 

int main(int, char**) {
    std::cout << "Hello, world!\n";
}

CmakeLists.txt 文件内容

cmake_minimum_required(VERSION 3.0.0)
project(demo VERSION 0.1.0)

set(SRC_LIST main.cpp)

message(STATUS "1.PROJECT_BINARY_DIR = ${PROJECT_BINARY_DIR}")
add_executable(demo ${SRC_LIST})

建立build文件夹,mkdir build

目录结构

liuwang@LAPTOP-9FR3DSS2:~/cmake$ ls
CMakeLists.txt  build  main.cpp

2.开始构建

所有的文件创建完成后,目录中应该存在 main.c 和 CMakeLists.txt 两个文件,一个build文件夹,我们进入build文件夹。
接下来我们来构建这个工程,在这个目录运行  cmake ..

进行cmake结果 

liuwang@LAPTOP-9FR3DSS2:~/cmake/build$ cmake ..
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- 1.PROJECT_BINARY_DIR = /home/liuwang/cmake/build
-- Configuring done
-- Generating done
-- Build files have been written to: /home/liuwang/cmake/build

你会发现,系统自动生成了:
CMakeFiles, CMakeCache.txt, cmake_install.cmake 等文件,并且生成了
Makefile.
现在不需要理会这些文件的作用,以后你也可以不去理会。最关键的是,它自动生成了
Makefile.
然后进行工程的实际构建,在这个目录输入 make 命令,大概会得到如下的彩色输出        

liuwang@LAPTOP-9FR3DSS2:~/cmake/build$ make
Scanning dependencies of target demo
[ 50%] Building CXX object CMakeFiles/demo.dir/main.cpp.o
[100%] Linking CXX executable demo
[100%] Built target demo

如果你需要看到 make 构建的详细过程,可以使用 make VERBOSE=1 或者 VERBOSE=1
make 命令来进行构建。

liuwang@LAPTOP-9FR3DSS2:~/cmake/build$ make VERBOSE=1
/usr/bin/cmake -S/home/liuwang/cmake -B/home/liuwang/cmake/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/liuwang/cmake/build/CMakeFiles /home/liuwang/cmake/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/liuwang/cmake/build'
make -f CMakeFiles/demo.dir/build.make CMakeFiles/demo.dir/depend
make[2]: Entering directory '/home/liuwang/cmake/build'
cd /home/liuwang/cmake/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/liuwang/cmake /home/liuwang/cmake /home/liuwang/cmake/build /home/liuwang/cmake/build /home/liuwang/cmake/build/CMakeFiles/demo.dir/DependInfo.cmake --color=
Dependee "/home/liuwang/cmake/build/CMakeFiles/demo.dir/DependInfo.cmake" is newer than depender "/home/liuwang/cmake/build/CMakeFiles/demo.dir/depend.internal".
Dependee "/home/liuwang/cmake/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/liuwang/cmake/build/CMakeFiles/demo.dir/depend.internal".
Scanning dependencies of target demo
make[2]: Leaving directory '/home/liuwang/cmake/build'
make -f CMakeFiles/demo.dir/build.make CMakeFiles/demo.dir/build
make[2]: Entering directory '/home/liuwang/cmake/build'
[ 50%] Building CXX object CMakeFiles/demo.dir/main.cpp.o
/usr/bin/c++     -o CMakeFiles/demo.dir/main.cpp.o -c /home/liuwang/cmake/main.cpp
[100%] Linking CXX executable demo
/usr/bin/cmake -E cmake_link_script CMakeFiles/demo.dir/link.txt --verbose=1
/usr/bin/c++    -rdynamic CMakeFiles/demo.dir/main.cpp.o  -o demo
make[2]: Leaving directory '/home/liuwang/cmake/build'
[100%] Built target demo
make[1]: Leaving directory '/home/liuwang/cmake/build'
/usr/bin/cmake -E cmake_progress_start /home/liuwang/cmake/build/CMakeFiles 0

你可能感兴趣的:(cmake)