讨论广场 问答详情
添加BUILD.gn配置文件,其中cflags参数不起作用
weixin_53617138 2024-07-27 20:29:56
217 评论 分享

想要添加一个第三方库spdlog,按照官方提供的移植第三方库的方法尝试后,可以开始编译,但是编译报错

[OHOS ERROR] ../../third_party/spdlog/src/logger.h:323:9: error: cannot use 'try' with exceptions disabled
[OHOS ERROR]         SPDLOG_TRY {

经过查询发现是编译参数问题,想要在BUILD.gn文件中cflags添加如下参数

    cflags = [ 
      "-O3",
      "-std=c++11",
      "-DNDEBUG",
      "-fcxx-exceptions"
      ]

发现没有作用,通过编译日志也看到编译的时候这个参数没有加入到编译过程中

[OHOS ERROR] FAILED: obj/third_party/spdlog/src/spdlog_source/stdout_sinks.o 
[OHOS ERROR] /usr/bin/ccache ../../prebuilts/clang/ohos/linux-x86_64/llvm/bin/clang++ -MMD -MF obj/third_party/spdlog/src/spdlog_source/stdout_sinks.o.d -DV8_DEPRECATION_WARNINGS -D_GNU_SOURCE -DHAVE_SYS_UIO_H -D__MUSL__ -D_LIBCPP_HAS_MUSL_LIBC -D__BUILD_LINUX_WITH_CLANG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -DCOMPONENT_BUILD -D__GNU_SOURCE=1 -DCHROMIUM_CXX_TWEAK_INLINES -DNDEBUG -DNVALGRIND -DDYNAMIC_ANNOTATIONS_ENABLED=0 -Iobj/third_party/musl/usr/include/arm-linux-ohos -Ioverride/third_party -I../.. -Igen -fno-strict-aliasing -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -funwind-tables -fcolor-diagnostics -fmerge-all-constants -Xclang -mllvm -Xclang -instcombine-lower-dbg-declare=0 -no-canonical-prefixes -flto=thin -fsplit-lto-unit -ffunction-sections -fno-short-enums --target=arm-linux-ohos -march=armv7-a -mfloat-abi=softfp -mtune=generic-armv7-a -fstack-protector-strong -fPIC -mfpu=neon -mthumb -Wall -Werror -Wextra -Wimplicit-fallthrough -Wthread-safety -Wno-missing-field-initializers -Wno-unused-parameter -Wno-c++11-narrowing -Wno-unneeded-internal-declaration -Wno-error=c99-designator -Wno-error=anon-enum-enum-conversion -Wno-error=sizeof-array-div -Wno-error=implicit-fallthrough -Wno-error=reorder-init-list -Wno-error=range-loop-construct -Wno-error=deprecated-copy -Wno-error=implicit-int-float-conversion -Wno-error=inconsistent-dllimport -Wno-error=unknown-warning-option -Wno-error=sign-compare -Wno-error=int-in-bool-context -Wno-error=return-stack-address -Wno-error=dangling-gsl -Wno-unused-but-set-variable -Wno-deprecated-declarations -Wno-unused-but-set-parameter -Wno-null-pointer-subtraction -Wno-unqualified-std-cast-call -Wno-user-defined-warnings -Wno-unused-lambda-capture -Wno-null-pointer-arithmetic -Wno-enum-compare-switch -O2 -fno-ident -fdata-sections -ffunction-sections -fomit-frame-pointer -gdwarf-3 -g2 -ggnu-pubnames -fno-common -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -std=c++17 -fno-exceptions -fno-rtti --sysroot=obj/third_party/musl -fvisibility-inlines-hidden -c ../../third_party/spdlog/src/stdout_sinks.cpp -o obj/third_party/spdlog/src/spdlog_source/stdout_sinks.o

cflags,cflags_c和cflags_cc都尝试过,都无法起作用,下面是我的BUILD.gn,请问该如何正确添加编译参数。

import("//build/ohos.gni")

#需要包含的源文件
ohos_source_set("spdlog_source") {
    sources = [
        "//third_party/spdlog/src/spdlog.cpp",
        "//third_party/spdlog/src/stdout_sinks.cpp",
        "//third_party/spdlog/src/color_sinks.cpp",
        "//third_party/spdlog/src/file_sinks.cpp",
        "//third_party/spdlog/src/async.cpp",
        "//third_party/spdlog/src/cfg.cpp",
        "//third_party/spdlog/src/bundled_fmtlib_format.cpp"
    ]
}

##生成静态库
ohos_static_library("spdlog") {
    deps = [ ":spdlog_source" ]            #依赖的源文件
    include_dirs = [                    #依赖的头文件配置信息
        "//third_party/spdlog/include"
    ]
    part_name = "spdlog_lib"
    subsystem_name = "thirdparty"
    cflags = []
    cflags_cc = [ 
      "-O3",
      "-std=c++11",
      "-DNDEBUG",
      "-fcxx-exceptions"
      ]
}

 

217 评论 分享
写回答
全部评论(7)
4 楼

修改好BUILD.gn, 自验证这个库,在OH跑,也没有问题

 

 

 

2024-07-28 21:07:11
2024-07-29 10:55:07
特别感谢您的回答,我刚才仿照libbpf中的BUILD.gn调整了一下,现在的编译参数可以添加进去了。非常感谢
2024-07-29 10:55:07
2024-07-29 10:48:18
引用:“可以问一下您的编译方法吗,我当前的编译方法是添加到rk3568的产品解决方案config.json文件中,然后通过hb build -T spdlog,出现了问答中的错误。有可能是在bundle.json文件出问题吗?”
BUILD.gn如下,如果确认好,帮忙点采纳回答
2024-07-29 10:48:18
2024-07-29 10:41:36
可以问一下您的编译方法吗,我当前的编译方法是添加到rk3568的产品解决方案config.json文件中,然后通过hb build -T spdlog,出现了问答中的错误。有可能是在bundle.json文件出问题吗?
2024-07-29 10:41:36
2 楼

可以试试采用configs,如添加config:

使用添加的config:

 

 

2024-07-27 21:20:56
2024-07-28 14:00:14
引用:“configs改成public_configs呢”
仍旧不行
2024-07-28 14:00:14
2024-07-28 12:25:56
引用:“尝试过这种方法,仍然没有作用”
configs改成public_configs呢
2024-07-28 12:25:56
2024-07-28 11:13:20
尝试过这种方法,仍然没有作用
2024-07-28 11:13:20