编译顺序 [color=rgba(0, 0, 0, 0.87)]在正式开始程序编写之前,有必要先整体上了解程序的编译顺序。 [color=rgba(0, 0, 0, 0.87)]Harmony的编译路径主要由vendor/xradio/xr806/config.json决定 code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;"> "product_name": "wifi_skylark", "ohos_version": "OpenHarmony 1.0.1", "device_company": "xradio", "board": "xr806", "kernel_type": "liteos_m",[color=rgba(0, 0, 0, 0.87)]其中"product_name"决定了执行hb set出现的选项,"device_company","board","kernel_type"共同决定了编译路径。 [color=rgba(0, 0, 0, 0.87)]hb set选定wifi_skylark后,会在根目录中生成文件ohos_config.json,里面的内容大致如下: code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">{ "root_path": "/home/workspace/Harmony_XR806", "board": "xr806", "kernel": "liteos_m", "product": "wifi_skylark", "product_path": "/home/workspace/Harmony_XR806/vendor/xradio/xr806", "device_path": "/home/workspace/Harmony_XR806/device/xradio/xr806/liteos_m"}[color=rgba(0, 0, 0, 0.87)]在执行hb build -f后,HarmonyOS的编译脚本会从device_path中获取编译工具和编译选项,从product_path下寻找BUILD.gn文件。BUILD.gn文件内容如下: code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">build_ext_component("libSDK") { exec_path = rebase_path(".", root_build_dir) outdir = rebase_path("$root_out_dir") command = "./build.sh ${outdir}" deps = [ "//kernel/liteos_m:kernel", #(3) "os:liteos_glue", "adapter/hals:adapter", "adapter/console:app_console", "ohosdemo:ohosdemo", #(2) ]}group("xr806") { deps = [ ":libSDK" #(1) ]}[color=rgba(0, 0, 0, 0.87)](1)deps关键字代表执行group("xr806")前需要先执行libSDK,也就是build_ext_component("libSDK")。 [color=rgba(0, 0, 0, 0.87)](2)执行build_ext_component("libSDK")前要求先寻找ohosdemo文件夹下的BUILD.gn,并且BUILD.gn里面会有ohosdemo的定义。 [color=rgba(0, 0, 0, 0.87)](3)“//”代表绝对路径,可以理解为根目录。//kernel/liteos_m代表根目录下的kernel/liteos_m文件夹。 [color=rgba(0, 0, 0, 0.87)]打开ohosdemo/BUILD.gn code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">group("ohosdemo") { deps = [ "hello_demo:app_hello", #(1) #"iot_peripheral:app_peripheral", #"wlan_demo:app_WlanTest", ]}[color=rgba(0, 0, 0, 0.87)](1)“#”是gn文件的注释符号。 [color=rgba(0, 0, 0, 0.87)]打开hello_demo/BUILD.gn code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">import("//device/xradio/xr806/liteos_m/config.gni") #(1)static_library("app_hello") { #(2) configs = [] sources = [ "src/main.c", #(3) ] cflags = board_cflags #(4) include_dirs = board_include_dirs #(5) include_dirs += [ "//kernel/liteos_m/kernel/arch/include", #(6) ]}[color=rgba(0, 0, 0, 0.87)](1).gni文件类似C语言的.h文件,用于定义一些模板 [color=rgba(0, 0, 0, 0.87)](2)static_library代表生成静态库(.a)文件,其中包含main.c的静态库必须是app_打头,如app_hello,否则虽然可以编译成功,但无法生效。 [color=rgba(0, 0, 0, 0.87)](3)需要编译的C文件,代表src文件夹下的main.c文件。 [color=rgba(0, 0, 0, 0.87)](4)gcc编译选项,其中board_cflags在//device/xradio/xr806/liteos_m/config.gni中定义。 [color=rgba(0, 0, 0, 0.87)](5)全局头文件,其中board_include_dirs在//device/xradio/xr806/liteos_m/config.gni中定义。 [color=rgba(0, 0, 0, 0.87)](6)除了board_include_dirs,额外需要添加的头文件。 代码 code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">#include <stdio.h>#include "ohos_init.h" //(2) #include "kernel/os/os.h"static OS_Thread_t g_main_thread;static void MainThread(void *arg){ while (1) { printf("hello world!\n"); LOS_Msleep(1000); }}void HelloTestMain(void){ printf("Wifi Test Start\n"); if (OS_ThreadCreate(&g_main_thread, "MainThread", MainThread, NULL, OS_THREAD_PRIO_APP, 4 * 1024) != OS_OK) { printf("[ERR] Create MainThread Failed\n"); }}SYS_RUN(HelloTestMain); //(1)[color=rgba(0, 0, 0, 0.87)](1)Harmony启动时会调用所有SYS_RUN,上文中提到的应用库必须用“app_ ”打头就是因为SYS_RUN不会加入ld链接,所以使用"app_"标识,强制链接。 [color=rgba(0, 0, 0, 0.87)](2)必须添加ohos_init.h头文件,否则SYS_RUN失效。 演示[color=rgba(0, 0, 0, 0.87)]XR806_OpenHarmony串口默认配置为:波特率115200,无校验,8位数据位,1位停止位。 [color=rgba(0, 0, 0, 0.87)]开发板成功连接PC上对串口调试助手后,按下开发板的复位按键,串口输出如下。 [color=rgba(0, 0, 0, 0.87)] [color=var(--md-typeset-a-color)]
|