简体   繁体   中英

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

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, eg:

  • _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, ie 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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM