1e41f4b71Sopenharmony_ci# bootstrap服务启动组件 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_cibootstrap服务启动组件实现了服务的自动初始化,即服务的初始化函数无需显式调用,而是将其使用宏定义的方式申明,就会在系统启动时自动被执行。实现原理是将服务启动的函数通过宏定义的方式申明之后,放在预定义好的zInit代码段中,系统启动的时候调用OHOS_SystemInit接口遍历该代码段并调用其中的函数。因此,需要在链接脚本中添加zInit段,并且在main函数里调用OHOS_SystemInit接口。 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_cizInit段的添加可参考已有的Hi3861平台的链接脚本,文件路径为vendor/hisi/hi3861/hi3861/build/link/link.ld.S。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci用于实现服务的自动初始化的宏定义接口请参见启动恢复子系统的[API接口文档](https://device.harmonyos.com/cn/docs/develop/apiref/init-0000001054598113)。 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## 接口说明 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_cibootstrap服务自动初始化宏如表1所述。 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci **表1** 主要的服务自动初始化宏 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci| 接口名 | 描述 | 20e41f4b71Sopenharmony_ci| -------- | -------- | 21e41f4b71Sopenharmony_ci| SYS_SERVICE_INIT(func) | 标识核心系统服务的初始化启动入口。 | 22e41f4b71Sopenharmony_ci| SYS_FEATURE_INIT(func) | 标识核心系统功能的初始化启动入口。 | 23e41f4b71Sopenharmony_ci| APP_SERVICE_INIT(func) | 标识应用层服务的初始化启动入口。 | 24e41f4b71Sopenharmony_ci| APP_FEATURE_INIT(func) | 标识应用层功能的初始化启动入口。 | 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci## 开发实例 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci 服务自动初始化宏使用实例: 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci``` 32e41f4b71Sopenharmony_civoid SystemServiceInit(void) { 33e41f4b71Sopenharmony_ci printf("Init System Service\n"); 34e41f4b71Sopenharmony_ci} 35e41f4b71Sopenharmony_ciSYS_SERVICE_INIT(SystemServiceInit); 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_civoid SystemFeatureInit(void) { 38e41f4b71Sopenharmony_ci printf("Init System Feature\n"); 39e41f4b71Sopenharmony_ci} 40e41f4b71Sopenharmony_ciSYS_FEATURE_INIT(SystemFeatureInit); 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_civoid AppServiceInit(void) { 43e41f4b71Sopenharmony_ci printf("Init App Service\n"); 44e41f4b71Sopenharmony_ci} 45e41f4b71Sopenharmony_ciAPP_SERVICE_INIT(AppServiceInit); 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_civoid AppFeatureInit(void) { 48e41f4b71Sopenharmony_ci printf("Init App Feature\n"); 49e41f4b71Sopenharmony_ci} 50e41f4b71Sopenharmony_ciAPP_FEATURE_INIT(AppFeatureInit); 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci// 日志打印顺序为: 53e41f4b71Sopenharmony_ci// Init System Service 54e41f4b71Sopenharmony_ci// Init System Feature 55e41f4b71Sopenharmony_ci// Init App Service 56e41f4b71Sopenharmony_ci// Init App Feature 57e41f4b71Sopenharmony_ci``` 58