BUILD.gn文件怎么写,Gn + Ninja编译一个Hello world程序的例子Demo

  • 这是一个简单的BUILD.gn配置文件
jim@ubuntu:~/0_Git/third_party_gn/examples/simple_build$ cat 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.

executable("hello") {
  sources = [ "hello.cc" ]

  deps = [
    ":hello_shared",
    ":hello_static",
  ]
}

shared_library("hello_shared") {
  sources = [
    "hello_shared.cc",
    "hello_shared.h",
  ]

  defines = [ "HELLO_SHARED_IMPLEMENTATION" ]
}

static_library("hello_static") {
  sources = [
    "hello_static.cc",
    "hello_static.h",
  ]
}
  • 这是Gn官方的例子,在Gn的源码里面

    • 使用Ubuntu系统,安装Git、Ninja、Gn、Clang软件
    • git clone git@gitee.com:openharmony/third_party_gn.git 拉取源码,或者:
    • https://gitee.com/openharmony/third_party_gn/repository/archive/master.zip 下载源码
  • cd examples/simple_build/ 下载源码后进入其中的子目录

  • ls 先看看里面的文件

jim@ubuntu:~/0_Git/third_party_gn/examples/simple_build$ ls
build  BUILD.gn  hello.cc  hello_shared.cc  hello_shared.h  hello_static.cc  hello_static.h  README.md  tutorial

jim@ubuntu:~/0_Git/third_party_gn/examples/simple_build$ cat hello.cc
// 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.

#include <stdio.h>

#include "hello_shared.h"
#include "hello_static.h"

int main(int argc, char* argv[]) {
  printf("%s, %s\n", GetStaticText(), GetSharedText());
  return 0;
}
  • gn gen -C out 按Gn规则文件生成Ninja配置文件
jim@ubuntu:~/0_Git/third_party_gn/examples/simple_build$ gn gen -C out
Done. Made 3 targets from 4 files in 45ms
  • cd out/ 进入输出目录,看看生成的什么内容
jim@ubuntu:~/0_Git/third_party_gn/examples/simple_build$ tree out/
out/
├── args.gn
├── build.ninja
├── build.ninja.d
├── obj
│   ├── hello.ninja
│   ├── hello_shared.ninja
│   └── hello_static.ninja
└── toolchain.ninja

1 directory, 7 files


jim@ubuntu:~/0_Git/third_party_gn/examples/simple_build$ cat out/build.ninja
ninja_required_version = 1.7.2

rule gn
  command = ../../../../../../../usr/bin/gn --root=./.. -q --C --regeneration gen .
  pool = console
  description = Regenerating ninja files

build build.ninja: gn
  generator = 1
  depfile = build.ninja.d

subninja toolchain.ninja

build hello_shared: phony ./libhello_shared.so
build hello_static: phony obj/libhello_static.a
build $:hello: phony hello
build $:hello_shared: phony ./libhello_shared.so
build $:hello_static: phony obj/libhello_static.a

build all: phony $
    hello $
    ./libhello_shared.so $
    obj/libhello_static.a

default all
  • ninja 开始编译(和make命令类似)
jim@ubuntu:~/0_Git/third_party_gn/examples/simple_build/out$ ninja
[6/6] LINK hello

jim@ubuntu:~/0_Git/third_party_gn/examples/simple_build/out$ tree
.
├── args.gn
├── build.ninja
├── build.ninja.d
├── hello
├── libhello_shared.so
├── obj
│   ├── hello.hello.o
│   ├── hello.ninja
│   ├── hello_shared.ninja
│   ├── hello_static.ninja
│   ├── libhello_shared.hello_shared.o
│   ├── libhello_static.a
│   └── libhello_static.hello_static.o
└── toolchain.ninja

1 directory, 13 files
  • ./hello 运行编译后的可执行文件
jim@ubuntu:~/0_Git/third_party_gn/examples/simple_build/out$ ./hello 
Hello, world

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:

如何快速入门:https://qr21.cn/FV7h05

  1. 基本概念
  2. 构建第一个ArkTS应用
  3. ……

开发基础知识:https://qr21.cn/FV7h05

  1. 应用基础知识
  2. 配置文件
  3. 应用数据管理
  4. 应用安全管理
  5. 应用隐私保护
  6. 三方应用调用管控机制
  7. 资源分类与访问
  8. 学习ArkTS语言
  9. ……

基于ArkTS 开发:https://qr21.cn/FV7h05

  1. Ability开发
  2. UI开发
  3. 公共事件与通知
  4. 窗口管理
  5. 媒体
  6. 安全
  7. 网络与链接
  8. 电话服务
  9. 数据管理
  10. 后台任务(Background Task)管理
  11. 设备管理
  12. 设备使用信息统计
  13. DFX
  14. 国际化开发
  15. 折叠屏系列
  16. ……

鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH

鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH

1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值