• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using NDK in a CMake Project
2
3## What Is Native API
4
5For details, see [Native APIs](../reference/native-api-intro.md).
6
7## Downloading the NDK
8
9You 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:
10
11- (Recommended) Acquire source code from mirrors for an officially released version. For details, see [release notes](../../release-notes/OpenHarmony-v3.2-release.md).
12- Download the SDK from the SDK Manager in DevEco Studio.
13- Download the SDK from the [daily build](http://ci.openharmony.cn/workbench/cicd/dailybuild/dailylist), by clicking the download link to the **ohos-sdk-full** component.
14
15![](figures/ci_download.png)
16
17## Decompressing the NDK
18
19Place the downloaded NDK in a folder you prefer and decompress it. Below shows the directory structure after decompression.
20![](figures/sdk-structure.png)
21
22Configure the Linux environment as follows: (Skip them if the NDK is downloaded from DevEco Studio.)
23
24Add the CMake tool that comes with the NDK to the environment variables.
25
26```
27    # Open the .bashrc file.
28    vim ~/.bashrc
29    # Append the custom CMake path to the file.
30    export PATH=~/ohos-sdk/ohos-sdk/linux/native/build-tools/cmake/bin:$PATH
31    # Run the source ~/.bashrc command to make the environment variables take effect.
32    source ~/.bashrc
33```
34
35Check the default CMake path.
36
37```
38    # Run the which cmake command.
39    which cmake
40    # The result should be the same as the custom path previously appended to the .bashrc file.
41    ~/ohos-sdk/ohos-sdk/linux/native/build-tools/cmake/bin/cmake
42```
43
44## Using the NDK to Compile a Native Program
45
46You 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.
47
48### Folders in the NDK
49
50#### build Folder: ohos.toolchain.cmake file
51
52The **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).
53
54#### build-tools folder: Build Tool Provided by the NDK
55
56```
57    # Run the following command to view the CMake version:
58    cmake -version
59    # Result
60    cmake version 3.16.5
61
62    CMake suite maintained and supported by Kitware (kitware.com/cmake).
63```
64
65#### llvm Folder: Compiler Provided by the NDK
66
67![](figures/images.png)
68
69### Demo Project for the NDK
70
71#### Demo Project Directory
72
73```
74    demo
75      ├── CMakeLists.txt
76      ├── include
77           └── sum.h
78      └── src
79           ├── CMakeLists.txt
80           ├── sum.cpp
81           └── hello.cpp
82```
83
84#### CMakeLists.txt in the demo Directory
85
86```
87    # Specify the minimum CMake version.
88    CMAKE_MINIMUM_REQUIRED(VERSION 3.16)
89
90    # Set the project name, which is HELLO in this example.
91    PROJECT(HELLO)
92
93    # Add a subdirectory and build the subdirectory.
94    ADD_SUBDIRECTORY(src bin)
95```
96
97#### CMakeLists.txt in the src Directory
98
99```
100    SET(LIBHELLO_SRC hello.cpp)
101
102    # Set compilation flags.
103    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
104
105    # Set the link parameter. The value below is only for exemplary purposes.
106    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--emit-relocs --verbose")
107
108    # Add a libsum dynamic library target. If the compilation is successful, a libsum.so file is generated.
109    ADD_LIBRARY(sum SHARED sum.cpp)
110
111    # Add the executable target called Hello. If the compilation is successful, a Hello executable is generated.
112    ADD_EXECUTABLE(Hello ${LIBHELLO_SRC})
113
114    # Specify the path to the include directory of the Hello target.
115    TARGET_INCLUDE_DIRECTORIES(Hello PUBLIC ../include)
116
117    # Specify the name of the library to be linked to the Hello target.
118    TARGET_LINK_LIBRARIES(Hello PUBLIC sum)
119```
120
121For details about CMake, see [CMake Tutorial](https://cmake.org/cmake/help/v3.16/guide/tutorial/).
122
123#### Source Code
124
125**hello.cpp** source code:
126
127```
128    #include <iostream>
129    #include "sum.h"
130
131    int main(int argc,const char **argv)
132    {
133        std::cout<< "hello world!" <<std::endl;
134        int total = sum(1, 100);
135        std::cout<< "Sum 1 + 100=" << total << std::endl;
136        return 0;
137    }
138```
139
140**sum.h** source code:
141
142```
143    int sum(int a, int b);
144
145```
146
147**sum.cpp** source code:
148
149```
150    #include <iostream>
151
152    int sum(int a, int b)
153    {
154        return a + b;
155    }
156```
157
158### Key Parameters in ohos.toolchain.cmake
159
160| Parameter  | Type|Description|
161|--------|------|------|
162|OHOS_STL|c++\_shared/c++\_static|STL to use. The value must be consistent across the native libraries in the same application.<br>**c++\_shared** (default): The shared library of libc++, libc++\_shared.so, is used.<br>**c++\_static**: The static library of libc++, libc++\_static.a, is used.|
163|OHOS_ARCH|armeabi-v7a/arm64-v8a/x86_64|ABI to support. Currently, three types of ABI are supported.|
164|OHOS_PLATFORM|OHOS|Target platform. Currently, only OpenHarmony is supported.|
165|CMAKE_TOOLCHAIN_FILE|Toolchain file|CMake toolchain file, that is, the aforementioned **ohos.toolchain.cmake** file.|
166
167### Building from Command Line
168
169In the project directory, create the **build** directory to store the intermediate files generated during CMake building.
170
171> **NOTE**
172>
173> In the following commands, **ohos-sdk** is the root directory of the downloaded SDK. Replace it with the actual directory.
174
1751. Use **OHOS_STL=c++_shared** for dynamic compilation.
176
177   ```
178    >mkdir build && cd build
179    >cmake -DOHOS_STL=c++_shared -DOHOS_ARCH=armeabi-v7a -DOHOS_PLATFORM=OHOS -DCMAKE_TOOLCHAIN_FILE={ohos-sdk}/linux/native/build/cmake/ohos.toolchain.cmake ..
180    >cmake --build .
181   ```
182
1832. Use **OHOS_STL=c++_static** for static compilation.
184
185   ```
186    >mkdir build && cd build
187    >cmake -DOHOS_STL=c++_static -DOHOS_ARCH=armeabi-v7a -DOHOS_PLATFORM=OHOS -DCMAKE_TOOLCHAIN_FILE={ohos-sdk}/linux/native/build/cmake/ohos.toolchain.cmake ..
188    >cmake --build .
189   ```
190
191<!--no_check-->