root@minloha:~/LearnCmake/demo01# cmake . -- This is BINARY dir -- This is SOURCE dir -- Configuring done -- Generating done -- Build files have been written to: /root/LearnCmake/demo01 root@minloha:~/LearnCmake/demo01# ls a.out CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt hello hello.cpp Makefile root@minloha:~/LearnCmake/demo01#
然后就可以用make指令编译,bash进行执行
1 2 3 4 5 6 7 8
root@minloha:~/LearnCmake/demo01# make Scanning dependencies of target hello [ 50%] Building CXX object CMakeFiles/hello.dir/hello.cpp.o [100%] Linking CXX executable hello [100%] Built target hello root@minloha:~/LearnCmake/demo01# ./hello 2.71828 root@minloha:~/LearnCmake/demo01#
root@minloha:~/LearnCmake/demo02# mkdir build root@minloha:~/LearnCmake/demo02# cd build/ root@minloha:~/LearnCmake/demo02/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 -- This is BINARY dir -- This is SOURCE dir -- Configuring done -- Generating done -- Build files have been written to: /root/LearnCmake/demo02/build root@minloha:~/LearnCmake/demo02/build# ls CMakeCache.txt CMakeFiles cmake_install.cmake Makefile root@minloha:~/LearnCmake/demo02/build#
root@minloha:~/LearnCmake/demo02/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 CMake Warning (dev) in CMakeLists.txt: No cmake_minimum_required command is present. A line of code such as
cmake_minimum_required(VERSION 3.16)
should be added at the top of the file. The version specified may be lower if you wish to support older CMake versions for this project. For more information run "cmake --help-policy CMP0000". This warning is for project developers. Use -Wno-dev to suppress it.
-- Configuring done -- Generating done -- Build files have been written to: /root/LearnCmake/demo02/build root@minloha:~/LearnCmake/demo02/build# ls bin CMakeCache.txt CMakeFiles cmake_install.cmake Makefile root@minloha:~/LearnCmake/demo02/build#
root@minloha:~/LearnCmake/demo02/build# make Scanning dependencies of target YEAH [ 50%] Building CXX object bin/CMakeFiles/YEAH.dir/main.o [100%] Linking CXX executable YEAH [100%] Built target YEAH root@minloha:~/LearnCmake/demo02/build# ls bin CMakeCache.txt CMakeFiles cmake_install.cmake Makefile root@minloha:~/LearnCmake/demo02/build# cd bin/ root@minloha:~/LearnCmake/demo02/build/bin# ls CMakeFiles cmake_install.cmake Makefile YEAH root@minloha:~/LearnCmake/demo02/build/bin#
root@minloha:~/LearnCmake/demo03/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 CMake Warning (dev) in CMakeLists.txt: No cmake_minimum_required command is present. A line of code such as
cmake_minimum_required(VERSION 3.16)
should be added at the top of the file. The version specified may be lower if you wish to support older CMake versions for this project. For more information run "cmake --help-policy CMP0000". This warning is for project developers. Use -Wno-dev to suppress it.
-- Configuring done -- Generating done -- Build files have been written to: /root/LearnCmake/demo03/build
然后就可以make构建然后make安装了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
root@minloha:~/LearnCmake/demo03/build# make Scanning dependencies of target YEAH [ 50%] Building CXX object bin/CMakeFiles/YEAH.dir/main.o [100%] Linking CXX executable YEAH [100%] Built target YEAH lroot@minloha:~/LearnCmake/demo03/build# ls bin CMakeCache.txt CMakeFiles cmake_install.cmake Makefile root@minloha:~/LearnCmake/demo03/build# cd bin root@minloha:~/LearnCmake/demo03/build/bin# ls CMakeFiles cmake_install.cmake Makefile YEAH root@minloha:~/LearnCmake/demo03/build/bin# make install [100%] Built target YEAH Install the project... -- Install configuration: "" root@minloha:~/LearnCmake/demo03/build/bin#
root@minloha:~/LearnCmake/demo04/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 CMake Warning (dev) in CMakeLists.txt: No cmake_minimum_required command is present. A line of code such as
cmake_minimum_required(VERSION 3.16)
should be added at the top of the file. The version specified may be lower if you wish to support older CMake versions for this project. For more information run "cmake --help-policy CMP0000". This warning is for project developers. Use -Wno-dev to suppress it.
-- Configuring done -- Generating done -- Build files have been written to: /root/LearnCmake/demo04/build root@minloha:~/LearnCmake/demo04/build# make Scanning dependencies of target YEAH [ 50%] Building CXX object bin/CMakeFiles/YEAH.dir/main.o [100%] Linking CXX shared library libYEAH.so [100%] Built target YEAH root@minloha:~/LearnCmake/demo04/build# cd bin/ root@minloha:~/LearnCmake/demo04/build/bin# ls CMakeFiles cmake_install.cmake libYEAH.so Makefile root@minloha:~/LearnCmake/demo04/build/bin#
可以看到生成了linYEAH.so的动态库
指令总结与补充
指令
功能
PROJECT (项目名)
定义项目
SET(变量名 值)
定义一个Make变量保存数据
MESSAGE(STATUS 字符串)
使用make时输出一些信息或警告
ADD_EXECUTABLE(生成文件名 源文件)
添加CMAKE时生成的可执行文件
ADD_SUBDIRECTORY( 当前目录 生成的目录)
将已有的目录作为Make生成的子目录
INSTALL(类型 文件 DESTINATION 目标文件夹)
将不同类型的文件生成到不同的文件夹中,类型可以参考上文
ADD_LIBRARY(库名 静态/动态 源文件)
将源文件生成为动态或静态库
全篇的运行Cmake都有警告,解决办法是在CMakeLists.txt中加入cmake_minimum_required( VERSION 2.8 ),降低Cmake语言版本即可