Can someone please tell me why Microsoft insists on prefixing all the functions that I love with an underscore? The stat, access, snprintf, and many others all start with an underscore. Is it because they just don't wany my unix code to compile?
Can someone please tell me why Microsoft insists on prefixing all the functions that I love with an underscore? The stat, access, snprintf, and many others all start with an underscore. Is it because they just don't wany my unix code to compile?
Nope. That's because their linker does it like that.
A simple fix for that is to create a header file like: fix_ms_nonesense.h
Then include that in all your projects.Code:#ifdef _MSC_VER # define stat _stat # define access _access ... #endif
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
While it's true that Microsoft's linker will prefix exported symbols with an underscore, that is not what Zlatko is talking about.Nope. That's because their linker does it like that.
Microsoft has a policy for putting the underscore on all functions in the CRT that are not part of the C standard. I don't know why they do this, but I doubt it is due to preventing people from easily writing cross platform code (although it is a side effect).
You don't need the leading underscore for those functions as long as __STDC__ is undefined or 0.
Predefined Macros
gg
I usually use the fix_ms_nonsense.h approach, and I'll continue with that. I'd rather have the compiler enforce standard C compliance. Thanks for all the replies.
I think they are adding the underscore to all POSIX functions...
If I am optimistic, I would guess they are doing it to make sure programmers can define functions with the same names as those "non standard" POSIX functions (since symbols with an underscore prefix are reserved for implementation use).
Why? I don't know. To make it more difficult to write protable programs? (since programmers can unknowingly declare functions with same names as POSIX functions). And I don't see them adding underscores to Win API functions...
Now they have a set of functions that only work on Windows (the _s functions), and a set of functions that only DON'T work on Windows (POSIX functions). Nice.