Why are standard library function names different between Windows and Linux?

寵の児 提交于 2020-01-03 09:55:14

问题


I am porting a Windows library to Android (with the GNU Standard C++ Library option, libstdc++-v3) and there seem to be numerous naming differences between the VC and GNU libraries, e.g.:

  • _stricmp is called strcasecmp instead
  • _unlink is called unlink
  • _scalb is called scalbn
  • _finite is called isfinite
  • _isnan is called isnan
  • _itoa and itoa do not seem to exist in GNU C++
  • atoi does exist, but not atoi64

The documentation of both VC and GNU libraries implies that they implement "ISO" C++, for example I can get a few warnings out of VC2008 for not using "ISO C++" names, such as this one: "warning C4996: 'itoa': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _itoa." Similarly GNU's manual says "The GNU Standard C++ Library v3 is an ongoing project to implement the ISO 14882 Standard C++ library".

So how do the libraries end up with these different names? How can I tell which names are more "standard"?

Also, is there an index of libstdc++-v3 anywhere, i.e. a simple list of all functions in the library? I can only find a manual and the "source documentation" which does not appear to offer a list of functions.


回答1:


This has very little to do with the C++ standard library. It has more to do with C99 and POSIX.

  • strcasecmp is a POSIX function that libstdc++ happens to implement. msvcrt typically stays at arm's length from POSIX.
  • unlink is similar—it's a POSIX function.
  • scalbn is the name of the function in the C99 standard. MSVC doesn't support C99. However, scalbn is part of C++11, so I would expect it to show up in msvcrt eventually.
  • isfinite and isnan are both C99.
  • itoa is neither C99 nor POSIX. It's a strange beast that just shows up in the night.

I'll also point out what several others have pointed out: it's technically more correct to prefix any functions in the standard library that are actually non-standard with an underscore. That's the reason for the proliferation of underscores in msvcrt.




回答2:


None of them are standard. The standard does say that you shouldn't add names, though so that is what the warnings actually mean. The underscore makes them standards compliant because they won't be confused as something in standard C/C++.

They imply that by adding the underscore that your code will be using something in the standard, but neither version are.




回答3:


I guess any implementation can call their implementation "ISO standard" if they prepend _ to the function names and do whatever they want, since those identifiers are guaranteed to be reserved to the implementation by the standard. But they are also not guaranteed to be portable either.



来源:https://stackoverflow.com/questions/9896411/why-are-standard-library-function-names-different-between-windows-and-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!