栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

cmake 的使用

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

cmake 的使用

决定代码的组织方式及其编译方式,也是程序设计的一部分。因此,我们需要cmake和autotools这样的工具来帮助我们构建并维护项目代码。cmake和autotools正是makefile的上层工具,它们的目的正是为了产生可移植的makefile,并简化自己动手写makefile时的巨大工作量。

下载 cmake

http://www.cmake.org/cmake/resources/software.html 或者:cmake-3.22.2

#tar -xvf cmake-3.22.0-rc2.tar.gz        
#cd cmake-3.22.0-rc2
#./bootstrap
#make
#make install
 
cmake 会默认安装在 /usr/local/bin 下面
建立工程
q@ubuntu:~/test_cmake_prj$ tree .
.
├── build
├── CMakeLists.txt
├── main.c
└── src
    ├── CMakeLists.txt
    ├── test.c
    └── test.h

2 directories, 5 files
q@ubuntu:~/test_cmake_prj$ cat CMakeLists.txt 
PROJECT(cmake_prj) # 项目名称
CMAKE_MINIMUM_REQUIRED(VERSION 2.6) # cmake最低版本需求,不加入此行会受到警告信息
ADD_SUBDIRECTORY(src) # 指明本项目包含一个子目录src, 进入目录src对其中的CMakeLists.txt进行解析
AUX_SOURCE_DIRECTORY(. DIR_SRCS) # 把当前目录(.)下所有源代码文件和头文件加入变量DIR_SRCS
ADD_EXECUTABLE(main ${DIR_SRCS})
# 生成应用程序 main
TARGET_link_LIBRARIES(main Test) # 指明可执行文件 main 需要连接一个名为Test的链接库
q@ubuntu:~/test_cmake_prj$ cat main.c 
#include
int main(void)
{
    char s[] = "Hello,Worldn";
    printf(s);
    println(s);
    return 0;
}
q@ubuntu:~/test_cmake_prj$ cat src/CMakeLists.txt 
AUX_SOURCE_DIRECTORY(. DIR_TEST1_SRCS) # 把当前目录(.)下所有源代码文件和头文件加入变量DIR_TEST1_SRCS
ADD_LIBRARY(Test ${DIR_TEST1_SRCS}) # 将src目录中的源文件编译为共享库
q@ubuntu:~/test_cmake_prj$ cat src/test.c
#include 
#include "test.h"
void println()
{
	printf("PI=%.2fn", PI);
}
q@ubuntu:~/test_cmake_prj$ cat src/test.h
#define PI 3.14
void println();
生成 Makefile - cmake

注意事项,尽量在新建的 build 目录中进行生成操作,注意最后参数 ..为上级目录。

q@ubuntu:~/test_cmake/build$ sudo cmake ..
CMake Deprecation Warning at CMakeLists.txt:2 (CMAKE_MINIMUM_REQUIRED):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument  value or use a ... suffix to tell
  CMake that the project does not need compatibility with older versions.


-- Configuring done
-- Generating done
-- Build files have been written to: /home/q/test_cmake/build

生成位置如下:

q@ubuntu:~/test_cmake_prj/build$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile  src
make

运行

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/733903.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号