1

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);
}
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
ant2009
  • 27,094
  • 154
  • 411
  • 609
  • 1
    How do you get a runtime error, if you got a compile time error? What did you change to get it to compile? – Rob Walker Nov 10 '09 at 02:48
  • I wondered about the same thing as Rob Walker. Also, log_msg() as written is equivalent to fprintf(). Does fprintf() work okay ? Assuming it does, you could explore the difference with assembly level debug. – Bill Forster Nov 10 '09 at 03:06
  • I had a compile error with the __func__ so I commented it out and then ran the code without the %s and __func__. Then I got the error with the vfprintf. – ant2009 Nov 10 '09 at 03:13
  • 1
    As with so many questions about errors, it's really, really helpful to have the exact code that causes the error. What you've posted shows the compile error due to `__func__` not being supported in MSVC. Since you seem to be more interested in the runtime error with `vfprintf()`, it would be helpful to post that code so we're not guessing what it might be. Maybe the `%d` got taken out by mistake instead of one of the `%s` conversion specs, and when `__LINE__` is treated as a string - crash - because it's not a valid pointer. I dunno. – Michael Burr Nov 10 '09 at 06:48
  • Yes, you are correct. I shouldn't have put the code that was causing a problem. Maybe that caused some confusion. However, I wanted to put my complete source listing, plus the __func__ I was using. However, I did comment that was causing a compile error. I will try and be more precise next time. Thanks. – ant2009 Nov 10 '09 at 10:18
  • So, just curious - what was causing the `vfprintf()` crash without the `__func__` macro being used? – Michael Burr Nov 10 '09 at 16:37
  • I had a %s when it should have been a %d. Thanks. – ant2009 Nov 11 '09 at 09:35

2 Answers2

2

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

user200783
  • 13,722
  • 12
  • 69
  • 135
1

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.

Community
  • 1
  • 1
dajobe
  • 4,938
  • 35
  • 41