2014/12/01

Linux compiler warnings

没有评论:
When compiling C++ programs, gcc reports -Wwrite-strings warning.
xxx.cpp:344:5: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
Solution 1 (推荐):
     cast constant string from char * s = "constant string" to const char *s= "constant string".

Solution 2:
 you can also pass -Wno-write-strings to gcc which then suppresses this warning.

Solution 3:
#pragma GCC diagnostic ignored "-Wwrite-strings"    忽略警报
...
#pragma GCC diagnostic pop       重新打开警报


gcc reports -Wmultichar warning:
xxx.cpp:332:7: warning: multi-character character constant [-Wmultichar]
 #if (('1234' >> 24) == '1')     // little endian ?
       ^
这个是用宏判断机器是否little-endian/big-endian,一般主机都是little-endian.
Solution 1 (推荐):
Check endian-ness at run time:
 int IS_LITTLE_ENDIAN() {
  static const int NL_AT_END = 0x000A;
  return ((char*)(void*)&NL_AT_END)[0] == '\n';
 }

Solution 2:
pass -Wno-multichar  to gcc for suppressing this warning.

Further reading: http://zipcon.net/~swhite/docs/computers/languages/c_multi-char_const.html