轻鸿蒙NDK使用指南
头像 Tiechui.Wang 2021-03-26 13:21:00    发布
3806 浏览 17 点赞 20 收藏

在鸿蒙源码的最新版本1.0.1中有关于NDK开发的教程,教程的方法个人感觉和鸿蒙源码的1.0版中的Hi3861上开发第一个应用方法一样。教程位于源码的/build/lite/ndk/下

轻鸿蒙NDK使用指南-鸿蒙开发者社区

以下为教程内容


简介

NDK = Native Development Kit, 主要包括系统提供的c/c++接口库文件,编译工具链,工具和接口描述文档。


目录结构:

├── build 编译框架 │ ├── config │ └── toolchain ├── doc native api接口描述文档
├── gcc 编译工具链 │ ├── arm-linux-ohoseabi -> arm-linux-musleabi │ ├──
arm-linux-musleabi │ ├── bin │ ├── host_bin │ ├── lib │ ├── libexec │
├── runtime_musl │ └── target ├── prebuilts 构建工具 │ └── build-tools ├──
sample 用户编译样例 │ ├── include │ └── src └── sysroot Native API └── usr


编译框架

编译命令

编译使用gn构建系统,在根目录执行:python build.py即可启动编译。支持的命令如下:


build: python build.py 或 ``python build.py build`


clean: python build.py clean


debug/release:python build.py -v debug/release


debug和release版本的区别:


debug版本:-g


release版本:-O2 + strip符号表


默认编译选项

1、安全编译选项:-fstack-protector-all,PIE,PIC,_FORTIFY_SOURCE=2,-Wl,-z,now,-Wl,-z,relro,-Wl,-z,noexecstack


2、告警级别:-Werror


应用编译示例

1、创建应用目录,并在目录下创建BUILD.gn:


# Copyright (c) 2020 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

static_library("hello_world") {	# 应用库文件target,也可是shared_library
    sources = [
        "src/hello_world.c",
    ]

    include_dirs = [
        "include",
    ]
}

executable("sample") {			# 应用可执行文件target
    ldflags = [
        "-lsys_parameter",  	# 应用需要使用的库
        "-lsec_shared"
    ]
    deps = [
        ":hello_world",
    ]
}

2、将sample加入到编译入口,编译入口为根目录下的BUILD.gn:


# Copyright (c) 2020 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/toolchain/${ohos_build_compiler}.gni")

group("ohos") {
    deps = []
    if (target_os == "ohos") {
        deps += [
            "//sample" # 新加的应用
        ]
    }
}

3、编译输出:out/bin


烧录和运行

1、请先烧录内核和文件系统


2、将应用程序nfs或者tftp到usr/bin目录下, 运行即可


©本站发布的所有内容,包括但不限于文字、图片、音频、视频、图表、标志、标识、广告、商标、商号、域名、软件、程序等,除特别标明外,均来源于网络或用户投稿,版权归原作者或原出处所有。我们致力于保护原作者版权,若涉及版权问题,请及时联系我们进行处理。
分类
其它
地址:北京市朝阳区北三环东路三元桥曙光西里甲1号第三置业A座1508室 商务内容合作QQ:2291221 电话:13391790444或(010)62178877
版权所有:电脑商情信息服务集团 北京赢邦策略咨询有限责任公司
声明:本媒体部分图片、文章来源于网络,版权归原作者所有,我司致力于保护作者版权,如有侵权,请与我司联系删除
京ICP备:2022009079号-2
京公网安备:11010502051901号
ICP证:京B2-20230255