Ubuntu22.04中搭建GN编译环境

Ubuntu22.04中搭建GN编译环境

GN是一种元构建系统(Meta Build System),而ninja是一个注重速度的构建系统。ninja通常与元构建系统一并使用。常见的有cmake + make cmake + ninja gn + ninja 。本文要介绍的是gn + ninja的构建方式,另外OpenHarmony编译子系统也是以GNNinja构建为基座,对构建和配置粒度进行部件化抽象、对内建模块进行功能增强、对业务模块进行功能扩展的系统,具体可以参见:OpenHarmony编译构建指导

1. ninja安装

ninja安装有两种途径,一是通过ubuntu自带的软件库安装;另一个是从GitHub上下载ninja源码自己编译。本文是直接在Ubuntu中下载,如想自己编译,可以根据GitHub上的README.md进行操作,如:

复制代码
$ git clone https://github.com/ninja-build/ninja.git && cd ninja
$ git checkout release
$ cat README.md

我们直接通过ubuntu22.04中的sudo apt install ninja-build直接安装ninja软件:

shell 复制代码
sudo apt install ninja-build

安装完毕后,查询版本,显示出版本号后,说明安装成功。

shell 复制代码
$ ninja --version
1.11.1

说明我的Ubuntu22.04系统的ninja版本号是:1.11.1

2. 安装clang

编译gn需要编译器支持C++17的特性,所以编译器的版本不能太低,经过测试clang至少需要8.0的版本。我在我的系统上使用sudo apt search clang命令发现可以安装比较新的版本: clang-16,当然也可以安装其他的clang版本:

shell 复制代码
ccf@ccf-virtual-machine:~/WorkSpace/GithubProjects/ninja$ sudo apt search clang
Sorting... Done
Full Text Search... Done
bear/noble 3.1.3-1build4 amd64
  generate compilation database for Clang tooling

c2go/noble-updates,noble-security 0.26.10-1ubuntu0.24.04.3 amd64
  tool for transpiling C to Go

ccls/noble 0.20240202-1 amd64
  C/C++/ObjC language server

clang/noble 1:18.0-59~exp2 amd64
  C, C++ and Objective-C compiler (LLVM based), clang binary

clang-14/noble 1:14.0.6-19build4 amd64
  C, C++ and Objective-C compiler

clang-14-doc/noble,noble 1:14.0.6-19build4 all
  C, C++ and Objective-C compiler - Documentation

clang-14-examples/noble 1:14.0.6-19build4 amd64
  Clang examples

clang-15/noble 1:15.0.7-14build3 amd64
  C, C++ and Objective-C compiler

clang-15-doc/noble,noble 1:15.0.7-14build3 all
  C, C++ and Objective-C compiler - Documentation

clang-15-examples/noble 1:15.0.7-14build3 amd64
  Clang examples

clang-16/noble,now 1:16.0.6-23ubuntu4 amd64 [installed]
  C, C++ and Objective-C compiler

clang-16-doc/noble,noble 1:16.0.6-23ubuntu4 all
  C, C++ and Objective-C compiler - Documentation

clang-16-examples/noble 1:16.0.6-23ubuntu4 amd64
  Clang examples

clang-17/noble 1:17.0.6-9ubuntu1 amd64
  C, C++ and Objective-C compiler

clang-17-doc/noble,noble 1:17.0.6-9ubuntu1 all
  C, C++ and Objective-C compiler - Documentation

clang-17-examples/noble,noble 1:17.0.6-9ubuntu1 all
  Clang examples

clang-18/noble-updates 1:18.1.3-1ubuntu1 amd64
  C, C++ and Objective-C compiler

clang-18-doc/noble-updates,noble-updates 1:18.1.3-1ubuntu1 all
  C, C++ and Objective-C compiler - Documentation

clang-18-examples/noble-updates,noble-updates 1:18.1.3-1ubuntu1 all
  Clang examples

clang-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  C, C++ and Objective-C compiler

clang-19-doc/noble-updates,noble-updates 1:19.1.1-1ubuntu1~24.04.2 all
  C, C++ and Objective-C compiler - Documentation

clang-19-examples/noble-updates,noble-updates 1:19.1.1-1ubuntu1~24.04.2 all
  Clang examples

clang-format/noble 1:18.0-59~exp2 amd64
  Tool to format C/C++/Obj-C code

clang-format-14/noble 1:14.0.6-19build4 amd64
  Tool to format C/C++/Obj-C code

clang-format-15/noble 1:15.0.7-14build3 amd64
  Tool to format C/C++/Obj-C code

clang-format-16/noble 1:16.0.6-23ubuntu4 amd64
  Tool to format C/C++/Obj-C code

clang-format-17/noble 1:17.0.6-9ubuntu1 amd64
  Tool to format C/C++/Obj-C code

clang-format-18/noble-updates 1:18.1.3-1ubuntu1 amd64
  Tool to format C/C++/Obj-C code

clang-format-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  Tool to format C/C++/Obj-C code

clang-tidy/noble 1:18.0-59~exp2 amd64
  clang-based C++ linter tool

clang-tidy-14/noble 1:14.0.6-19build4 amd64
  clang-based C++ linter tool

clang-tidy-15/noble 1:15.0.7-14build3 amd64
  clang-based C++ linter tool

clang-tidy-16/noble 1:16.0.6-23ubuntu4 amd64
  clang-based C++ linter tool

clang-tidy-17/noble 1:17.0.6-9ubuntu1 amd64
  clang-based C++ linter tool

clang-tidy-18/noble-updates 1:18.1.3-1ubuntu1 amd64
  clang-based C++ linter tool

clang-tidy-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  clang-based C++ linter tool

clang-tools/noble 1:18.0-59~exp2 amd64
  clang-based tools

clang-tools-14/noble 1:14.0.6-19build4 amd64
  clang-based tools for C/C++ developments

clang-tools-15/noble 1:15.0.7-14build3 amd64
  clang-based tools for C/C++ developments

clang-tools-16/noble 1:16.0.6-23ubuntu4 amd64
  clang-based tools for C/C++ developments

clang-tools-17/noble 1:17.0.6-9ubuntu1 amd64
  clang-based tools for C/C++ developments

clang-tools-18/noble-updates 1:18.1.3-1ubuntu1 amd64
  clang-based tools for C/C++ developments

clang-tools-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  clang-based tools for C/C++ developments

clangd/noble 1:18.0-59~exp2 amd64
  Language server that provides IDE-like features to editors

clangd-14/noble 1:14.0.6-19build4 amd64
  Language server that provides IDE-like features to editors

clangd-15/noble 1:15.0.7-14build3 amd64
  Language server that provides IDE-like features to editors

clangd-16/noble 1:16.0.6-23ubuntu4 amd64
  Language server that provides IDE-like features to editors

clangd-17/noble 1:17.0.6-9ubuntu1 amd64
  Language server that provides IDE-like features to editors

clangd-18/noble-updates 1:18.1.3-1ubuntu1 amd64
  Language server that provides IDE-like features to editors

clangd-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  Language server that provides IDE-like features to editors

clazy/noble 1.11-4ubuntu2 amd64
  Clang plugin for additional warnings

debugbreak/noble 1.0-1 amd64
  Put breakpoints in C/C++ code

elpa-format-all/noble,noble 0.5.0-2 all
  Auto-format C, C++, JS, Python, Ruby and 50 other languages

elpa-irony/noble,noble 1.6.0-2build2 all
  Emacs C/C++ minor mode powered by libclang

emscripten/noble,noble 3.1.6~dfsg-7 all
  LLVM-to-JavaScript Compiler

emscripten-doc/noble,noble 3.1.6~dfsg-7 all
  LLVM-to-JavaScript Compiler

glslc/noble 2023.8-1build1 amd64
  Command line compiler for GLSL/HLSL to SPIR-V

golang-github-bmatsuo-lmdb-go-dev/noble-updates,noble-updates,noble-security,noble-security 1.8.0+git20170215.a14b5a3-4ubuntu0.24.04.3 all
  Bindings for the LMDB C library

htslib-test/noble,noble 1.19+ds-1.1build3 all
  Test data for HTSlib

irony-server/noble 1.6.0-2build2 amd64
  Emacs C/C++ minor mode powered by libclang (server)

iwyu/noble 8.21-1build2 amd64
  Analyze #includes in C and C++ source files

libbio-db-hts-perl/noble 3.01-4build3 amd64
  Perl interface to the HTS library

libclang-14-dev/noble 1:14.0.6-19build4 amd64
  Clang library - Development package

libclang-15-dev/noble 1:15.0.7-14build3 amd64
  Clang library - Development package

libclang-16-dev/noble 1:16.0.6-23ubuntu4 amd64
  Clang library - Development package

libclang-17-dev/noble 1:17.0.6-9ubuntu1 amd64
  Clang library - Development package

libclang-18-dev/noble-updates 1:18.1.3-1ubuntu1 amd64
  Clang library - Development package

libclang-19-dev/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  Clang library - Development package

libclang-common-14-dev/noble 1:14.0.6-19build4 amd64
  Clang library - Common development package

libclang-common-15-dev/noble 1:15.0.7-14build3 amd64
  Clang library - Common development package

libclang-common-16-dev/noble,now 1:16.0.6-23ubuntu4 amd64 [installed,automatic]
  Clang library - Common development package

libclang-common-17-dev/noble 1:17.0.6-9ubuntu1 amd64
  Clang library - Common development package

libclang-common-18-dev/noble-updates 1:18.1.3-1ubuntu1 amd64
  Clang library - Common development package

libclang-common-19-dev/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  Clang library - Common development package

libclang-cpp-dev/noble 1:18.0-59~exp2 amd64
  C++ interface to the clang library

libclang-cpp14-dev/noble 1:14.0.6-19build4 amd64
  C++ interface to the Clang library

libclang-cpp14t64/noble 1:14.0.6-19build4 amd64
  C++ interface to the Clang library

libclang-cpp15-dev/noble 1:15.0.7-14build3 amd64
  C++ interface to the Clang library

libclang-cpp15t64/noble 1:15.0.7-14build3 amd64
  C++ interface to the Clang library

libclang-cpp16-dev/noble 1:16.0.6-23ubuntu4 amd64
  C++ interface to the Clang library

libclang-cpp16t64/noble,now 1:16.0.6-23ubuntu4 amd64 [installed,automatic]
  C++ interface to the Clang library

libclang-cpp17-dev/noble 1:17.0.6-9ubuntu1 amd64
  C++ interface to the Clang library

libclang-cpp17t64/noble 1:17.0.6-9ubuntu1 amd64
  C++ interface to the Clang library

libclang-cpp18/noble-updates,now 1:18.1.3-1ubuntu1 amd64 [installed,automatic]
  C++ interface to the Clang library

libclang-cpp18-dev/noble-updates 1:18.1.3-1ubuntu1 amd64
  C++ interface to the Clang library

libclang-cpp19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  C++ interface to the Clang library

libclang-cpp19-dev/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  C++ interface to the Clang library

libclang-dev/noble 1:18.0-59~exp2 amd64
  clang library - Development package

libclang-perl/noble 0.09-6build4 amd64
  Perl bindings to the Clang compiler's indexing interface

libclang-rt-14-dev/noble 1:14.0.6-19build4 amd64
  Compiler-rt - development package

libclang-rt-14-dev-wasm32/noble,noble 1:14.0.6-19build4 all
  Compiler-rt - wasm32 builtins

libclang-rt-14-dev-wasm64/noble,noble 1:14.0.6-19build4 all
  Compiler-rt - wasm64 builtins

libclang-rt-15-dev/noble 1:15.0.7-14build3 amd64
  Compiler-rt - development package

libclang-rt-15-dev-wasm32/noble,noble 1:15.0.7-14build3 all
  Compiler-rt - wasm32 builtins

libclang-rt-15-dev-wasm64/noble,noble 1:15.0.7-14build3 all
  Compiler-rt - wasm64 builtins

libclang-rt-16-dev/noble,now 1:16.0.6-23ubuntu4 amd64 [installed,automatic]
  Compiler-rt - development package

libclang-rt-16-dev-wasm32/noble,noble 1:16.0.6-23ubuntu4 all
  Compiler-rt - wasm32 builtins

libclang-rt-16-dev-wasm64/noble,noble 1:16.0.6-23ubuntu4 all
  Compiler-rt - wasm64 builtins

libclang-rt-17-dev/noble 1:17.0.6-9ubuntu1 amd64
  Compiler-rt - development package

libclang-rt-17-dev-wasm32/noble,noble 1:17.0.6-9ubuntu1 all
  Compiler-rt - wasm32 builtins

libclang-rt-17-dev-wasm64/noble,noble 1:17.0.6-9ubuntu1 all
  Compiler-rt - wasm64 builtins

libclang-rt-18-dev/noble-updates 1:18.1.3-1ubuntu1 amd64
  Compiler-rt - development package

libclang-rt-18-dev-wasm32/noble-updates,noble-updates 1:18.1.3-1ubuntu1 all
  Compiler-rt - wasm32 builtins

libclang-rt-18-dev-wasm64/noble-updates,noble-updates 1:18.1.3-1ubuntu1 all
  Compiler-rt - wasm64 builtins

libclang-rt-19-dev/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  Compiler-rt - development package

libclang-rt-19-dev-wasm32/noble-updates,noble-updates 1:19.1.1-1ubuntu1~24.04.2 all
  Compiler-rt - wasm32 builtins

libclang-rt-19-dev-wasm64/noble-updates,noble-updates 1:19.1.1-1ubuntu1~24.04.2 all
  Compiler-rt - wasm64 builtins

libclang-rt-dev/noble 1:18.0-59~exp2 amd64
  Compiler-rt - Development package

libclang-rt-dev-wasm32/noble 1:18.0-59~exp2 amd64
  Compiler-rt - wasm32 builtins

libclang-rt-dev-wasm64/noble 1:18.0-59~exp2 amd64
  Compiler-rt - wasm64 builtins

libclang1/noble 1:18.0-59~exp2 amd64
  C, C++ and Objective-C compiler (LLVM based)

libclang1-14t64/noble 1:14.0.6-19build4 amd64
  C interface to the Clang library

libclang1-15t64/noble 1:15.0.7-14build3 amd64
  C interface to the Clang library

libclang1-16t64/noble,now 1:16.0.6-23ubuntu4 amd64 [installed,automatic]
  C interface to the Clang library

libclang1-17t64/noble 1:17.0.6-9ubuntu1 amd64
  C interface to the Clang library

libclang1-18/noble-updates,now 1:18.1.3-1ubuntu1 amd64 [installed,automatic]
  C interface to the Clang library

libclang1-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  C interface to the Clang library

libcode-tidyall-plugin-clangformat-perl/noble,noble 0.0.2-2 all
  module to run clang-format using Code::TidyAll

libear/noble 3.1.3-1build4 amd64
  generate compilation database for Clang tooling (wrapper library)

libghc-trifecta-dev/noble 2.1.3-1 amd64
  modern parser combinator library with convenient diagnostics

libghc-trifecta-doc/noble,noble 2.1.3-1 all
  modern parser combinator library with convenient diagnostics; documentation

libghc-trifecta-prof/noble 2.1.3-1 amd64
  modern parser combinator library with convenient diagnostics; profiling libraries

libhts-dev/noble 1.19+ds-1.1build3 amd64
  development files for the HTSlib

libhts3t64/noble 1.19+ds-1.1build3 amd64
  C library for high-throughput sequencing data formats

liblldb-14-dev/noble 1:14.0.6-19build4 amd64
  Next generation, high-performance debugger, header files

liblldb-14t64/noble 1:14.0.6-19build4 amd64
  Next generation, high-performance debugger, library

liblldb-15-dev/noble 1:15.0.7-14build3 amd64
  Next generation, high-performance debugger, header files

liblldb-15t64/noble 1:15.0.7-14build3 amd64
  Next generation, high-performance debugger, library

liblldb-16-dev/noble 1:16.0.6-23ubuntu4 amd64
  Next generation, high-performance debugger, header files

liblldb-16t64/noble 1:16.0.6-23ubuntu4 amd64
  Next generation, high-performance debugger, library

liblldb-17-dev/noble 1:17.0.6-9ubuntu1 amd64
  Next generation, high-performance debugger, header files

liblldb-17t64/noble 1:17.0.6-9ubuntu1 amd64
  Next generation, high-performance debugger, library

liblldb-18/noble-updates 1:18.1.3-1ubuntu1 amd64
  Next generation, high-performance debugger, library

liblldb-18-dev/noble-updates 1:18.1.3-1ubuntu1 amd64
  Next generation, high-performance debugger, header files

liblldb-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  Next generation, high-performance debugger, library

liblldb-19-dev/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  Next generation, high-performance debugger, header files

liblldb-dev/noble 1:18.0-59~exp2 amd64
  Next generation, high-performance debugger, header files

libopencl-clang-14-dev/noble 14.0.0-4build2 amd64
  thin wrapper for clang -- development files

libopencl-clang-15-dev/noble 15.0.0-3build2 amd64
  thin wrapper for clang -- development files

libopencl-clang-16-dev/noble 16.0.0-3.1build3 amd64
  thin wrapper for clang -- development files

libopencl-clang-17-dev/noble 17.0.0-2.1build2 amd64
  thin wrapper for clang -- development files

libopencl-clang14/noble 14.0.0-4build2 amd64
  thin wrapper for clang

libopencl-clang15/noble 15.0.0-3build2 amd64
  thin wrapper for clang

libopencl-clang16t64/noble 16.0.0-3.1build3 amd64
  thin wrapper for clang

libopencl-clang17t64/noble 17.0.0-2.1build2 amd64
  thin wrapper for clang

libparallel-hashmap-dev/noble,noble 1.3.12+ds-1 all
  header-only hash map implementation

librust-clang-sys+libloading-dev/noble 1.3.0-2 amd64
  Rust bindings for libclang - feature "libloading" and 1 more

librust-clang-sys-dev/noble 1.3.0-2 amd64
  Rust bindings for libclang - Rust source code

libshaderc-dev/noble 2023.8-1build1 amd64
  Library API for accessing glslc functionality - static libraries and headers

libshaderc1/noble 2023.8-1build1 amd64
  Library API for accessing glslc functionality - shared libraries

libsimde-dev/noble,noble 0.7.2-6 all
  Implementations of SIMD instructions for all systems

lldb/noble 1:18.0-59~exp2 amd64
  Next generation, high-performance debugger

lldb-14/noble 1:14.0.6-19build4 amd64
  Next generation, high-performance debugger

lldb-15/noble 1:15.0.7-14build3 amd64
  Next generation, high-performance debugger

lldb-16/noble 1:16.0.6-23ubuntu4 amd64
  Next generation, high-performance debugger

lldb-17/noble 1:17.0.6-9ubuntu1 amd64
  Next generation, high-performance debugger

lldb-18/noble-updates 1:18.1.3-1ubuntu1 amd64
  Next generation, high-performance debugger

lldb-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  Next generation, high-performance debugger

python3-clang/noble 1:18.0-59~exp2 amd64
  Clang Python Bindings

python3-clang-14/noble 1:14.0.6-19build4 amd64
  Clang Python Bindings

python3-clang-15/noble 1:15.0.7-14build3 amd64
  Clang Python Bindings

python3-clang-16/noble 1:16.0.6-23ubuntu4 amd64
  Clang Python Bindings

python3-clang-17/noble 1:17.0.6-9ubuntu1 amd64
  Clang Python Bindings

python3-clang-18/noble-updates 1:18.1.3-1ubuntu1 amd64
  Clang Python Bindings

python3-clang-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  Clang Python Bindings

python3-lldb/noble 1:18.0-59~exp2 amd64
  Next generation, high-performance debugger, python lib

python3-lldb-14/noble 1:14.0.6-19build4 amd64
  Next generation, high-performance debugger, python3 lib

python3-lldb-15/noble 1:15.0.7-14build3 amd64
  Next generation, high-performance debugger, python3 lib

python3-lldb-16/noble 1:16.0.6-23ubuntu4 amd64
  Next generation, high-performance debugger, python3 lib

python3-lldb-17/noble 1:17.0.6-9ubuntu1 amd64
  Next generation, high-performance debugger, python3 lib

python3-lldb-18/noble-updates 1:18.1.3-1ubuntu1 amd64
  Next generation, high-performance debugger, python3 lib

python3-lldb-19/noble-updates 1:19.1.1-1ubuntu1~24.04.2 amd64
  Next generation, high-performance debugger, python3 lib

rtags/noble 2.38-10 amd64
  C/C++ client/server indexer with integration for Emacs

ruby-whitequark-parser/noble,noble 3.1.3.0-1 all
  Ruby parser written in pure Ruby

sc3-plugins-language/noble,noble 3.9.1~repack-4build1 all
  sclang class files of UGen plugins for SuperCollider

texlive-science/noble,noble 2023.20240207-1 all
  TeX Live: Mathematics, natural sciences, computer science packages

vim-youcompleteme/noble,noble 0+20231230+git71166ea+ds-2 all
  fast, as-you-type, fuzzy-search code completion engine for Vim

wasi-libc/noble,noble 0.0~git20230113.4362b18-3 all
  WASI libc implementation for WebAssembly

zmk/noble,noble 0.5.1-2 all
  collection of reusable Makefiles

zmk-doc/noble,noble 0.5.1-2 all
  collection of reusable Makefiles (manual pages)

于是可以使用``命令安装clang-16

shell 复制代码
sudo apt install clang-16

同样的,安装完之后,查询下版本号

shell 复制代码
john@john-virtual-machine:~/WorkSpace/GithubProjects/ninja$ clang-16 --version
Ubuntu clang version 16.0.6 (23ubuntu4)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

注意到,此时我们输入的命令是clang-16而不是clang,要直接使用clang需要去/usr/bin目录下加入软链接:

shell 复制代码
$ sudo ln -s ../lib/llvm-16/bin/clang clang
$ sudo ln -s ../lib/llvm-16/bin/clang++ clang++

注意,如果系统本身安装过旧版本的clang,可能会提示failed to create symbolic link 'clang': File exists,此时要么rm -rf clang删除, 要么把上述命令改为ln -snf ../lib/llvm-16/bin/clang clang

修改之后,如下所示,此时再用clang-16就只需要输入clang即可

shell 复制代码
john@john-virtual-machine:/usr/bin$ ll|grep clang
lrwxrwxrwx  1 root root          24  7月 30 20:21 clang -> ../lib/llvm-16/bin/clang*
lrwxrwxrwx  1 root root          26  7月 30 20:22 clang++ -> ../lib/llvm-16/bin/clang++*
lrwxrwxrwx  1 root root          26  4月 14  2024 clang++-16 -> ../lib/llvm-16/bin/clang++*
lrwxrwxrwx  1 root root          24  4月 14  2024 clang-16 -> ../lib/llvm-16/bin/clang*
lrwxrwxrwx  1 root root          28  4月 14  2024 clang-cpp-16 -> ../lib/llvm-16/bin/clang-cpp*

查看clang版本号:

shell 复制代码
ccf@ccf-virtual-machine:~$ clang --version
Ubuntu clang version 16.0.6 (23ubuntu4)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

3. 安装 GN 构建系统

gn的源码可以从两个地方下载,google官方的需要科学上网,另一个就是GitHub上有fork的仓库,GitHub也上不去的话,就用鸿蒙提供的Gitee的仓库

shell 复制代码
git clone https://github.com/timniederhausen/gn.git
cd gn
python build/gen.py # --allow-warning if you want to build with warnings.
ninja -C out
# To run tests:
out/gn_unittests

等待编译结束,跑一下gn的测试代码,显示passed说明gn功能没问题

shell 复制代码
john@john-virtual-machine:~/WorkSpace/GithubProjects/gn$ out/gn_unittests 
[721/721] ImportantFileWriterTest.Basic
PASSED

编译成功后,将gn复制到/usr/bin目录下

复制代码
john@john-virtual-machine:~/WorkSpace/GithubProjects/gn$ ls
AUTHORS  build  docs  examples  infra  LICENSE  misc  out  OWNERS  README.md  src  tools
ccf@ccf-virtual-machine:~/WorkSpace/GithubProjects/gn$ sudo cp out/gn /usr/bin/

接着在任意终端中,输入gn help检测gn是否安装成功

shell 复制代码
john@john-virtual-machine:~/WorkSpace/GithubProjects/gn$ gn help

Commands (type "gn help <command>" for more help):
  analyze: Analyze which targets are affected by a list of files.
  args: Display or configure arguments declared by the build.
  check: Check header dependencies.
  clean: Cleans the output directory.
  clean_stale: Cleans the stale output files from the output directory.
  desc: Show lots of insightful information about a target or config.
  format: Format .gn files.
  gen: Generate ninja files.
  help: Does what you think.
  ls: List matching targets.
  meta: List target metadata collection results.
  outputs: Which files a source/target make.
  path: Find paths between two targets.
  refs: Find stuff referencing a target or file.

Target declarations (type "gn help <function>" for more help):
  action: Declare a target that runs a script a single time.
  action_foreach: Declare a target that runs a script over a set of files.
  bundle_data: [iOS/macOS] Declare a target without output.
  copy: Declare a target that copies files.
  create_bundle: [iOS/macOS] Build an iOS or macOS bundle.
  executable: Declare an executable target.
  generated_file: Declare a generated_file target.
  group: Declare a named group of targets.
  loadable_module: Declare a loadable module target.
  rust_library: Declare a Rust library target.
  rust_proc_macro: Declare a Rust procedural macro target.
  shared_library: Declare a shared library target.
  source_set: Declare a source set target.
  static_library: Declare a static library target.
  target: Declare a target with the given programmatic type.

Buildfile functions (type "gn help <function>" for more help):
  assert: Assert an expression is true at generation time.
  config: Defines a configuration object.
  declare_args: Declare build arguments.
  defined: Returns whether an identifier is defined.
  exec_script: Synchronously run a script and return the output.
  filter_exclude: Remove values that match a set of patterns.
  filter_include: Remove values that do not match a set of patterns.
  filter_labels_exclude: Remove labels that match a set of patterns.
  filter_labels_include: Remove labels that do not match a set of patterns.
  foreach: Iterate over a list.
  forward_variables_from: Copies variables from a different scope.
  get_label_info: Get an attribute from a target's label.
  get_path_info: Extract parts of a file or directory name.
  get_target_outputs: [file list] Get the list of outputs from a target.
  getenv: Get an environment variable.
  import: Import a file into the current scope.
  label_matches: Returns whether a label matches any of a list of patterns.
  not_needed: Mark variables from scope as not needed.
  pool: Defines a pool object.
  print: Prints to the console.
  print_stack_trace: Prints a stack trace.
  process_file_template: Do template expansion over a list of files.
  read_file: Read a file into a variable.
  rebase_path: Rebase a file or directory to another location.
  set_default_toolchain: Sets the default toolchain name.
  set_defaults: Set default values for a target type.
  split_list: Splits a list into N different sub-lists.
  string_join: Concatenates a list of strings with a separator.
  string_replace: Replaces substring in the given string.
  string_split: Split string into a list of strings.
  template: Define a template rule.
  tool: Specify arguments to a toolchain tool.
  toolchain: Defines a toolchain.
  write_file: Write a file to disk.

Built-in predefined variables (type "gn help <variable>" for more help):
  current_cpu: [string] The processor architecture of the current toolchain.
  current_os: [string] The operating system of the current toolchain.
  current_toolchain: [string] Label of the current toolchain.
  default_toolchain: [string] Label of the default toolchain.
  gn_version: [number] The version of gn.
  host_cpu: [string] The processor architecture that GN is running on.
  host_os: [string] The operating system that GN is running on.
  invoker: [string] The invoking scope inside a template.
  python_path: [string] Absolute path of Python.
  root_build_dir: [string] Directory where build commands are run.
  root_gen_dir: [string] Directory for the toolchain's generated files.
  root_out_dir: [string] Root directory for toolchain output files.
  target_cpu: [string] The desired cpu architecture for the build.
  target_gen_dir: [string] Directory for a target's generated files.
  target_name: [string] The name of the current target.
  target_os: [string] The desired operating system for the build.
  target_out_dir: [string] Directory for target output files.

Variables you set in targets (type "gn help <variable>" for more help):
  aliased_deps: [scope] Set of crate-dependency pairs.
  all_dependent_configs: [label list] Configs to be forced on dependents.
  allow_circular_includes_from: [label list] Permit includes from deps.
  arflags: [string list] Arguments passed to static_library archiver.
  args: [string list] Arguments passed to an action.
  asmflags: [string list] Flags passed to the assembler.
  assert_no_deps: [label pattern list] Ensure no deps on these targets.
  bridge_header: [string] Path to C/Objective-C compatibility header.
  bundle_contents_dir: Expansion of {{bundle_contents_dir}} in create_bundle.
  bundle_deps_filter: [label list] A list of labels that are filtered out.
  bundle_executable_dir: Expansion of {{bundle_executable_dir}} in create_bundle
  bundle_resources_dir: Expansion of {{bundle_resources_dir}} in create_bundle.
  bundle_root_dir: Expansion of {{bundle_root_dir}} in create_bundle.
  cflags: [string list] Flags passed to all C compiler variants.
  cflags_c: [string list] Flags passed to the C compiler.
  cflags_cc: [string list] Flags passed to the C++ compiler.
  cflags_objc: [string list] Flags passed to the Objective C compiler.
  cflags_objcc: [string list] Flags passed to the Objective C++ compiler.
  check_includes: [boolean] Controls whether a target's files are checked.
  code_signing_args: [string list] [deprecated] Args for the post-processing script.
  code_signing_outputs: [file list] [deprecated] Outputs of the post-processing step.
  code_signing_script: [file name] [deprecated] Script for the post-processing step.
  code_signing_sources: [file list] [deprecated] Sources for the post-processing step.
  complete_static_lib: [boolean] Links all deps into a static library.
  configs: [label list] Configs applying to this target or config.
  contents: Contents to write to file.
  crate_name: [string] The name for the compiled crate.
  crate_root: [string] The root source file for a binary or library.
  crate_type: [string] The type of linkage to use on a shared_library.
  data: [file list] Runtime data file dependencies.
  data_deps: [label list] Non-linked dependencies.
  data_keys: [string list] Keys from which to collect metadata.
  defines: [string list] C preprocessor defines.
  depfile: [string] File name for input dependencies for actions.
  deps: [label list] Private linked dependencies.
  description: [string] Command description for actions.
  externs: [scope] Set of Rust crate-dependency pairs.
  framework_dirs: [directory list] Additional framework search directories.
  frameworks: [name list] Name of frameworks that must be linked.
  friend: [label pattern list] Allow targets to include private headers.
  gen_deps: [label list] Declares targets that should generate when this one does.
  include_dirs: [directory list] Additional include directories.
  inputs: [file list] Additional compile-time dependencies.
  ldflags: [string list] Flags passed to the linker.
  lib_dirs: [directory list] Additional library directories.
  libs: [string list] Additional libraries to link.
  metadata: [scope] Metadata of this target.
  mnemonic: [string] Prefix displayed when ninja runs this action.
  module_name: [string] The name for the compiled module.
  output_conversion: Data format for generated_file targets.
  output_dir: [directory] Directory to put output file in.
  output_extension: [string] Value to use for the output's file extension.
  output_name: [string] Name for the output file other than the default.
  output_prefix_override: [boolean] Don't use prefix for output name.
  outputs: [file list] Output files for actions and copy targets.
  partial_info_plist: [filename] Path plist from asset catalog compiler.
  pool: [string] Label of the pool used by binary targets and actions.
  post_processing_args: [string list] Args for the post-processing script.
  post_processing_outputs: [file list] Outputs of the post-processing step.
  post_processing_script: [file name] Script for the post-processing step.
  post_processing_sources: [file list] Sources for the post-processing step.
  precompiled_header: [string] Header file to precompile.
  precompiled_header_type: [string] "gcc" or "msvc".
  precompiled_source: [file name] Source file to precompile.
  product_type: [string] Product type for the bundle.
  public: [file list] Declare public header files for a target.
  public_configs: [label list] Configs applied to dependents.
  public_deps: [label list] Declare public dependencies.
  rebase: [boolean] Rebase collected metadata as files.
  response_file_contents: [string list] Contents of .rsp file for actions.
  rustflags: [string list] Flags passed to the Rust compiler.
  script: [file name] Script file for actions.
  sources: [file list] Source files for a target.
  swiftflags: [string list] Flags passed to the swift compiler.
  testonly: [boolean] Declares a target must only be used for testing.
  transparent: [bool] True if the bundle is transparent.
  visibility: [label list] A list of labels that can depend on a target.
  walk_keys: [string list] Key(s) for managing the metadata collection walk.
  weak_frameworks: [name list] Name of frameworks that must be weak linked.
  write_runtime_deps: Writes the target's runtime_deps to the given path.
  xcasset_compiler_flags: [string list] Flags passed to xcassets compiler
  xcode_extra_attributes: [scope] Extra attributes for Xcode projects.
  xcode_test_application_name: [string] Name for Xcode test target.

Other help topics:
  all: Print all the help at once
  buildargs: How build arguments work.
  dotfile: Info about the toplevel .gn file.
  execution: Build graph and execution overview.
  grammar: Language and grammar for GN build files.
  input_conversion: Processing input from exec_script and read_file.
  file_pattern: Matching more than one file.
  label_pattern: Matching more than one label.
  labels: About labels.
  metadata_collection: About metadata and its collection.
  ninja_rules: How Ninja build rules are named.
  nogncheck: Annotating includes for checking.
  output_conversion: Specifies how to transform a value to output.
  runtime_deps: How runtime dependency computation works.
  source_expansion: Map sources to outputs for scripts.
  switches: Show available command-line switches.

gnninja编译示例程序

在gn仓库中,自带一个例程,路径为gn/example/simple_build

shell 复制代码
john@john-virtual-machine:~/WorkSpace/GithubProjects/gn$ ls
AUTHORS  build  docs  examples  infra  LICENSE  misc  out  OWNERS  README.md  src  tools
复制代码
ccf@ccf-virtual-machine:~/WorkSpace/GithubProjects/gn$ tree -L 2
.
├── AUTHORS
├── build
│   ├── build_aix.ninja.template
│   ├── build_haiku.ninja.template
│   ├── build_linux.ninja.template
│   ├── build_mac.ninja.template
│   ├── build_openbsd.ninja.template
│   ├── build_win.ninja.template
│   ├── build_zos.ninja.template
│   ├── full_test.py
│   ├── gen.py
│   └── windows.manifest.xml
├── docs
│   ├── cross_compiles.md
│   ├── faq.md
│   ├── language.md
│   ├── mingw.md
│   ├── quick_start.md
│   ├── reference.md
│   ├── standalone.md
│   └── style_guide.md
├── examples
│   ├── ios
│   ├── rust_example
│   └── simple_build
├── infra
│   ├── config
│   ├── README.recipes.md
│   ├── recipe_modules
│   ├── recipes
│   └── recipes.py
├── LICENSE
├── misc
│   ├── emacs
│   ├── help_as_html.py
│   ├── tm
│   └── vim
├── out
│   ├── base.a
│   ├── build.ninja
│   ├── build.ninja.d
│   ├── Debug
│   ├── gn
│   ├── gn_lib.a
│   ├── gn_unittests
│   ├── last_commit_position.h
│   └── src
├── OWNERS
├── README.md
├── src
│   ├── base
│   ├── gn
│   └── util
└── tools
    ├── find_unreachable.py
    ├── run_formatter.sh
    └── update_reference.sh

23 directories, 35 files
shell 复制代码
$ cd ./example/simple_build   #已在gn仓库目录下
$ gn gen out
Done. Made 3 targets from 4 files in 61ms
$ ninja -C out
ninja: Entering directory `out'
[6/6] LINK hello

最后运行我们通过gnninja编译的程序

shell 复制代码
john@john-virtual-machine:~/WorkSpace/GithubProjects/gn/examples/simple_build$ ./out/hello 
Hello, world

至此,我们可以愉快地使用gn来编译代码啦。

我的第一个简单的GN C++项目

使用GN构建的一个C++小项目,

该项目包含一个可执行程序,依赖于静态库 A 和动态库 B。

首先,让我们设计项目结构:

复制代码
john@john-virtual-machine:~/WorkSpace/CppProjects/GiteeProjects/my-first-gn-project$ tree -L 3
.
├── build
│   ├── BUILDCONFIG.gn
│   ├── BUILD.gn
│   └── toolchain
│       └── BUILD.gn
├── BUILD.gn    # 主构建文件
├── libA    # 静态库A
│   ├── a.cpp
│   ├── a.h
│   └── BUILD.gn
├── libB    # 动态库B
│   ├── b.cpp
│   ├── b.h
│   └── BUILD.gn
├── LICENSE
├── main.cpp    # 主程序
├── out
│   └── Default
│       ├── args.gn
│       ├── build.ninja
│       ├── build.ninja.d
│       ├── build.ninja.stamp
│       ├── libB.so
│       ├── my_app
│       ├── obj
│       └── toolchain.ninja
└── README.md

8 directories, 20 files

代码已上传到gitee码云和github上,对应地址为:

1. 构建项目

shell 复制代码
# 创建输出目录
mkdir -p out/Default

# 生成Ninja构建文件
gn gen out/Default

# 执行构建
ninja -C out/Default

2. 运行生成的可执行程序:

shell 复制代码
# 设置动态库路径
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(pwd)/out/Default/libB

# 运行程序
./out/Default/my_app

运行成功后,您应该看到以下输出:

shell 复制代码
Starting main function...
This is functionA from static library A
This is functionB from dynamic library B
Ending main function.

参考资料

相关推荐
寻月隐君2 小时前
Rust Scoped Threads 实战:更安全、更简洁的并发编程
后端·rust·github
每天敲200行代码3 小时前
Redis 初识Redis
数据库·redis·github
狂炫一碗大米饭3 小时前
如何在 Git 中检出远程分支
前端·git·github
pe7er4 小时前
使用 ineo6/hosts + SwitchHosts 提高 GitHub 国内访问速度
github
小蜗牛狂飙记5 小时前
github-idea新建文件就要弹窗提醒-如何关闭-2025.7.30
java·github·intellij-idea
大前端之旅8 小时前
GitCode的一些命令
github
海风极客9 小时前
又开发了一个优雅的小工具
后端·github
2301_7616455419 小时前
GitHub 热门项目 PandaWiki:零门槛搭建智能漏洞库,支持 10 + 大模型接入
github
不搞学术柒柒20 小时前
vscode、cursor无密码ssh远程连接服务器(配置密钥)
服务器·ssh·github