CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    setlocale() with LC_MESSAGES

    I'm building the Windows version of a cross-platform app. It was originally written for gcc (i.e. mingw, on Windows) but in my case I'm building with MSVC. One of the source modules has this line:-

    Code:
    setlocale (LC_MESSAGES, NULL);
    The above line crashes if I build with MSVC. I guess that's not surprising because MSVC doesn't have any support for LC_MESSAGES.

    But... what is it that doesn't support LC_MESSAGES? Is it MSVC - or does Windows itself not support LC_MESSAGES? I'm only asking because the same code built with mingw doesn't crash. Is that a happy accident? Or does mingw somehow manage to support this on Windows when MSVC can't??
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: setlocale() with LC_MESSAGES

    AFAIK, setlocale() only supports the following for category under VS. See https://msdn.microsoft.com/en-us/library/x99tb11d.aspx

    Code:
    // Locale categories
    #define LC_ALL          0
    #define LC_COLLATE      1
    #define LC_CTYPE        2
    #define LC_MONETARY     3
    #define LC_NUMERIC      4
    #define LC_TIME         5
    
    #define LC_MIN          LC_ALL
    #define LC_MAX          LC_TIME
    With mingw, what value is used for LC_MESSAGES?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: setlocale() with LC_MESSAGES

    Hi 2kaud - it seems to be defined in libintl.h and has the (slightly strange) value of 1729.

    BTW - when I said it crashes if I build with MSVC, I didn't mean that the build itself crashes... I meant that at run time, the resulting executable crashes at that point.

    Mingw will be using a completely different runtime library so it's quite possibly just a happy accident that a mingw build doesn't crash. I guess I'm trying to find out whether Windows itself will support values which MSVC doesn't support. Basically so I can either change the code to this...

    Code:
    #ifndef _WINDOWS
          setlocale (LC_MESSAGES, NULL);
    #endif
    or this...

    Code:
    #ifndef _MSC_VER
          setlocale (LC_MESSAGES, NULL);
    #endif
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: setlocale() with LC_MESSAGES

    Is it MSVC - or does Windows itself not support LC_MESSAGES?
    This has nothing to do with windows as setlocale() is part of the c-run time library (CRT) - and is therefore everything to do with VC. It looks like the VS CRT doesn't support LC_MESSAGES for a category - unlike some other compiler run-time libraries which do. For gcc on Linux for example, LC_MESSAGES is defined as 6.

    Try this at the top of the program after the includes

    Code:
    #ifndef LC_MESSAGES
       #define LC_MESSAGES LC_ALL
    #endif
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: setlocale() with LC_MESSAGES

    it seems to be defined in libintl.h and has the (slightly strange) value of 1729
    For my install of VS2015, LC_MESSAGES isn't defined - so from where is your code picking up a value for LC_MESSAGES? The file libintl.h is not part of VS and doesn't exist on my windows development machine. Is LC_MESSAGES being defined somewhere in the code if it isn't already defined?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: setlocale() with LC_MESSAGES

    Quote Originally Posted by 2kaud View Post
    Is LC_MESSAGES being defined somewhere in the code if it isn't already defined?
    No, it gets defined in libintl.h (remember that this is a cross-platform app).

    Quote Originally Posted by 2kaud View Post
    This has nothing to do with windows as setlocale() is part of the c-run time library (CRT)
    That's true. But what might be happening is that both CRT libraries are failing - it's just that the MSVC lib generates an error whereas maybe the mingw one doesn't. i.e. Windows itself might not be able to support values which are not supported by MSVC (and that's why MSVC doesn't offer them). I guess the only way to find out is to ask one of the mingw devs to test the return value from setlocale()
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: setlocale() with LC_MESSAGES

    No, it gets defined in libintl.h (remember that this is a cross-platform app).
    So the same source compiles correctly on different platforms.

    But the code needs to take account of platform differences - as well as development tool differences. libintl.h is not part of VS, though it is of mingw (gcc). So libintl.h shouldn't be used when compiling under VS and unless LC_MESSAGES is defined within the program code if not already defined, I would expect VS to complain with a compiler error.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured