zl程序教程

您现在的位置是:首页 >  其它

当前栏目

随想录(cmake编译)

编译 CMake 随想录
2023-09-27 14:27:11 时间


【 声明:版权所有,欢迎转载,请勿用于商业用途。  联系信箱:feixiaoxing @163.com】


    在linux下面,编译软件有很多的方法。一般来说,有这么几种。第一,自己写makefile,这种情况一般适用于工程比较简单的项目,结构不复杂,同时也能满足需求;第二,利用autoconf、automake生成makefile。目前来说,大多数开源项目都是采用这种编译方法。这种方法用起来也比较简单,常见的操作就是 ./configure && make && make install。第三,就是cmake这种,不用自己写makefile,只需要配置一下脚本,自动生成makefile。第四种不太常见,scons创建,和第三种差不多,但是比第三种还要简单,一两行脚本文件就能完成所有的操作。


    既然今天说的是cmake,那么我们说明一下如何利用cmake创建makefile编译。

    1) 安装cmake。

    2)    创建prj目录,cd到prj目录下面。

    3)    拷贝两个文件过来,比如说add.c和main.c,这是比较常见的情况,即多个文件生成一个执行文件。

    4)    创建CMakeLists.txt文件,输入下面的内容,

project(HELLO)
set(SRC_LIST main.c add.c)
add_executable(hello ${SRC_LIST})

    5)    输入cmake .命令,注意后面的点号。

feixiaoxingdeMacBook-Pro-2:cmake feixiaoxing$ cmake .
-- The C compiler identification is AppleClang 7.0.0.7000176
-- The CXX compiler identification is AppleClang 7.0.0.7000176
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/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: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/feixiaoxing/Desktop/cmake

    6)    此时makefile已经生成,直接make即可。

feixiaoxingdeMacBook-Pro-2:cmake feixiaoxing$ make
Scanning dependencies of target hello
[ 33%] Building C object CMakeFiles/hello.dir/main.c.o
[ 66%] Building C object CMakeFiles/hello.dir/add.c.o
[100%] Linking C executable hello
[100%] Built target hello