文章目录
1. 前言✨
本文通过一个简单的C语言工程构建实例,主要描述通过GN和ninjia如何构建、编译。
各位小伙伴,如果你是刚接触GN、ninja构建,这个实例工程非常适合你参考、学习,该实例包括静态库、动态库的编译、链接,还有对编译工具链的配置,编译输出的配置等,如果对你有帮助,请点赞👍👍👍、转发,评论&交流😊。💐💐💐🎉🎉🎉
2. 工程实例🚩
2.1 工程目录结构
.
├── BUILD.gn
├── README.md
├── build
│ ├── BUILD.gn
│ ├── BUILDCONFIG.gn
│ └── toolchains
│ └── BUILD.gn
├── include
│ └── utils.h
├── lib
│ ├── shared
│ │ ├── BUILD.gn
│ │ └── dlib.c
│ └── static
│ ├── BUILD.gn
│ └── slib.c
├── out
│ └── Default
│ ├── args.gn
│ ├── build.ninja
│ ├── build.ninja.d
│ ├── build.ninja.stamp
│ ├── libshared.so
│ ├── main
│ ├── obj
│ └── toolchain.ninja
├── src
│ └── main
│ ├── BUILD.gn
│ └── main.c
└── third_party
13 directories, 19 files
2.2 工程顶层.gn文件
BUILD.gn
# 顶层group定义
group("all") {
deps = [
"//lib/static",
"//lib/shared",
"//src/main"
]
}
2.3 工具链配置.gn文件
build/toolchains/BUILD.gn
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
toolchain("gcc") {
tool("cc") {
depfile = "{
{output}}.d"
command = "gcc -MMD -MF $depfile {
{defines}} {
{include_dirs}} {
{cflags}} {
{cflags_c}} -c {
{source}} -o {
{output}}"
depsformat = "gcc"
description = "CC {
{output}}"
outputs = [
"{
{source_out_dir}}/{
{target_output_name}}.{
{source_name_part}}.o",
]
}
tool("cxx") {
depfile = "{
{output}}.d"
command = "g++ -MMD -MF $depfile {
{defines}} {
{include_dirs}} {
{cflags}} {
{cflags_cc}} -c {
{source}} -o {
{output}}"
depsformat = "gcc"
description = "CXX {
{output}}"
outputs = [
"{
{source_out_dir}}/{
{target_output_name}}.{
{source_name_part}}.o",
]
}
tool("alink") {
rspfile = "{
{output}}.rsp"
command =