error: conversion from 'double' to 'float' may change value [-Werror=float-conversion]
维护者通常 12 天内回复
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 62/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 冷清
- 技术栈
- c, cmake
- 领域
- build-system
调研方向
使用 CMake FetchContent 重现严格警告构建,并检查报告认定为失败位置的 print_number 中 cJSON.c 第 607 行附近。将编译器诊断与周围的数学检查进行比较,并运行 cJSON 构建和测试;当库在不抑制错误的情况下使用报告的警告标志成功构建时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Description
I encountered a compilation error when integrating cJSON into my C++ project via CMake's FetchContent. The build fails at line 607 in cjson.c with the following error:
error: conversion from 'double' to 'float' may change value [-Werror=float-conversion]
Environment
- cJSON Version:
v1.7.19 - Compiler:
gcc version 15.1.0 (x86_64-win32-seh-rev0, Built by MinGW-Builds project) - Build System:
cmake version 4.0.2 (CMake; JetBrains IDE bundle; build 1) - OS: Windows 11
Steps to Reproduce
- Configure CMake to include cJSON via FetchContent:
include(FetchContent) FetchContent_Declare( cjson # actualy I downloaded form release and use `SOURSE_DIR` to include it GIT_REPOSITORY https://github.com/DaveGamble/cJSON.git GIT_TAG v1.7.19 ) FetchContent_MakeAvailable(cjson) target_link_libraries(my_target cjson) - Enable strict warnings (e.g.,
-Werror=float-conversion). - Build the project.
Error Log
"D:\software\CLion 2024.3.5\bin\cmake\win\x64\bin\cmake.exe" --build D:\projects\prj-terminal\cmake-build-debug --target prj_terminal -j 22
[1/3] Building C object _deps/cjson-build/CMakeFiles/cjson.dir/cJSON.c.obj
FAILED: _deps/cjson-build/CMakeFiles/cjson.dir/cJSON.c.obj
D:\software\mingw64\bin\cc.exe -DCJSON_API_VISIBILITY -DCJSON_EXPORT_SYMBOLS -DENABLE_LOCALES -Dcjson_EXPORTS -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2 -Wcast-qual -Wundef -Wswitch-default -Wconversion -Wc++-compat -fstack-protector-strong -Wdouble-promotion -Wparentheses -Wformat-overflow -Wunused-macros -Wmissing-variable-declarations -Wswitch-enum -fvisibility=hidden -g -std=gnu11 -fdiagnostics-color=always -MD -MT _deps/cjson-build/CMakeFiles/cjson.dir/cJSON.c.obj -MF _deps\cjson-build\CMakeFiles\cjson.dir\cJSON.c.obj.d -o _deps/cjson-build/CMakeFiles/cjson.dir/cJSON.c.obj -c D:/projects/prj-terminal/third_party/cJSON-1.7.19/cJSON.c
In file included from D:/projects/prj-terminal/third_party/cJSON-1.7.19/cJSON.c:42:
D:/projects/prj-terminal/third_party/cJSON-1.7.19/cJSON.c: In function 'print_number':
D:/projects/prj-terminal/third_party/cJSON-1.7.19/cJSON.c:607:15: error: conversion from 'double' to 'float' may change value [-Werror=float-conversion]
607 | if (isnan(d) || isinf(d))
| ^
D:/projects/prj-terminal/third_party/cJSON-1.7.19/cJSON.c:607:27: error: conversion from 'double' to 'float' may change value [-Werror=float-conversion]
607 | if (isnan(d) || isinf(d))
| ^
cc1.exe: all warnings being treated as errors
ninja: build stopped: subcommand failed.
Proposed Solution
The error occurs due to an implicit double-to-float conversion.
I asked ai, and it told me that to resolve this, consider:
- Explicitly casting the result to
float(if precision loss is acceptable). - Using
-Wno-float-conversionfor cJSON (not ideal for strict compliance). - Refactoring the calculation to avoid precision loss warnings.
Additional Context
- This issue arises when compiling with strict warning flags (e.g.,
-Werror). - The problematic code is in the
parse_hex4function (line 607 in latest master).
I wander if there is a way to solve this without remove the flag Werror=float-conversion
I'm still learning Cpp and Cmake. So if you would be willing to help me, I would greatly appreciate it. Thank you for reading my issue. Let me know if you need more details!
- 主要语言
- C
- 星标
- 13k
- 派生
- 3.5k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
DaveGamble/cJSON 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
DaveGamble/cJSON#1106 ·
维护者通常 12 天内回复
-
Undefined-Behavior in function cJSON_SetNumberHelper可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 70/100
DaveGamble/cJSON#1100 · 1 个 reaction ·
维护者通常 12 天内回复
-
难度 2/5 1-3 小时 新手友好度 86/100
DaveGamble/cJSON#1094 · 9 条评论 · 1 个 reaction ·
维护者通常 12 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
DaveGamble/cJSON#1093 ·
维护者通常 12 天内回复
-
Use-after-free in cJSONUtils_ApplyPatches when a patch removes the patch array可能已有人在做 @iliasabk 于 33 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
DaveGamble/cJSON#1082 ·
维护者通常 12 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 66/100
维护者通常 1 天内回复
-
Bug Status: Needs Triage
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
interpreter-core type-feature
难度 1/5 1 小时以内 新手友好度 78/100
维护者通常 1 天内回复
-
category:port-update
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 2 天内回复
-
Update OpenBSD CI to OpenBSD 8.0 and vmactions/openbsd-vm v1.4.9可能已有人在做 @hyoklee 今天认领。 未关闭
难度 1/5 1-3 小时 新手友好度 85/100
维护者通常 2 天内回复