首页 > 其他分享 >用CMake 替换 Makefile

用CMake 替换 Makefile

时间:2023-06-28 17:55:35浏览次数:37  
标签:Hello CMake -- Makefile hello build cmake 替换 compiler

原文:https://blog.csdn.net/love131452098/article/details/116241448

CMake实质上是用于生成Makefile的工具,现在越来越多开源的项目, 在项目的编译框架上使用CMake替换Makefile. CMake 相对于Makefile规则更加简单,重要的是CMake官方网站提供了较为友好的指南.

CMake 官方学习资料指引

CMake 入门指南(英文版)

CMake tutorial CMake 教程

CMake 学习资料集合

学习资料集合

CMake 替换Makefile CMake 替换Makefile

接下来会以以下三个点来展示CMake 替换Makefile.

  1. 编译目标文件,直接生成可执行程序
  2. .a 或者 .so 库文件编译生成
  3. 链接库文件
1. 直接生成可执行程序

打开Linux下的终端,创建如下目录和文件.

➜  cmake_example tree
.
├── build
└── src
    ├── CMakeLists.txt
    └── hello.c

-> hello.c, 只是简单的一个main函数,然后打印字符串"Hello"

#include <stdio.h>

int main(int argc, char **argv){
    printf("Hello \r\n");
    return 0;                                                   
}

-> src/CMakeLists.txt, 只是设置了项目名称(最终可执行程序的名称),和直接直接编译可执行程序

# set the project name and version
cmake_minimum_required(VERSION 3.10)

#set the project name
project(Hello VERSION 2.0)


#add the executable
add_executable(Hello hello.c)

在完成目录和文件的创建后,我们通过cmake指令进行Makefile的生成和代码工程编译.

在工程的顶层目录,执行cd指令进入build目录,然后运行cmake指令+源码所在目录(也就是CMakeLists.txt的所在目录). 具体过程如下,cmake命令运行成功后在build目录生成了Makefile文件和其它相关文件.

➜  cmake_example cd build 
➜  build ls
➜  build cmake ../src/
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tracy/Work_Space/Apollo/base_tech/apollo_cmake/basic_start/cmake_example/build
➜  build ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile

在生成Makefile文件后,可以直接在build目录下执行: make或者cmake指令,具体如下,

➜  build cmake --build .
Scanning dependencies of target Hello
[ 50%] Building C object CMakeFiles/Hello.dir/hello.c.o
[100%] Linking C executable Hello
[100%] Built target Hello
➜  build make
[100%] Built target Hello

最后会在build目录下生成,可执行程序Hello.

➜  build make
[100%] Built target Hello
➜  build ls
CMakeCache.txt  cmake_install.cmake  Makefile
CMakeFiles      Hello
➜  build ./Hello 
Hello 
2. .so或者 .a 库文件编译生成

打开Linux下的终端,创建如下目录和文件

➜  cmake_example tree
.
├── build
└── src
    ├── CMakeLists.txt
    ├── hello.c
    └── hello.h

hello.c, 库的源文件

#include <stdio.h>

int say_hello(void){
    printf("Hello \r\n");
    return 0;
}

hello.h

#ifndef __HELLO__
#define __HELLO__

int say_hello(void);

#endif

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

#set the project name
project(Hello VERSION 2.0)

# SHARED 为动态库, STATIC为静态库
add_library(hello SHARED 
            hello.c) 

 target_include_directories(hello PUBLIC
                           "${PROJECT_BINARY_DIR}"
                           )
                           
在以上文件完成创建后,在工程的顶层目录,执行cd指令进入build目录,然后运行cmake指令+源码所在目录(也就是CMakeLists.txt的所在目录). 具体过程如下,cmake命令运行成功后在build目录生成了Makefile文件和其它相关文件
```bash
➜  build cmake ../src
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tracy/Work_Space/Apollo/base_tech/apollo_cmake/basic_start/cmake_example/build
➜  build make
Scanning dependencies of target hello
[ 50%] Building C object CMakeFiles/hello.dir/hello.c.o
[100%] Linking C shared library libhello.so
[100%] Built target hello

最后会在build目录下生成,库文件libhello.so

3. 链接库文件

打开Linux下的终端,创建如下目录和文件

➜  cmake_example tree
.
├── build
└── src
    ├── CMakeLists.txt
    ├── hello.c
    ├── hello.h
    └── main.c

main.c, 调用libhello.so 中的say_hello()
main.c, 调用libhello.so 中的say_hello()

#include <stdio.h>
#include "hello.h"

int main(int argc, char **argv){
    say_hello();
    return 0;
}

hello.c, 库的源文件

#include <stdio.h>

int say_hello(void){
    printf("Hello \r\n");
    return 0;
}

hello.h

#ifndef __HELLO__
#define __HELLO__

int say_hello(void);

#endif

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

#set the project name
project(Hello VERSION 2.0)

# add the executable
add_executable(Hello main.c)

add_library(hello SHARED 
            hello.c)

target_link_libraries(Hello PUBLIC hello)

target_include_directories(hello PUBLIC
                           "${PROJECT_BINARY_DIR}"
                           )

在以上文件完成创建后,在工程的顶层目录,执行cd指令进入build目录,然后运行cmake指令+源码所在目录(也就是CMakeLists.txt的所在目录). 具体过程如下,cmake命令运行成功后在build目录生成了Makefile文件和其它相关文件.

最后在build目录,生成了可执行程序Hello.

 

refs:

https://blog.csdn.net/love131452098/article/details/116241448

https://www.cnblogs.com/silence-cho/p/16212503.html

 

 

标签:Hello,CMake,--,Makefile,hello,build,cmake,替换,compiler
From: https://www.cnblogs.com/bluestorm/p/17512134.html

相关文章

  • CMake快速入门
    CMake快速入门目录CMake快速入门1.为什么要使用CMake?2.创建第一个CMake工程3.CMake指令介绍3.1cmake_minimum_required指令3.2project指令3.3set指令3.4message指令3.5add_executable指令3.6add_subdirectory指令3.7add_library指令3.8add_compile_opti......
  • 不能使用astyle对Linux的Kconfig和Makefile进行排版,会导致编译错误
    代码排版工具不同人代码的排版习惯不一样。遇到自己不喜欢的格式,看起来比较麻烦。可以使用astyle对代码进行排版。我喜欢以"otbs"格式的排版。命令如下:astyle-s--style=otbs*.castyle-s--style=otbs*.h错误排版一次使用时,不小心对所有文件进行了排版。hankf@XSZGS4......
  • makefile只编译修改过的文件
    本文演示配置makefile,检测修改的代码才编译,提升编译效率。1.测试代码$tree.├──app.c├──lib│├──module2.c│└──module2.h├──makefile├──module1.c└──module1.happ.c#include<stdio.h>#include"app.h"#include"......
  • python版本的 playwright , 如何拦截请求,替换响应内容
         fromplaywright.sync_apiimportPlaywright,sync_playwrightdefrequest_interceptor(route,request):logger.info(request.url)if'api.js'inrequest.url:#替换响应内容route.fulfill(status=200,......
  • C#操作Word模板文件 替换并重新生成
    这里用到了一个操作Word的第三方开源库:DocX;这个库对于操作Word文件绝对是个好东西,更优于NPOI,而且也是不依赖于Office的;   核心代码///<summary>///Word模板替换///<para>当前适用的字段模板形如:[=Name],其中Name就是字段名</para>......
  • jeecg2-VUE-全局替换字体(鸿蒙、、、)
    html,body{font-family:-apple-system,BlinkMacSystemFont,'SegoeUI','PingFangSC',"HarmonyOSSansSC",'HiraginoSansGB','MicrosoftYaHei','HelveticaNeue',Helvetica,Arial,sans-serif......
  • 调试 Web 页面时如何替换请求响应结果
    开发jssdk项目的时候,经常会希望快速检查我们的改动在真实场景中的表现。我们不可能直接更新到生产环境,于是很多人会考虑配置代理,并替换响应内容。但代理配置起来并不方便,比如在ios/android真机上开关代理需要手动输入ip、端口。我们还有一些更便捷的方法:Chromelocalover......
  • CMake基础(三) - Cmake实战
    一个实战教程,通过一个具体的示例来演示如何使用CMake构建和管理一个C++项目。这里有一个简单的数学库,其中包含两个源文件math.cpp和math.h,并且有一个示例程序main.cpp使用该库。以下是项目的目录结构:-MyMathLib-CMakeLists.txt-src-main.cpp......
  • 【pycharm】替换字符串的三种方法
    一、场景  工作中我们可能需要修改一些字符串为同一字符串,此时pycharm的一些替换功能就很好用 二、快捷键1、基于当前文件CTRL+R2、基于全局的替换 CTRL+SHIFT+R  三、替换的三种方法1、基于Cc的字符串 这种最简单,就是简单的替换某个字符串为另一个,可以......
  • 时速云使用 Higress 替换 Ngnix Ingress + Spring Cloud Gateway 的生产实践
    作者:王金山,北京云思畅想科技有限公司技术部微服务架构师,负责公司API网关和服务网格等研发工作时速云介绍时速云成立于2014年10月,致力于通过云原生技术帮助企业实现数字化转型,拥有云原生应用平台TCAP和云原生数据平台KubeData两大核心产品体系,产品包含云原生DevOps、容器......