跟着小白一起学鸿蒙之第一个OpenHarmony程序

系统 OpenHarmony
今天我们来熟悉下OpenHarmony的编译框架和如果自己开发一个HelloWord程序。

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​

今天我们来熟悉下OpenHarmony的编译框架和如果自己开发一个HelloWord程序。

简介

OpenHarmony的编译过程分析

graph LR
build.sh --> build.py --> hb --> gn
hb --> ninja
hb --> clang

#跟着小白一起学鸿蒙# [二]第一个OpenHarmony程序-开源基础软件社区

名词解释

gn: generate ninja工具,在out目录下生成ninja编译文件*.ninja,文件位置在prebuilts/build-tools/linux-x86目录里。

$ ./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.
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.
path: Find paths between two targets.
refs: Find stuff referencing a target or file.
……

ninja:构建工具,根据gn生成的*.ninja文件进行编译构建,文件位置在prebuilts/build-tools/linux-x86目录里。

./ninja --help
usage: ninja [options] [targets...]
if targets are unspecified, builds the 'default' target (see manual).
options:
--version print ninja version ("1.10.1")
-v, --verbose show all command lines while building
-C DIR change to DIR before doing anything else
-f FILE specify input build file [default=build.ninja]
-j N run N jobs in parallel (0 means infinity) [default=10 on this system]
-k N keep going until N jobs fail (0 means infinity) [default=1]
-l N do not start new jobs if the load average is greater than N
-n dry run (don't run commands but act like they succeeded)
-d MODE enable debugging (use '-d list' to list modes)
-t TOOL run a subtool (use '-t list' to list subtools)
terminates toplevel options; further flags are passed to the tool
-w FLAG adjust warnings (use '-w list' to list warnings)

clang:编译器,同gcc兼容。

1、查看.gn并增加应用输出

路径为:oh32/third_party/libuv/BUILD.gn,找到如下位置增加。

//静态库
ohos_static_library("uv_static") {
deps = [ ":libuv_source" ]
public_configs = [ ":libuv_config" ]
subsystem_name = "thirdparty"
part_name = "libuv"
}
//动态库
ohos_shared_library("uv") {
deps = [ ":libuv_source" ]
public_configs = [ ":libuv_config" ]
subsystem_name = "thirdparty"
part_name = "libuv"
if (is_ohos) {
output_extension = "so"
}
install_images = [
"system",
"updater",
]
}
//增加的新应用
ohos_executable("helloworld") {
sources = [ "helloword.c",]
}
2、增加helloworld.c

在oh32/third_party/libuv目录下增加helloworld.c。

3、增加helloworld编译生成的入口

在test/xts/acts/graphic/BUILD.gn里增加deps,如下:

import("//build/ohos_var.gni")
group("graphic") {
testonly = true
if (is_standard_system) {
deps = [
"webGL:webGL_hap_test",
"windowStage:ActsWindowStageTest",
"windowstandard:window_hap_test",
"//third_party/libuv:helloworld", #引入helloworld程序编译
]
} else {
deps = [
"appaccount:appaccount_hap",
"osaccount:osaccount_hap",
]
}
}

源码根目录执行:

./build.sh --product-name rk3568

源码根目录执行:

./build.sh --product-name rk3568 --gn-args build_xts=true --build-target "acts" --gn-args is_standard_system=true
5、编译完成后生成的第三方库验证程序helloworld使用流程

runtest库文件会输出helloworld,路径为:out/rk3568/common/common。

~/oh32/out/rk3568/common/common$ ls
libc libimagePixelmap.so libnativerender.so libsqlite.z.so libteststring.so libusb_shared.z.so helloworld

使用方式如下:

1. 拷贝helloworld到板子上
hdc_std.exe file send helloworld /data/local/tmp
//2. 登录板子,运行helloworld,如果第一次需要给权限
pc端:hdc_std.exe shell
板子:cd /data/local/tmp
板子:chmod +x helloworld
板子:./helloworld

结果:

#./helloworld
hello world!#

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​​。

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2022-09-30 14:08:26

Hap应用鸿蒙

2022-08-19 19:02:20

开源鸿蒙操作系统

2023-01-03 15:09:10

鸿蒙常用工具

2022-11-29 16:35:02

Tetris鸿蒙

2022-12-02 14:20:09

Tetris鸿蒙

2022-12-06 15:39:16

鸿蒙主干代码

2022-11-22 15:15:46

Wi-Fi鸿蒙

2022-12-05 15:02:14

鸿蒙用户鉴权

2022-11-28 15:42:39

分布式软总线鸿蒙

2022-09-28 13:57:41

鸿蒙开源

2022-09-16 15:22:16

测试套件鸿蒙

2022-11-25 16:48:54

鸿蒙Stage HAP

2022-11-24 14:34:41

Hap程序鸿蒙

2023-04-04 09:24:11

鸿蒙HiDumper

2022-10-10 14:47:04

蓝牙应用鸿蒙

2023-03-30 09:32:27

2022-11-14 17:01:34

游戏开发画布功能

2023-02-27 16:30:32

鸿蒙开源协议分析

2023-03-30 09:19:54

SELinux安全子系统

2023-03-15 16:19:03

BinderIPC工具
点赞
收藏

51CTO技术栈公众号