Thread: #define snprintf _snprintf

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    #define snprintf _snprintf

    Hello guys!

    My first post here ever

    I'm migrating a C++ project the first was done in visual studio to CodeComposer4 (IDE for embedded developing of Texas Instruments products).

    I have a definition:
    Code:
    #ifdef MSYS_WIN32
    #define snprintf _snprintf
    #endif*/
    and the using of snprintf:
    Code:
    snprintf( cFileName, 1023, "%s_ScalSEI.txt", pucBaseName );
    And there are an error on it: "identifier _snprintf in undefined".
    I tried to use
    Code:
    #include <cstdio>
    #include <stdio.h>
    (in the original file they aren't included) and nothing helps.
    The intresting part the in VS it works great! what could be the reason?

    Thanks, Stas.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The preceding #ifdef MSYS_WIN32 suggests that the macro is specific to one compiler (at a guess, the MINGW compiler suite, as that uses a suite of tools called MSYS), and possibly to one version of that compiler.

    When you are migrating to another compiler, you will need to work out what works. One unfortunate consequence of using compiler dependent hacks is that they need to be updated and confirmed to be working correctly every time you migrate to a new compiler.

    MSYS_WIN32 will not usually be defined when compiling with VS, so the substitution will not occur. If your VS is recent enough, it supports constructs from the 1999 C standard, so will not have a problem with your code.

    Your problem is that snprintf() was not part of the 1989/1990 C standard, and was not part of the C++ standard. It became standard in the 1999 C standard.

    Given you are working in C++, support of snprintf() will vary between compilers and compiler versions (some won't support it, some will support something similar like _snprintf(), some will support it).

    Given your circumstance, I'd probably attempt to eliminate all calls of snprintf() from the program you are trying to migrate. That may be painful now, but would make any future migrations easier.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Alternatively he could add a compile time define for MSYS_WIN32 ....
    The method of doing that would vary compiler to compiler but it would get around the problem.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    Thanks guys!

    CommonTater, how do i add a compile time define? what is this exactly?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Pre-defined C/C++ Compiler Macros
    Look up which symbol(s) are predeclared by your compiler. For example _MSC_VER is used by all (except possibly some ancient versions).

    Your code should look like
    Code:
    #ifdef MSYS_WIN32
    #define snprintf _snprintf
    #elif defined(_MSC_VER)
    // do something
    #else
    #error Need something specific for your compiler here
    #endif
    If you subsequently went to Borland say, you would get the #error the first time you tried to compile.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    If you project is actually C++, you should really be using something else instead, like stringstream. But if it's actually C, then you should compile your sources as such.

    See: stringstream - C++ Reference

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Yarin View Post
    If you project is actually C++, you should really be using something else instead, like stringstream. But if it's actually C, then you should compile your sources as such.
    I wouldn't go near iostreams with a 10 foot pole on an embedded system. For one thing, it presumes they even exist
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CommonTater View Post
    Alternatively he could add a compile time define for MSYS_WIN32 ....
    The method of doing that would vary compiler to compiler but it would get around the problem.
    Not for a compiler/library that does not support snprintf, or a function that can be directly substituted for it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 09-16-2009, 10:01 AM
  2. snprintf warning error
    By waxydock in forum C Programming
    Replies: 4
    Last Post: 06-03-2007, 09:22 AM
  3. _snprintf
    By mhandlon in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 09:37 AM
  4. _snprintf
    By mhandlon in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2006, 08:05 AM
  5. newbie: memset before snprintf
    By webwesen in forum C Programming
    Replies: 3
    Last Post: 04-25-2002, 02:06 PM