1# Building an NDK Project with the DevEco Studio Template
2
3
4The NDK uses CMake and Ninja to build C/C++ code of applications. The figure below shows the build process.
5
6
7![Snipaste_2023-11-03_14-41-18](figures/Snipaste_2023-11-03_14-41-18.png)
8
9
10The core build process is as follows:
11
12
131. The build settings in the CMake configuration script and **externalNativeOptions** in **build-profile.json5** are compared with the settings in the native cache to generate CMake commands, and execute them.
14
152. Run Ninja, perform compilation and linking based on the makefile, and synchronize the generated .so file and the .so file on which the runtime depends to the output directory to complete the build process.
16
17
18You can use the application template provided by DevEco Studio to quickly generate a CMake build script and set related compilation and build parameters in **build-profile.json5**.
19
20
21## CMakeLists.txt
22
23The NDK project created using the DevEco Studio template contains the automatically generated **CMakeLists.txt** script, as shown in the following:
24
25```
26# the minimum version of CMake.
27cmake_minimum_required(VERSION 3.4.1)
28project(MyApplication)
29
30# Define a variable and assign the cpp directory of the current module as its value.
31set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
32
33# Add an .h directory, which includes the cpp and cpp/include directories, and instruct CMake to find in this directory the imported header file.
34include_directories(${NATIVERENDER_ROOT_PATH}
35                    ${NATIVERENDER_ROOT_PATH}/include)
36
37# Declare the product libentry.so. SHARED indicates that the product is a dynamic library, and hello.cpp indicates the source code of the product.
38add_library(entry SHARED hello.cpp)
39
40# Declare the third-party library libace_napi.z.so required for linking to the product entry.
41# The name of the third-party library is directly used here because it is in the NDK and has been in the link addressing path. No additional declaration is required.
42target_link_libraries(entry PUBLIC libace_napi.z.so)
43```
44
45The source code, header files, and third-party libraries required for compilation are contained in the default **CMakeLists.txt** script. You can add custom compilation parameters, function declarations, and simple logic control based on the actual project.
46
47
48## externalNativeOptions
49
50The **externalNativeOptions** parameter in the module-level **build-profile.json5** file is the entry for configuring C/C++ file compilation of the NDK project. You can use **path** to specify the path to the CMake script, **arguments** to set CMake parameters, **cppFlags** to set C++ compiler parameters, and **abiFilters** to set the compilation architecture.
51```
52"apiType": "stageMode",
53"buildOption": {
54  "arkOptions": {
55   },
56  "externalNativeOptions": {
57    "path": "./src/main/cpp/CMakeLists.txt",
58    "arguments": "",
59    "cppFlags": "",
60    "abiFilters": [
61       "arm64-v8a",
62       "x86_64"
63    ],
64  }
65}
66```
67
68The following table describes the **externalNativeOptions** parameter.
69
70| Configuration Item| Type| Description|
71| -------- | -------- | -------- |
72| path | string | Path to the CMake build script, that is, the **CMakeLists.txt** file.|
73| abiFilters | array | Local ABI. Available options are as follows:<!--Del--><br>- armeabi-v7a<!--DelEnd--><br>- arm64-v8a<br>- x86_64<br>If this parameter is not set, the **arm64-v8a** option is used.|
74| arguments | string | CMake compilation parameters.|
75| cppFlags | string | C++ compiler parameters.|
76
77For details about the parameters in the **build-profile.json5** file, see <!--RP1-->[build-profile.json5](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V5/ide-hvigor-build-profile-V5)<!--RP1End-->.
78