1# 编写“Hello World”程序 2 3 4下方将展示如何在单板上运行第一个应用程序,其中包括新建应用程序、编译、烧写、运行等步骤,最终输出“Hello World!”。 5 6 7## 示例目录 8 9示例完整目录如下。 10 11 12``` 13applications/sample/hello 14│── BUILD.gn 15│── include 16│ └── helloworld.h 17│── src 18│ └── helloworld.c 19├── bundle.json 20build 21└── subsystem_config.json 22productdefine/common 23└── products 24 └── rk3568.json 25``` 26 27 28## 开发步骤 29 30请在源码目录中通过以下步骤创建“Hello World”应用程序: 31 321. 创建目录,编写业务代码。 33 34 新建applications/sample/hello/src/helloworld.c目录及文件,代码如下所示,用户可以自定义修改打印内容(例如:修改World为OHOS)。其中helloworld.h包含字符串打印函数HelloPrint的声明。当前应用程序可支持标准C及C++的代码开发。 35 36 37 ``` 38 #include <stdio.h> 39 #include "helloworld.h" 40 41 int main(int argc, char **argv) 42 { 43 HelloPrint(); 44 return 0; 45 } 46 47 void HelloPrint() 48 { 49 printf("\n\n"); 50 printf("\n\t\tHello World!\n"); 51 printf("\n\n"); 52 } 53 ``` 54 55 再添加头文件applications/sample/hello/include/helloworld.h,代码如下所示。 56 57 58 ``` 59 #ifndef HELLOWORLD_H 60 #define HELLOWORLD_H 61 #ifdef __cplusplus 62 #if __cplusplus 63 extern "C" { 64 #endif 65 #endif 66 67 void HelloPrint(); 68 69 #ifdef __cplusplus 70 #if __cplusplus 71 } 72 #endif 73 #endif 74 #endif // HELLOWORLD_H 75 ``` 76 772. 新建编译组织文件。 78 79 1. 新建applications/sample/hello/BUILD.gn文件,内容如下所示: 80 81 ``` 82 import("//build/ohos.gni") # 导入编译模板 83 ohos_executable("helloworld") { # 可执行模块 84 sources = [ # 模块源码 85 "src/helloworld.c" 86 ] 87 include_dirs = [ # 模块依赖头文件目录 88 "include" 89 ] 90 cflags = [] 91 cflags_c = [] 92 cflags_cc = [] 93 ldflags = [] 94 configs = [] 95 deps =[] # 部件内部依赖 96 part_name = "hello" # 所属部件名称,必选 97 install_enable = true # 是否默认安装(缺省默认不安装),可选 98 } 99 ``` 100 101 2. 新建applications/sample/hello/bundle.json文件,添加sample部件描述,内容如下所示。 102 103 ``` 104 { 105 "name": "@ohos/hello", 106 "description": "Hello world example.", 107 "version": "3.1", 108 "license": "Apache License 2.0", 109 "publishAs": "code-segment", 110 "segment": { 111 "destPath": "applications/sample/hello" 112 }, 113 "dirs": {}, 114 "scripts": {}, 115 "component": { 116 "name": "hello", 117 "subsystem": "sample", 118 "syscap": [], 119 "features": [], 120 "adapted_system_type": [ "mini", "small", "standard" ], 121 "rom": "10KB", 122 "ram": "10KB", 123 "deps": { 124 "components": [], 125 "third_party": [] 126 }, 127 "build": { 128 "sub_component": [ 129 "//applications/sample/hello:helloworld" 130 ], 131 "inner_kits": [], 132 "test": [] 133 } 134 } 135 } 136 ``` 137 138 bundle.json文件包含两个部分,第一部分描述该部件所属子系统的信息,第二部分component则定义该部件构建相关配置。添加的时候需要指明该部件包含的模块sub_component,假如有提供给其它部件的接口,需要在inner_kits中说明,假如有测试用例,需要在test中说明,inner_kits与test没有也可以不添加。 139 1403. 修改子系统配置文件。 141 142 在build/subsystem_config.json中添加新建的子系统的配置。 143 144 145 ``` 146 "sample": { 147 "path": "applications/sample/hello", 148 "name": "sample" 149 }, 150 ``` 151 1524. 修改产品配置文件。 153 154 在productdefine\common\products\rk3568.json中添加对应的hello部件,直接添加到原有部件后即可。 155 156 ``` 157 "usb:usb_manager_native":{}, 158 "applications:prebuilt_hap":{}, 159 "sample:hello":{}, 160 "wpa_supplicant-2.9:wpa_supplicant-2.9":{}, 161 ``` 162