Warnings are being treated as errors in Codeblocks

2020-05-09 18:05发布

问题:

I am trying to build a cpp program in codeblocks, that includes graphics.h file in it.

But I am getting a

warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

  initgraph(&gd, &gm, "");

The warning is treated as errors: I have modified all the linker settings to include graphics.h in codeblocks.

initgraph(&gd, &gm, "");

Codeblocks shows:

=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===

回答1:

The problem is that C++ nowadays const qualify all string literals, unlike C and pre-standard C++. TC++ and Borland BGI is from the pre-standard days, so it won't compile cleanly with standard C++.

Best work-around:

char empty[] = "";
initgraph(&gd, &gm, empty);

Quick & dirty work-around:

initgraph(&gd, &gm, (char*)"");

You might also want to ask yourself why you are using 30 years old outdated libs written for MS DOS.