1e41f4b71Sopenharmony_ci# Performing Lifecycle Management Using Node-API
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Introduction
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciIn Node-API, **napi_value** is an abstract data type that represents an ArkTS value of any type, which includes the basic type (such as number, string, or Boolean) and the composite type (such as array, function, or object).
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciThe **napi_value** lifecycle is closely related to the lifecycle of the ArkTS value. When an ArkTS value is garbage-collected, the **napi_value** associated with it is no longer valid. Avoid using the **napi_value** when the ArkTS value no longer exists.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciScope is used to manage the **napi_value** lifecycle in the framework layer. You can use **napi_open_handle_scope** to open a scope and use **napi_close_handle_scope** to close a scope. By creating a **napi_value** in a scope, you can ensure that the **napi_value** is automatically released when the scope ends. This helps prevent memory leaks.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci**napi_ref** is a Node-API type used to manage the **napi_value** lifecycle. It allows reference to a **napi_value** during its lifecycle, even if the value is beyond its original context. The reference allows a **napi_value** to be shared in different contexts and released in a timely manner.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci## Basic Concepts
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ciNode-API provides APIs for creating and manipulating ArkTS objects, managing references to and lifecycle of the ArkTS objects, and registering garbage collection (GC) callbacks in C/C++. Before you get started, you need to understand the following concepts:
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci- Scope: used to ensure that the objects created within a certain scope remain active and are properly cleared when no longer required. Node-API provides APIs for creating and closing normal and escapeable scopes.
18e41f4b71Sopenharmony_ci- Reference management: Node-API provides APIs for creating, deleting, and managing object references to extend the lifecycle of objects and prevent the use-after-free issues. In addition, reference management also helps prevent memory leaks.
19e41f4b71Sopenharmony_ci- Escapeable scope: used to return the values created within the **escapable_handle_scope** to a parent scope. It is created by **napi_open_escapable_handle_scope** and closed by **napi_close_escapable_handle_scope**.
20e41f4b71Sopenharmony_ci- GC callback: You can register GC callbacks to perform specific cleanup operations when ArkTS objects are garbage-collected.
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ciUnderstanding these concepts helps you securely and effectively manipulate ArkTS objects in C/C++ and perform object lifecycle management.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci## Available APIs
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ciThe following table lists the APIs for ArkTS object lifecycle management.  
27e41f4b71Sopenharmony_ci| API| Description|
28e41f4b71Sopenharmony_ci| -------- | -------- |
29e41f4b71Sopenharmony_ci| napi_open_handle_scope | Opens a scope.<br/>When processing ArkTS objects with Node-API, you need to create a temporary scope to store object references so that the objects can be correctly accessed during the execution and closed after the execution. |
30e41f4b71Sopenharmony_ci| napi_close_handle_scope | Closes a scope. |
31e41f4b71Sopenharmony_ci| napi_open_escapable_handle_scope | Opens a scope from which one object can be prompted to the outer scope. |
32e41f4b71Sopenharmony_ci| napi_close_escapable_handle_scope | Closes an escapable handle scope. |
33e41f4b71Sopenharmony_ci| napi_escape_handle | Promotes the handle to an ArkTS object so that it is valid for the lifetime of its parent scope.|
34e41f4b71Sopenharmony_ci| napi_create_reference | Creates a reference to a value to extend the ArkTS object's lifespan. |
35e41f4b71Sopenharmony_ci| napi_delete_reference | Deletes a reference. |
36e41f4b71Sopenharmony_ci| napi_reference_ref | Increments the reference count. |
37e41f4b71Sopenharmony_ci| napi_reference_unref | Decrements the reference count. |
38e41f4b71Sopenharmony_ci| napi_get_reference_value | Obtains the ArkTS object associated with the reference.|
39e41f4b71Sopenharmony_ci| napi_add_finalizer | Adds a **napi_finalize** callback, which will be called to clean up or release resources before the ArkTS object is garbage-collected.|
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci## Example
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ciIf you are just starting out with Node-API, see [Node-API Development Process](use-napi-process.md). The following demonstrates only the C++ and ArkTS code related to lifecycle management.
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci### napi_open_handle_scope and napi_close_handle_scope
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ciUse **napi_open_handle_scope** to open a scope, and use **napi_close_handle_scope** to close it. You can use these two APIs to manage the **napi_value** lifecycle of an ArkTS object, which prevents the object from being incorrectly garbage-collected. 
48e41f4b71Sopenharmony_ciProperly using these two APIs can minimize lifecycle and prevent memory leaks.
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ciFor details about the code, see [Lifecycle Management](napi-guidelines.md).
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ciCPP code:
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci```cpp
55e41f4b71Sopenharmony_ci#include "napi/native_api.h"
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_cistatic napi_value HandleScopeTest(napi_env env, napi_callback_info info)
58e41f4b71Sopenharmony_ci{
59e41f4b71Sopenharmony_ci    // Call napi_open_handle_scope to open a scope.
60e41f4b71Sopenharmony_ci    napi_handle_scope scope;
61e41f4b71Sopenharmony_ci    napi_open_handle_scope(env, &scope);
62e41f4b71Sopenharmony_ci    // Create an object within the scope.
63e41f4b71Sopenharmony_ci    napi_value obj = nullptr;
64e41f4b71Sopenharmony_ci    napi_create_object(env, &obj);
65e41f4b71Sopenharmony_ci    // Add a property to the object.
66e41f4b71Sopenharmony_ci    napi_value value = nullptr;
67e41f4b71Sopenharmony_ci    napi_create_string_utf8(env, "handleScope", NAPI_AUTO_LENGTH, &value);
68e41f4b71Sopenharmony_ci    napi_set_named_property(env, obj, "key", value);
69e41f4b71Sopenharmony_ci    // Obtain the object property in the scope and return it.
70e41f4b71Sopenharmony_ci    napi_value result = nullptr;
71e41f4b71Sopenharmony_ci    napi_get_named_property(env, obj, "key", &result);
72e41f4b71Sopenharmony_ci    // Close the scope. Then, the object handle created within the scope is automatically released.
73e41f4b71Sopenharmony_ci    napi_close_handle_scope(env, scope);
74e41f4b71Sopenharmony_ci    // The value of result is 'handleScope'.
75e41f4b71Sopenharmony_ci    return result;
76e41f4b71Sopenharmony_ci}
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_cistatic napi_value HandleScope(napi_env env, napi_callback_info info)
79e41f4b71Sopenharmony_ci{
80e41f4b71Sopenharmony_ci    // Call napi_open_handle_scope to open a scope.
81e41f4b71Sopenharmony_ci    napi_handle_scope scope;
82e41f4b71Sopenharmony_ci    napi_open_handle_scope(env, &scope);
83e41f4b71Sopenharmony_ci    // Create an object within the scope.
84e41f4b71Sopenharmony_ci    napi_value obj = nullptr;
85e41f4b71Sopenharmony_ci    napi_create_object(env, &obj);
86e41f4b71Sopenharmony_ci    // Add a property to the object.
87e41f4b71Sopenharmony_ci    napi_value value = nullptr;
88e41f4b71Sopenharmony_ci    napi_create_string_utf8(env, "handleScope", NAPI_AUTO_LENGTH, &value);
89e41f4b71Sopenharmony_ci    napi_set_named_property(env, obj, "key", value);
90e41f4b71Sopenharmony_ci    // Close the scope. Then, the object handle created within the scope is automatically released.
91e41f4b71Sopenharmony_ci    napi_close_handle_scope(env, scope);
92e41f4b71Sopenharmony_ci    // Obtain and return the object property outside the scope. In this example, "undefined" is obtained.
93e41f4b71Sopenharmony_ci    napi_value result = nullptr;
94e41f4b71Sopenharmony_ci    napi_get_named_property(env, obj, "key", &result);
95e41f4b71Sopenharmony_ci    return result;
96e41f4b71Sopenharmony_ci}
97e41f4b71Sopenharmony_ci```
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ciAPI declaration:
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci```ts
102e41f4b71Sopenharmony_ci// index.d.ts
103e41f4b71Sopenharmony_ciexport const handleScopeTest: () => string;
104e41f4b71Sopenharmony_ciexport const handleScope: () => string;
105e41f4b71Sopenharmony_ci```
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ciArkTS code:
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci```ts
110e41f4b71Sopenharmony_ciimport hilog from '@ohos.hilog'
111e41f4b71Sopenharmony_ciimport testNapi from 'libentry.so'
112e41f4b71Sopenharmony_citry {
113e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', 'Test Node-API handleScopeTest: %{public}s', testNapi.handleScopeTest());
114e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', 'Test Node-API handleScope: %{public}s', testNapi.handleScope());
115e41f4b71Sopenharmony_ci} catch (error) {
116e41f4b71Sopenharmony_ci  hilog.error(0x0000, 'testTag', 'Test Node-API handleScopeTest errorCode: %{public}s, errorMessage: %{public}s', error.code, error.message);
117e41f4b71Sopenharmony_ci}
118e41f4b71Sopenharmony_ci```
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci### napi_open_escapable_handle_scope, napi_close_escapable_handle_scope, and napi_escape_handle
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ciUse **napi_open_escapable_handle_scope** to open an escapeable scope, which allows the declared values in the scope to be returned to the parent scope. You can use **napi_close_escapable_handle_scope** to close it. 
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ciUse **napi_escape_handle** to promote the lifecycle of an ArkTS object so that it is valid for the lifetime of the parent scope.
125e41f4b71Sopenharmony_ciThese APIs are helpful for managing ArkTS objects more flexibly in C/C++, especially when passing cross-scope values.
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ciCPP code:
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci```cpp
130e41f4b71Sopenharmony_ci#include "napi/native_api.h"
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_cistatic napi_value EscapableHandleScopeTest(napi_env env, napi_callback_info info)
133e41f4b71Sopenharmony_ci{
134e41f4b71Sopenharmony_ci    // Create an escapeable scope.
135e41f4b71Sopenharmony_ci    napi_escapable_handle_scope scope;
136e41f4b71Sopenharmony_ci    napi_open_escapable_handle_scope(env, &scope);
137e41f4b71Sopenharmony_ci    // Create an object within the escapeable scope.
138e41f4b71Sopenharmony_ci    napi_value obj = nullptr;
139e41f4b71Sopenharmony_ci    napi_create_object(env, &obj);
140e41f4b71Sopenharmony_ci    // Add a property to the object.
141e41f4b71Sopenharmony_ci    napi_value value = nullptr;
142e41f4b71Sopenharmony_ci    napi_create_string_utf8(env, "Test napi_escapable_handle_scope", NAPI_AUTO_LENGTH, &value);
143e41f4b71Sopenharmony_ci    napi_set_named_property(env, obj, "key", value);
144e41f4b71Sopenharmony_ci    // Call napi_escape_handle to promote the ArkTS object handle to make it valid with the lifetime of the outer scope.
145e41f4b71Sopenharmony_ci    napi_value escapedObj = nullptr;
146e41f4b71Sopenharmony_ci    napi_escape_handle(env, scope, obj, &escapedObj);
147e41f4b71Sopenharmony_ci    // Close the escapeable scope to clear resources.
148e41f4b71Sopenharmony_ci    napi_close_escapable_handle_scope(env, scope);
149e41f4b71Sopenharmony_ci    // Obtain and return the property of the object whose scope is promoted. You can also obtain napi_escapable_handle_scope here.
150e41f4b71Sopenharmony_ci    napi_value result = nullptr;
151e41f4b71Sopenharmony_ci    // To verify the escape implementation, obtain the object property here. "undefined" is obtained.
152e41f4b71Sopenharmony_ci    napi_get_named_property(env, escapedObj, "key", &result);
153e41f4b71Sopenharmony_ci    return result;
154e41f4b71Sopenharmony_ci}
155e41f4b71Sopenharmony_ci```
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ciAPI declaration:
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci```ts
160e41f4b71Sopenharmony_ci// index.d.ts
161e41f4b71Sopenharmony_ciexport const escapableHandleScopeTest: () => string;
162e41f4b71Sopenharmony_ci```
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ciArkTS code:
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ci```ts
167e41f4b71Sopenharmony_ciimport hilog from '@ohos.hilog'
168e41f4b71Sopenharmony_ciimport testNapi from 'libentry.so'
169e41f4b71Sopenharmony_citry {
170e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', 'Test Node-API EscapableHandleScopeTest: %{public}s', testNapi.escapableHandleScopeTest());
171e41f4b71Sopenharmony_ci} catch (error) {
172e41f4b71Sopenharmony_ci  hilog.error(0x0000, 'testTag', 'Test Node-API EscapableHandleScopeTest errorCode: %{public}s, errorMessage: %{public}s', error.code, error.message);
173e41f4b71Sopenharmony_ci}
174e41f4b71Sopenharmony_ci```
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci### napi_create_reference and napi_delete_reference
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ciUse **napi_create_reference** to create a reference for an object to extend its lifespan. The caller needs to manage the reference lifespan. Use **napi_delete_reference** to delete a reference.
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci### napi_reference_ref and napi_reference_unref
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ciUse **napi_reference_ref** to increment the reference count and use **napi_reference_unref** to decrement the reference count, and return the new count value.
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci### napi_get_reference_value
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ciUse **napi_get_reference_value** to obtain the ArkTS object associated with the reference.
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci### napi_add_finalizer
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ciUse **napi_add_finalizer** to add a **napi_finalizer** callback, which will be called when the ArkTS object is garbage-collected.
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ciCPP code:
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci```cpp
195e41f4b71Sopenharmony_ci// log.h is used to print logs in C++.
196e41f4b71Sopenharmony_ci#include "hilog/log.h"
197e41f4b71Sopenharmony_ci#include "napi/native_api.h"
198e41f4b71Sopenharmony_ci// Create a pointer to napi_ref to store the created reference. Before calling napi_create_reference, you need to allocate a variable of the napi_ref type and pass its address to the parameter in result.
199e41f4b71Sopenharmony_cinapi_ref g_ref;
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_civoid Finalizer(napi_env env, void *data, void *hint)
202e41f4b71Sopenharmony_ci{
203e41f4b71Sopenharmony_ci    // Clear resources.
204e41f4b71Sopenharmony_ci    OH_LOG_INFO(LOG_APP, "Node-API: Use terminators to release resources.");
205e41f4b71Sopenharmony_ci}
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_cistatic napi_value CreateReference(napi_env env, napi_callback_info info)
208e41f4b71Sopenharmony_ci{
209e41f4b71Sopenharmony_ci    napi_value obj = nullptr;
210e41f4b71Sopenharmony_ci    napi_create_object(env, &obj);
211e41f4b71Sopenharmony_ci    napi_value value = nullptr;
212e41f4b71Sopenharmony_ci    napi_create_string_utf8(env, "CreateReference", NAPI_AUTO_LENGTH, &value);
213e41f4b71Sopenharmony_ci    // Add a property to the object.
214e41f4b71Sopenharmony_ci    napi_set_named_property(env, obj, "key", value);
215e41f4b71Sopenharmony_ci    // Create a reference to the ArkTS object.
216e41f4b71Sopenharmony_ci    napi_status status = napi_create_reference(env, obj, 1, &g_ref);
217e41f4b71Sopenharmony_ci    if (status != napi_ok) {
218e41f4b71Sopenharmony_ci        napi_throw_error(env, nullptr, "napi_create_reference fail");
219e41f4b71Sopenharmony_ci        return nullptr;
220e41f4b71Sopenharmony_ci    }
221e41f4b71Sopenharmony_ci    // Add a terminator.
222e41f4b71Sopenharmony_ci    void *data = {};
223e41f4b71Sopenharmony_ci    napi_add_finalizer(env, obj, data, Finalizer, nullptr, &g_ref);
224e41f4b71Sopenharmony_ci    // Increment the reference count and return the new reference count.
225e41f4b71Sopenharmony_ci    uint32_t result = 0;
226e41f4b71Sopenharmony_ci    napi_reference_ref(env, g_ref, &result);
227e41f4b71Sopenharmony_ci    OH_LOG_INFO(LOG_APP, "napi_reference_ref, count = %{public}d.", result);
228e41f4b71Sopenharmony_ci    if (result != 2) {
229e41f4b71Sopenharmony_ci        // If the reference count passed in does not increase, throw an error.
230e41f4b71Sopenharmony_ci        napi_throw_error(env, nullptr, "napi_reference_ref fail");
231e41f4b71Sopenharmony_ci        return nullptr;
232e41f4b71Sopenharmony_ci    }
233e41f4b71Sopenharmony_ci    return obj;
234e41f4b71Sopenharmony_ci}
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_cistatic napi_value UseReference(napi_env env, napi_callback_info info)
237e41f4b71Sopenharmony_ci{
238e41f4b71Sopenharmony_ci    napi_value obj = nullptr;
239e41f4b71Sopenharmony_ci    // Call napi_get_reference_value to obtain the referenced ArkTS object.
240e41f4b71Sopenharmony_ci    napi_status status = napi_get_reference_value(env, g_ref, &obj);
241e41f4b71Sopenharmony_ci    if (status != napi_ok) {
242e41f4b71Sopenharmony_ci        napi_throw_error(env, nullptr, "napi_get_reference_value fail");
243e41f4b71Sopenharmony_ci        return nullptr;
244e41f4b71Sopenharmony_ci    }
245e41f4b71Sopenharmony_ci    // Return the obtained object.
246e41f4b71Sopenharmony_ci    return obj;
247e41f4b71Sopenharmony_ci}
248e41f4b71Sopenharmony_ci
249e41f4b71Sopenharmony_cistatic napi_value DeleteReference(napi_env env, napi_callback_info info)
250e41f4b71Sopenharmony_ci{
251e41f4b71Sopenharmony_ci    // Decrement the reference count and return the new reference count.
252e41f4b71Sopenharmony_ci    uint32_t result = 0;
253e41f4b71Sopenharmony_ci    napi_value count = nullptr;
254e41f4b71Sopenharmony_ci    napi_reference_unref(env, g_ref, &result);
255e41f4b71Sopenharmony_ci    OH_LOG_INFO(LOG_APP, "napi_reference_ref, count = %{public}d.", result);
256e41f4b71Sopenharmony_ci    if (result != 1) {
257e41f4b71Sopenharmony_ci        // If the reference count passed in does not decrease, throw an error.
258e41f4b71Sopenharmony_ci        napi_throw_error(env, nullptr, "napi_reference_unref fail");
259e41f4b71Sopenharmony_ci        return nullptr;
260e41f4b71Sopenharmony_ci    }
261e41f4b71Sopenharmony_ci    // Call napi_delete_reference to delete the reference to the ArkTS object.
262e41f4b71Sopenharmony_ci    napi_status status = napi_delete_reference(env, g_ref);
263e41f4b71Sopenharmony_ci    if (status != napi_ok) {
264e41f4b71Sopenharmony_ci        napi_throw_error(env, nullptr, "napi_delete_reference fail");
265e41f4b71Sopenharmony_ci        return nullptr;
266e41f4b71Sopenharmony_ci    }
267e41f4b71Sopenharmony_ci    napi_value returnResult = nullptr;
268e41f4b71Sopenharmony_ci    napi_create_string_utf8(env, "napi_delete_reference success", NAPI_AUTO_LENGTH, &returnResult);
269e41f4b71Sopenharmony_ci    return returnResult;
270e41f4b71Sopenharmony_ci}
271e41f4b71Sopenharmony_ci```
272e41f4b71Sopenharmony_ci
273e41f4b71Sopenharmony_ciAPI declaration:
274e41f4b71Sopenharmony_ci
275e41f4b71Sopenharmony_ci```ts
276e41f4b71Sopenharmony_ci// index.d.ts
277e41f4b71Sopenharmony_ciexport const createReference: () => Object | void;
278e41f4b71Sopenharmony_ciexport const useReference: () => Object | void;
279e41f4b71Sopenharmony_ciexport const deleteReference: () => string | void;
280e41f4b71Sopenharmony_ci```
281e41f4b71Sopenharmony_ci
282e41f4b71Sopenharmony_ciArkTS code:
283e41f4b71Sopenharmony_ci
284e41f4b71Sopenharmony_ci```ts
285e41f4b71Sopenharmony_ciimport hilog from '@ohos.hilog'
286e41f4b71Sopenharmony_ciimport testNapi from 'libentry.so'
287e41f4b71Sopenharmony_citry {
288e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', 'Test Node-API createReference: %{public}s', JSON.stringify(testNapi.createReference()));
289e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', 'Test Node-API useReference: %{public}s', JSON.stringify(testNapi.useReference()));
290e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', 'Test Node-API deleteReference: %{public}s', testNapi.deleteReference());
291e41f4b71Sopenharmony_ci} catch (error) {
292e41f4b71Sopenharmony_ci  hilog.error(0x0000, 'testTag', 'Test Node-API ReferenceTest errorCode: %{public}s, errorMessage: %{public}s', error.code, error.message);
293e41f4b71Sopenharmony_ci}
294e41f4b71Sopenharmony_ci```
295e41f4b71Sopenharmony_ci
296e41f4b71Sopenharmony_ciTo print logs in the native CPP, add the following information to the **CMakeLists.txt** file and add the header file by using **#include "hilog/log.h"**.
297e41f4b71Sopenharmony_ci
298e41f4b71Sopenharmony_ci```text
299e41f4b71Sopenharmony_ci// CMakeLists.txt
300e41f4b71Sopenharmony_ciadd_definitions( "-DLOG_DOMAIN=0xd0d0" )
301e41f4b71Sopenharmony_ciadd_definitions( "-DLOG_TAG=\"testTag\"" )
302e41f4b71Sopenharmony_citarget_link_libraries(entry PUBLIC libhilog_ndk.z.so)
303e41f4b71Sopenharmony_ci```
304