HarmonyOS 鸿蒙Next如何在.cpp文件中调用Hilog打印log
HarmonyOS 鸿蒙Next如何在.cpp文件中调用Hilog打印log
如上操作后编译项目报错
说是找不到OHOSHiLogPrint。
就想问下大佬,如何在cpp文件中打印log??
完整报错信息如下
15:48:32: Executing task ‘assembleDebug’…
Executing tasks: [assembleDebug]
> Task :entry:preBuild
FAILED: E:/project/MyApplication2/entry/build/intermediates/cmake/debug/obj/arm64-v8a/libhello.so
cmd.exe /C "cd . && C:\Users\chenjiac\AppData\Local\Huawei\Sdk\native\2.0.1.93\llvm\bin\clang++.exe --target=aarch64-linux-ohos --gcc-toolchain=C:/Users/chenjiac/AppData/Local/Huawei/Sdk/native/2.0.1.93/llvm --sysroot=C:/Users/chenjiac/AppData/Local/Huawei/Sdk/native/2.0.1.93/sysroot -fPIC -g -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,–noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info --rtlib=compiler-rt -fuse-ld=lld -Wl,–build-id=sha1 -Wl,–warn-shared-textrel -Wl,–fatal-warnings -lunwind -Wl,–no-undefined -Qunused-arguments -Wl,-z,noexecstack -shared -Wl,-soname,libhello.so -o E:\project\MyApplication2\entry\build\intermediates\cmake\debug\obj\arm64-v8a\libhello.so CMakeFiles/hello.dir/hello.cpp.o -lm && cd ."
ld.lld: error: undefined symbol: OHOSHiLogPrint
>>> referenced by hello.cpp:14 (E:/project/MyApplication2/entry/src/main/cpp\hello.cpp:14)
>>> CMakeFiles/hello.dir/hello.cpp.o:(Java_com_gizwits_opensource_myapplication_slice_MainAbilitySlice_stringFromJNI)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
Cmake thread
org.gradle.api.GradleException: native build cmake execute failed
at com.huawei.ohos.build.utils.ProcessUtils.processWait(ProcessUtils.groovy:104)
at com.huawei.ohos.build.utils.ProcessUtils.execute(ProcessUtils.groovy:90)
at com.huawei.ohos.build.utils.ProcessUtils.execute(ProcessUtils.groovy)
at com.huawei.ohos.build.utils.ProcessUtils$execute$1.callCurrent(Unknown Source)
at com.huawei.ohos.build.utils.ProcessUtils.execute(ProcessUtils.groovy:66)
at com.huawei.ohos.build.utils.ProcessUtils$execute$0.callCurrent(Unknown Source)
at com.huawei.ohos.build.utils.ProcessUtils.execute(ProcessUtils.groovy:62)
at com.huawei.ohos.build.utils.ProcessUtils$execute.call(Unknown Source)
at com.huawei.ohos.build.dsl.CmakeThread.executeCmakeBuild(CmakeThread.groovy:106)
at com.huawei.ohos.build.dsl.CmakeThread.run(CmakeThread.groovy:58)
Exception in thread “Thread-2149” org.gradle.api.GradleException: native build cmake execute failed
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:80)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:249)
at com.huawei.ohos.build.utils.ProcessUtils.processWait(ProcessUtils.groovy:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.runtime.callsite.PlainObjectMetaMethodSite.doInvoke(PlainObjectMetaMethodSite.java:43)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:190)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:58)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:184)
at com.huawei.ohos.build.utils.ProcessUtils.execute(ProcessUtils.groovy:90)
at com.huawei.ohos.build.utils.ProcessUtils.execute(ProcessUtils.groovy)
at com.huawei.ohos.build.utils.ProcessUtils$execute$1.callCurrent(Unknown Source)
at com.huawei.ohos.build.utils.ProcessUtils.execute(ProcessUtils.groovy:66)
at com.huawei.ohos.build.utils.ProcessUtils$execute$0.callCurrent(Unknown Source)
at com.huawei.ohos.build.utils.ProcessUtils.execute(ProcessUtils.groovy:62)
at com.huawei.ohos.build.utils.ProcessUtils$execute.call(Unknown Source)
at com.huawei.ohos.build.dsl.CmakeThread.executeCmakeBuild(CmakeThread.groovy:106)
at com.huawei.ohos.build.dsl.CmakeThread.run(CmakeThread.groovy:58)
> Task :entry:compileDebugNativeWithCmake FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ‘:entry:compileDebugNativeWithCmake’.
> native build cmake execute failed
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use ‘–warning-mode all’ to show the individual deprecation warnings.
See https://docs.gradle.org/6.3/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 15s
2 actionable tasks: 2 executed
15:48:47: Task execution finished ‘assembleDebug’.
开发者您好,已经帮您去问开发人员了,请稍等!
CMakeLists.txt
target_link_libraries(hello hilog_ndk.z)
感觉这个也可行,只要找到对应的库,怎么链接都可行!
开发者您好,让您久等了,您加上这句就可以了:
#pragma comment(lib, "libhilog_ndk.z.so")
加在哪里啊?OH4.1也有这样的问题
在HarmonyOS鸿蒙Next系统中,若你需要在.cpp
文件中调用Hilog
打印日志,可以按照以下步骤进行:
-
包含头文件: 首先,确保你的
.cpp
文件中包含了Hilog
的头文件。通常这个头文件是<hilog/log.h>
。#include <hilog/log.h>
-
初始化模块ID: 定义一个模块ID,通常这个ID是在你的项目或系统中唯一标识日志来源的。你可以根据需求定义,但一般遵循项目的命名规范。
#define MY_MODULE_ID 0x0001
-
调用Hilog打印日志: 使用
HILOG_INFO
、HILOG_WARN
、HILOG_ERROR
等宏来打印不同级别的日志。HILOG_INFO(MY_MODULE_ID, "This is an info log"); HILOG_WARN(MY_MODULE_ID, "This is a warning log"); HILOG_ERROR(MY_MODULE_ID, "This is an error log");
确保你的编译环境已经正确配置了HarmonyOS的SDK,并且你的项目已经正确链接了必要的库。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html