Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

error: conversion from 'double' to 'float' may change value [-Werror=float-conversion]

Aperta Adatta ai principianti
#963 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 12 giorni

@coveyjorjet ci sta già lavorando.

Dal 1/8/2026.

  • #1061 di @coveyjorjet — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
62/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
c, cmake
Ambito
build-system

Direzione di ricerca

Riproduci la build con warning rigorosi usando CMake FetchContent e ispeziona cJSON.c intorno alla riga 607 in print_number, che il report identifica come la posizione del fallimento. Confronta i diagnostici del compilatore con i controlli matematici circostanti ed esegui la build e i test di cJSON; l’attività è completata quando la libreria viene compilata correttamente con i flag di warning segnalati senza sopprimere l’errore.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

  1. 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)  
    
  2. Enable strict warnings (e.g., -Werror=float-conversion).
  3. 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:

  1. Explicitly casting the result to float (if precision loss is acceptable).
  2. Using -Wno-float-conversion for cJSON (not ideal for strict compliance).
  3. 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_hex4 function (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!

Lingua principale
C
Stelle
13k
Fork
3.5k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di DaveGamble/cJSON

Tutte le issue di DaveGamble/cJSON

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.