views:

210

answers:

2

Hello,

Visual Studio 2008

I am using the following source code that compiles ok using linux gcc 4.4.1.

However, I am trying to compile on windows xp sp3 using VS 2008 compiling as c code.

I get a run-time error on the call to vfprintf. And also the __func__ gives me a compile error. "Undeclared identifier". I thought __func__ was defined in the stdarg.h file.

Many thanks for any advice,

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

void log_msg(FILE *out, const char *fmt, ...);

int main(void)
{
    printf("== Start program ===\n");

    log_msg(stderr, "[ %s ] : [ %s ] : [ %d ]\n", __FILE__, __func__, __LINE__);

    return 0;
}

void log_msg(FILE *out, const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    vfprintf(out, fmt, ap); /* run-time error here */
    va_end(ap);
}
+2  A: 

__func__ is a C99 construct and is unavailable in Visual Studio. You could try using __FUNCTION__ instead.

Other than that, your example works fine for me.

Paul Baker
Are you compiling on Visual Studio 2008 SP1?
robUK
Yes, VS2008 SP1
Paul Baker
Yes, works fine now. Thanks.
robUK
+1  A: 

Also __func__ is not defined in a header file, it's a pre-defined constant. See Can I substitute func into an identifier name in a C macro? for more.

dajobe