meson编译C代码简单介绍及实例

最近工作中接触到了使用meson编译源码的方式,不同于以往的make/cmake,特此学习记录一下,也给没接触过的小伙伴做一点点参考。

一、什么是meson

Meson 旨在开发最具可用性和快速的构建系统。提供简单但强大的声明式语言用来描述构建。原生支持最新的工具和框架,如 Qt5 、代码覆盖率、单元测试和预编译头文件等。利用一组优化技术来快速变异代码,包括增量编译和完全编译。

二、安装meson

以ubuntu18.04.4为例:

注意:meson安装前必须确认是否已经安装python3.5及以上版本;因为meson依赖于python3和ninja
1、安装python3和ninja: sudo apt-get install python3 python3-pip ninja-build
2、切换至python3:
Ubuntu系统上一般默认安装了python2.7,因此如果安装了python3系统还是默认使用python2.7,因此需要切换至python3。
sudo rm /usr/bin/python3
sudo ln -s /usr/bin/python3.5 /usr/bin/python3
3、安装meson:pip3 install --user meson

三、使用meson

1、创建程序 test.c:

#include<stdio.h>

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

2、创建文件meson.build:

$ project('test','c')
$ executable('demo','test.c')

3、文件路径内容如下:

$ root@ZHP:~/meson_test# ls
$ meson.build  test.c

4、执行命令 meson build:

$ root@ZHP:~/meson_test# meson build
$ The Meson build system
$ Version: 0.45.1
$ Source dir: /root/meson_test
$ Build dir: /root/meson_test/build
$ Build type: native build
$ Project name: test
$ Native C compiler: cc (gcc 7.5.0 "cc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0")
$ Build machine cpu family: x86_64
$ Build machine cpu: x86_64
$ Build targets in project: 1
$ Found ninja-1.8.2 at /usr/bin/ninja
$ root@ZHP:~/meson_test# 

此时目录下产生文件夹build:

$ root@ZHP:~/meson_test# ls
$ build  meson.build  test.c
$ root@ZHP:~/meson_test# 

文件夹build内容:

$ root@ZHP:~/meson_test/build# ls
$ build.ninja  compile_commands.json  meson-logs  meson-private
$ root@ZHP:~/meson_test/build# 

5、进入build文件夹,执行命令 ninja :

$ root@ZHP:~/meson_test/build# ninja 
$ [2/2] Linking target demo.
$ root@ZHP:~/meson_test/build# ls
$ build.ninja  compile_commands.json  demo  demo@exe  meson-logs  meson-private
$ root@ZHP:~/meson_test/build# 

ninja 相当于 make, 所以上面会编译代码,生成可执行文件 demo。

6、执行目标文件:

$ root@ZHP:~/meson_test/build# ./demo
$ Hello meson!
$ root@ZHP:~/meson_test/build# 

四、编译开源代码

现在很多开源代码都转向meson进行构建和编译了,例如open5gs等等,可以在代码根目录下看到很多meson.build文件,并且每个子目录页包含meson.build文件 :

$ root@ZHP:~/open5gs# ls
$ build  configs  debian  docker  docs  install  lib  LICENSE  meson.build meson_options.txt  misc  README.md  src  subprojects  tests  vagrant  webui
$ root@ZHP:~/open5gs# ls src/
amf  ausf  bsf  hss  main.c  meson.build  mme  nrf  nssf  pcf  pcrf  sgwc  sgwu  smf  udm  udr  upf
$ root@ZHP:~/open5gs# ls src/upf/
app.c  arp-nd.cpp  arp-nd.h  context.c  context.h  event.c  event.h  gtp-path.c  gtp-path.h  init.c  meson.build  n4-build.c  n4-build.h  n4-handler.c  n4-handler.h  pfcp-path.c  pfcp-path.h  pfcp-sm.c  rule-match.c  rule-match.h  timer.c  timer.h  upf-sm.c  upf-sm.h
$ root@ZHP:~/open5gs# 

编译过程为:

$ cd open5gs
$ meson build --prefix=`pwd`/install
$ ninja -C build

[推荐一个零声学院免费公开课程,个人觉得老师讲得不错,分享给大家:Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK等技术内容,点击立即学习:

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值