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 ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
DaveGamble/cJSON のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
DaveGamble/cJSON#1106 ·
メンテナーはふだん 12 日以内に返信
-
Undefined-Behavior in function cJSON_SetNumberHelper対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
DaveGamble/cJSON#1100 · リアクション 1 件 ·
メンテナーはふだん 12 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
DaveGamble/cJSON#1094 · コメント 9 件 · リアクション 1 件 ·
メンテナーはふだん 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 が 32 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
DaveGamble/cJSON#1082 ·
メンテナーはふだん 12 日以内に返信
DaveGamble/cJSON の issue をすべて見る
似ている issue
-
v9.6.0: CMake incorrectly enforces presence of libevdev対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
メンテナーはふだん 1 日以内に返信
-
IoTSDK
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
Azure/azure-iot-sdk-c#2776 ·
メンテナーはふだん 2 日以内に返信
-
[Bug] Unhandled ValueError terminates watchdog thread when GISRC value contains multiple colonsオープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
bug Component component: net
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
RT-Thread/rt-thread#11913 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
contiki-ng/contiki-ng#3289 ·
メンテナーはふだん 3 日以内に返信