diff --git a/en/application-dev/faqs/Readme-EN.md b/en/application-dev/faqs/Readme-EN.md index 21effd689817299aabdaaab34724edd8349622dc..d7316d5a25c20f4cd076b8ebca4ed700d7c387c7 100644 --- a/en/application-dev/faqs/Readme-EN.md +++ b/en/application-dev/faqs/Readme-EN.md @@ -2,6 +2,7 @@ - [Full SDK Compilation](full-sdk-compile-guide.md) - [Switching to Full SDK](full-sdk-switch-guide.md) +- [Using Native APIs (NDK) of the OpenHarmony SDK in a CMake Project](howto-migrate-cmake-with-ohosndk.md) - [Application Model Development](faqs-ability.md) - ArkUI Development (ArkTS) - [ArkTS Syntax Usage](faqs-arkui-arkts.md) diff --git a/en/application-dev/faqs/figures/ci_download.png b/en/application-dev/faqs/figures/ci_download.png new file mode 100644 index 0000000000000000000000000000000000000000..da0d306e0f95a4fa114af51ca010d9109ae7e029 Binary files /dev/null and b/en/application-dev/faqs/figures/ci_download.png differ diff --git a/en/application-dev/faqs/figures/images.png b/en/application-dev/faqs/figures/images.png new file mode 100644 index 0000000000000000000000000000000000000000..037e72b015f4f8bbfff818516a65cc51cb4e2944 Binary files /dev/null and b/en/application-dev/faqs/figures/images.png differ diff --git a/en/application-dev/faqs/figures/sdk-structure.png b/en/application-dev/faqs/figures/sdk-structure.png new file mode 100644 index 0000000000000000000000000000000000000000..18a9315151bc7b184a87c2f95b23238bbefc2b82 Binary files /dev/null and b/en/application-dev/faqs/figures/sdk-structure.png differ diff --git a/en/application-dev/faqs/howto-migrate-cmake-with-ohosndk.md b/en/application-dev/faqs/howto-migrate-cmake-with-ohosndk.md new file mode 100644 index 0000000000000000000000000000000000000000..36f339529c78327f2698f90ce199e758ba06485f --- /dev/null +++ b/en/application-dev/faqs/howto-migrate-cmake-with-ohosndk.md @@ -0,0 +1,191 @@ +# Using Native APIs (NDK) of the OpenHarmony SDK in a CMake Project + +## What Is Native API + +For details, see [Native APIs](../reference/native-api-intro.md). + +## Downloading the NDK + +You download the Native API Development Kit (NDK) by downloading the OHOS SDK, where the NDK is included. To download the OHOS SDK, use any of the following modes: + +- (Recommended) Acquire source code from mirrors for an officially released version. For details, see [release notes](../../release-notes/OpenHarmony-v3.2-release.md). +- Download the SDK from the SDK Manager in DevEco Studio. +- Download the SDK from the [daily build](http://ci.openharmony.cn/dailys/dailybuilds), by clicking the download link to the **ohos-sdk-full** component. + +![Download from Daily Build](figures/ci_download.png) + +## Decompressing the NDK + +Place the downloaded NDK in a folder you prefer and decompress it. Below shows the directory structure after decompression. +![SDK Directory Structure](figures/sdk-structure.png) + +Configure the Linux environment as follows: (Skip them if the NDK is downloaded from DevEco Studio.) + +1. Add the CMake tool that comes with the NDK to the environment variables. + +``` + # Open the .bashrc file. + vim ~/.bashrc + # Append the custom CMake path to the file. Save the file and exit. + export PATH=~/ohos-sdk/ohos-sdk/linux/native/build-tools/cmake/bin:$PATH + # Run the source ~/.bashrc command to make the environment variables take effect. + source ~/.bashrc +``` + +2. Check the default CMake path. + +``` + # Run the which cmake command. + which cmake + # The result should be the same as the custom path previously appended to the file. + ~/ohos-sdk/ohos-sdk/linux/native/build-tools/cmake/bin/cmake +``` + +## Using the NDK to Compile a Native Program + +You can use the NDK to quickly develop a native program, including native dynamic libraries, static libraries, and executable files. The ArkUI application framework can call the native dynamic libraries through the NAPI framework. The following exemplifies how to use the NDK to compile a C/C++ dynamic library in a C/C++ demo project. + +### Folders in the NDK + +#### build Folder: ohos.toolchain.cmake file + +The **ohos.toolchain.cmake** file contains the attributes of the CMake compilation target. Its path must be specified in the **CMAKE_TOOLCHAIN_FILE** parameter so that it can be located during CMake compilation. For details about the mandatory parameters in the **ohos.toolchain.cmake** file, see [Key Parameters in ohos.toolchain.cmake](#key-parameters-in-ohostoolchaincmake). + +#### build-tools folder: Build Tool Provided by the NDK + +``` + # Run the following command to view the CMake version: + cmake -version + # Result + cmake version 3.16.5 + + CMake suite maintained and supported by Kitware (kitware.com/cmake). +``` + +#### llvm Folder: Compiler Provided by the NDK + +![](figures/images.png) + +### Demo Project for the NDK + +#### Demo Project Directory + +``` + demo + ├── CMakeLists.txt + ├── include + └── sum.h + └── src + ├── CMakeLists.txt + ├── sum.cpp + └── hello.cpp +``` + +#### CMakeLists.txt in the demo Directory + +``` + # Specify the minimum CMake version. + CMAKE_MINIMUM_REQUIRED(VERSION 3.16) + + # Set the project name, which is HELLO in this example. + PROJECT(HELLO) + + # Add a subdirectory and build the subdirectory. + ADD_SUBDIRECTORY(src bin) +``` + +#### CMakeLists.txt in the src Directory + +``` + SET(LIBHELLO_SRC hello.cpp) + + # Set compilation flags. + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0") + + # Set the link parameter. The value below is only for exemplary purposes. + SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--emit-relocs --verbose") + + # Add a libsum dynamic library target. If the compilation is successful, a libsum.so file is generated. + ADD_LIBRARY(sum SHARED sum.cpp) + + # Add the executable target called Hello. If the compilation is successful, a Hello executable is generated. + ADD_EXECUTABLE(Hello ${LIBHELLO_SRC}) + + # Specify the path to the include directory of the Hello target. + TARGET_INCLUDE_DIRECTORIES(Hello PUBLIC ../include) + + # Specify the name of the library to be linked to the Hello target. + TARGET_LINK_LIBRARIES(Hello PUBLIC sum) +``` + +For details about CMake, see [CMake Tutorial](https://cmake.org/cmake/help/v3.16/guide/tutorial/). + +#### Source Code + +**hello.cpp** source code: + +``` + #include + #include "sum.h" + + int main(int argc,const char **argv) + { + std::cout<< "hello world!" <