跳过正文
Meson

Meson

··
目录

不建议参考,我并不熟悉meson以及编程

安装
#

sudo pacman -S meson

运行Meson
#

创建一个新目录来保存您的项目:

mkdir testproject
cd testproject

使用 Meson 创建并构建一个新的示例项目:

meson init --name testproject --build

这将为您创建一个项目框架并对其进行编译。结果放在build子目录中,可直接从那里运行:

build/testproject

该项目现已准备好进行开发。您可以使用任何编辑器编辑代码,并通过进入build子目录并执行meson compile命令来重建它。如果您的 Meson 版本太旧,您可以通过运行命令ninja来编译项目。

meson.build example
#

project(
    'hello',
    'c',
    meson_version: '>= 1.3.0',
    version: '0.1',
    default_options: ['warning_level=3'],
)

# 输出参数到 stdout
message('Beautifual World\n')

# 寻找源文件(使用 find 命令)
# 排除特定子目录(例如 build)
# find . -path "./build" -prune -or -name "*.c" -print
# 指定搜索深度
# find . -maxdepth 1 -name "*.c" -print
globber = run_command('find', '.', '-path', './build', '-prune', '-or', '-name', '*.c', '-print', check: true)
src = globber.stdout().strip().split('\n')

# 寻找库, mathlib可以作为依赖, 例如: 寻找 m 库
gcc = meson.get_compiler('c')
mathlib = gcc.find_library('m')

# 进入子目录, 执行子目录里的 meson.build 文件, 例如: path
subdir('path')

# 生成库文件, 返回的对象在 executable() 中可以用关键字 link_with 来链接
foo_sources = []
foolib = shared_library(
    'foo',
    foo_sources,
    include_directories: inc,
    dependencies: glib_dep,
    install: true,
)
foolib = static_library(
    'foo',
    foo_sources,
    include_directories: inc,
    dependencies: glib_dep,
    install: true,
)

dependencies = []

exe = executable('hello', 'hello.c', dependencies: dependencies, install: true)

test('basic', exe)
  • 可以运行meson install安装构建的可执行文件
  • 可以运行meson test测试构建的可执行文件
  • 可以运行meson format --inplace格式化 meson.build 文件
作者
Ask Me Why