IT袋

当前位置:主页 > 经验教程 > 建站编程 >

如何使用meson构建C语言项目

如何使用meson构建C语言项目(2)

时间:2024-01-29 01:28:34 来源:IT袋 作者:马勇
导读:如何使用meson构建C语言项目,project('my_project', 'c')# Define sourcessrc_files = files(['src/main.c', 'src/util/helper.c'])# Define include directoriesinc_path = include_directories('include')# Set CC environment variable to sp

如何使用meson构建C语言项目

project('my_project', 'c')
# Define sources
src_files = files(['src/main.c', 'src/util/helper.c'])
# Define include directories
inc_path = include_directories('include')
# Set CC environment variable to specify the compiler
# Add an executable
executable('my_hello', src_files, include_directories: inc_path)

native-file.txt

该文件指定一些构建要用的参数

[binaries]
c = ['ccache', 'x86_64-linux-gnu-gcc']
cpp = ['ccache', 'x86_64-linux-gnu-g++']
ar = 'x86_64-linux-gnu-gcc-ar'
strip = 'x86_64-linux-gnu-strip'
pkgconfig = 'x86_64-linux-gnu-pkg-config'
pcap-config = ''
cmake = 'cmake'

2.2 完整构建meson_project项目

1. meson setup build 创建构建目录

这个是告诉meson在哪个目录下构建(这里是源码根目录下的build目录),meson一定要在一个和源码独立的目录里做构建,这样多次构建可以指定不同的构建目录和构建配置,相互之间不受影响,比如对于同样的程序,构建一个riscv版本可以这样指定构建目录:

meson setup --cross-file ./rv_cross_file rv_builddir

开始创建build目录

$ meson setup build  # 创建build目录
The Meson build system
Version: 0.61.2
Source dir: /home/meson_project
Build dir: /home/meson_project/build
Build type: native build
Project name: my_project
Project version: undefined
C compiler for the host machine: cc (gcc 11.4.0 "cc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0")
C linker for the host machine: cc ld.bfd 2.38
Host machine cpu family: x86_64
Host machine cpu: x86_64
Build targets in project: 1
Found ninja-1.10.1 at /usr/bin/ninja

2.根据native_file.txt配置文件,指定构建目录

meson_project$ ls
build  include  meson.build  native-file.txt  src
meson_project$ meson build --cross-file native-file.txt
Directory already configured.
Just run your build command (e.g. ninja) and Meson will regenerate as necessary.
If ninja fails, run "ninja reconfigure" or "meson --reconfigure"
to force Meson to regenerate.
If build failures persist, run "meson setup --wipe" to rebuild from scratch
using the same options as passed when configuring the build.
To change option values, run "meson configure" instead.

3.使用ninja将源码编译并输出到build目录下

meson_project$ ninja -C build -v
ninja: Entering directory `build'
[1/3] cc -Imy_hello.p -I. -I.. -I../include -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -g -MD -MQ my_hello.p/src_util_helper.c.o -MF my_hello.p/src_util_helper.c.o.d -o my_hello.p/src_util_helper.c.o -c ../src/util/helper.c
[2/3] cc -Imy_hello.p -I. -I.. -I../include -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -g -MD -MQ my_hello.p/src_main.c.o -MF my_hello.p/src_main.c.o.d -o my_hello.p/src_main.c.o -c ../src/main.c
[3/3] cc  -o my_hello my_hello.p/src_main.c.o my_hello.p/src_util_helper.c.o -Wl,--as-needed -Wl,--no-undefined
meson_project$
meson_project$
meson_project$ ls
build  include  meson.build  native-file.txt  src

相关阅读

  • 云存储的三种存储方式 云存储的使用场景

    云存储的三种存储方式 云存储的使用场景

    今天带来的IT技巧小经验云存储的三种存储方式的电脑方面的小经验,下面IT袋网为您详细介绍 云存储服务通常提供三种主要的存储方式,分别是:对象存储、块存储和文件存储。 这三种存储

  • 网络存储类型、其优缺点以及使用场景有哪些

    网络存储类型、其优缺点以及使用场景有哪些

    为关注IT袋网网的网友们详解网络存储类型、其优缺点以及使用场景有哪些方面的介绍,如有不对的地方欢迎指正! 1. 简介 网络存储是一种通过网络连接的方式,将数据存储在独立的存储设备

  • 专业网站制作的教程是什么 关于网页制作步骤

    专业网站制作的教程是什么 关于网页制作步骤

    今天带来的IT技巧小经验专业网站制作的教程是什么和关于网页制作步骤的话题,具体详情如下: 在找工作的过程中,个人简历发挥着重要作用。要想让自己的简历更加吸引人,你得多花费点

  • 深入探讨:x86与ARM架构的比较与差异

    深入探讨:x86与ARM架构的比较与差异

    如果想了解的介绍,请看下面详细的介绍。 在电脑和移动设备的世界里,你可能经常听到x86和ARM这两个词。 这些都是处理器架构的类型,但它们在设计和工作方式上有很大的不同。 为了理解