HarmonyOS 鸿蒙Next 引入静态库.a
HarmonyOS 鸿蒙Next 引入静态库.a
blockquote鸿蒙支持引入静态库.a文件,前提是.a静态库是由鸿蒙提供的工具链编译的产物。
Native C++工程目录结构如截图,libets-native.a是由鸿蒙提供的工具链编译的产物,里面简单实现了add函数,引入头文件add.h。
add.h
#ifndef ADD_H
#define ADD_H
double add(double a, double b);
#endif
hello.cpp
#include "add.h" //引入静态库的头文件"
#include "hilog/log.h"
#include "napi/native_api.h"
#include <string>
#include <assert.h>
#define TAG "testTag"
static napi_value Add(napi_env env, napi_callback_info info) {
OH_LOG_Print(LOG_APP, LOG_INFO, 0, TAG, "Add is called");
size_t requireArgc = 2;
size_t argc = 2;
napi_status status;
napi_value args[2] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
napi_valuetype valuetype0;
napi_typeof(env, args[0], &valuetype0);
napi_valuetype valuetype1;
napi_typeof(env, args[1], &valuetype1);
double value0;
napi_get_value_double(env, args[0], &value0);
double value1;
napi_get_value_double(env, args[1], &value1);
napi_value sum;
//这里调用静态库的add函数
status = napi_create_double(env, add(value0, value1), &sum);
if (status != napi_ok) {
napi_throw_error(env, "-1", "napi_create_double failed");
return nullptr;
}
return sum;
}
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
OH_LOG_Print(LOG_APP, LOG_INFO, 0, TAG, "Init is called");
napi_property_descriptor desc[] = {
{ "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr }
};
napi_status status = napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
if (status != napi_ok) {
napi_throw_error(env, "-1", "Init napi_define_properties failed");
return nullptr;
}
return exports;
}
EXTERN_C_END
static napi_module demoModule = {
.nm_version =1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "library",
.nm_priv = ((void*)0),
.reserved = { 0 },
};
extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
OH_LOG_Print(LOG_APP, LOG_INFO, 0, TAG, "RegisterEntryModule is called");
napi_module_register(&demoModule);
}
CMakeLists.txt
# the minimum version of CMake.
cmake_minimum_required(VERSION 3.4.1)
project(so_test4)
set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${NATIVERENDER_ROOT_PATH}
${NATIVERENDER_ROOT_PATH}/include)
add_compile_options("-Werror=return-type")
add_library(entry SHARED hello.cpp)
target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so)
target_link_libraries(entry PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${OHOS_ARCH}/libets-native.a)
发帖会限制上传资源demo工程,有需要可以留言获取。
更多关于HarmonyOS 鸿蒙Next 引入静态库.a的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS(鸿蒙)Next中,引入静态库(.a文件)的过程主要涉及以下几个步骤:
-
准备静态库文件:确保你已经拥有编译好的静态库文件(.a文件),并且该库与目标平台的架构兼容。
-
配置项目文件:在HarmonyOS项目的
build.gradle
文件中,添加静态库的路径和依赖。例如:sourceSets { main { jniLibs.srcDirs = ['libs'] } }
-
放置静态库文件:将静态库文件(.a文件)放置在项目的
libs
目录下,或者你指定的其他目录中。 -
链接静态库:在
CMakeLists.txt
文件中,添加静态库的链接指令。例如:add_library(my_static_lib STATIC IMPORTED) set_target_properties(my_static_lib PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/libmylib.a) target_link_libraries(my_target my_static_lib)
-
编译项目:完成上述配置后,重新编译项目,确保静态库被正确链接和使用。
-
验证:在代码中调用静态库中的函数,确保功能正常。
通过以上步骤,你可以在HarmonyOS Next项目中成功引入并使用静态库(.a文件)。
更多关于HarmonyOS 鸿蒙Next 引入静态库.a的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中引入静态库.a,首先需要将静态库文件放置在项目的libs
目录下。然后在CMakeLists.txt
文件中,通过add_library
命令将静态库链接到项目中。例如:
add_library(my_static_lib STATIC IMPORTED)
set_target_properties(my_static_lib PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/libmy_static_lib.a)
target_link_libraries(my_target PRIVATE my_static_lib)
确保my_target
是你的目标可执行文件或库。这样,静态库.a就会被正确链接到你的项目中。