Microsoft and underscores in function names

This is a discussion on Microsoft and underscores in function names within the C Programming forums, part of the General Programming Boards category; Can someone please tell me why Microsoft insists on prefixing all the functions that I love with an underscore? The ...

  1. #1
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132

    Microsoft and underscores in function names

    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?

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Nope. That's because their linker does it like that.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    A simple fix for that is to create a header file like: fix_ms_nonesense.h
    Code:
    #ifdef _MSC_VER
    #  define stat _stat
    #  define access _access
    ...
    #endif
    Then include that in all your projects.
    "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

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Nope. That's because their linker does it like that.
    While it's true that Microsoft's linker will prefix exported symbols with an underscore, that is not what Zlatko is talking about.

    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).

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    You don't need the leading underscore for those functions as long as __STDC__ is undefined or 0.
    Predefined Macros

    gg

  6. #6
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    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.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    It does have standard compliance. You are using non-standard functions, that's all.
    You may want to say that you want it to stop being so childish about the underscores instead
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error LNK2001: unresolved external symbol
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-12-2002, 08:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21