android ndk开发中初始化char数组报错问题

Android Studio报错:

error: constant expression evaluates to -14 which cannot be narrowed to type 'char' [-Wc++11-narrowing]

错误位置在定义char数组的地方

官方说明

看看cflags中 fsigned-char的说明:

 -fsigned-char — Allows the type char in the native libraries to be signed, like signed char. Each kind of machine has a default for what char should be. It is either like unsigned charby default or like signed char by default. By default on Android NDK char type is unsigned, but char is treated as signed on x86. This is an option imposed by Superpowered SDK authors, so we’ll include this as well.
默认android ndk中char类型是unsigned,在x86上是signed类型,在ndk中,对于负数来说,char val = -3,printf出的值是253,而不是-3。

所以要修改app目下的build.gradle,在android节点下的externalNativeBuild->cmake里的cppFlags添加-fsigned-char选项

我这里使用的是CMake,如果使用的是android.mk,则添加:

LOCAL_CFLAGS := -fsigned-char

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值