1

I am trying to compile a really old software in linux debian 9.5, i keep getting this error:

janpdf/PDF.cpp: In member function ‘void PDF::OpenFile(const char*)’:
janpdf/PDF.cpp:41:74: error: narrowing conversion of ‘199’ from ‘int’ to 
‘char’ inside { } [-Wnarrowing]
char signature[] = {'%', '%', 'G' + 128, 'R' + 128, 'A' + 128, '\n', 0};
                                                                      ^
janpdf/PDF.cpp:41:74: error: narrowing conversion of ‘210’ from ‘int’ to 
‘char’ inside { } [-Wnarrowing]
janpdf/PDF.cpp:41:74: error: narrowing conversion of ‘193’ from ‘int’ to 
‘char’ inside { } [-Wnarrowing]
Makefile:153: recipe for target 'janpdf/PDF.o' failed
make: *** [janpdf/PDF.o] Error 1

I have already tried teh signed / unsigned 'char'approach. Although I know almos nothing about coding this is the only answer I found. Any other solution is welcome. thanks

  • A `char` may be either signed or unsigned - implementation defined. If it is signed then `199` won't fit. Seems your implementation uses signed chars. – Jesper Juhl Aug 15 '18 at 15:58

3 Answers3

3

Well, apparently in your implementation values like 210 and 199 do not fit into the range of type char. So, the conversion is narrowing. {} initializers do not allow narrowing conversions.

This suggests that your implementation apparently uses signed char type.

You can forcefully convert the values to char by using explicit casts inside the {}. You can stop using {} initializers. You can force your implementation to use unsigned char. There are many "solutions" for this problem, but there's no way to chose one without more context.

If the code was originally written for the same "family" of implementations you are compiling it on now, then most likely it was simply written for an older version of the language, which performed that narrowing conversion implicitly. In that case to reproduce the old behavior you'll need explicit casts

char signature[] = 
  {'%', '%', (char) ('G' + 128), (char) ('R' + 128), (char) ('A' + 128), '\n', 0};
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 1
    Looks like he's just trying to compile some dependency that was written in an old version of the language. Depending on the size of it, editing the source may not be a viable strategy. – Not a real meerkat Aug 15 '18 at 15:52
  • The lowest effort edits are to either change `128` to `-128` - the generated bitwise pattern will be as intended, and the value will fit in signed char; or change char to unsigned char, which may have other consequences. – Gem Taylor Aug 15 '18 at 18:16
1

The lowest-effort way to get your thing to build is probably to add -Wno-narrowing to your compiler invocation. If you're using make, you can probably start it with something like CFLAGS=-Wno-narrowing make (assuming you're using bash) to get the desired effect.

zneak
  • 134,922
  • 42
  • 253
  • 328
  • I wouldn't recomment to just disable the error and keep using C++11(or C++14). There are subtle changes in the meaning of some constructs that were introduced in those versions of the language, and those may cause silent bugs. – Not a real meerkat Aug 15 '18 at 16:01
  • I got pretty much same waring/error while making wxGTK-2.8.12. Based on the Ben's answer from https://stackoverflow.com/questions/20302595/how-to-disable-narrowing-conversion-warnings , I successfully built the file by adding " -Wno-c++11-narrowing" to CFLAGES var in MakeFile – bf39L Feb 03 '21 at 01:19
0

Current compilers use by default newer versions of C++. Your compiler may be trying to compile the source in C++11 or C++14 mode.

Try adding -std=c++03 to your compiler flags.

Not a real meerkat
  • 5,604
  • 1
  • 24
  • 55