Connecting Tech Pros Worldwide Forums | Help | Site Map

Where is the error in this code?

Erwin Lindemann
Guest
 
Posts: n/a
#1: Jan 22 '08
Hi group!

I found the following code in a free unix program and used it
successfully in a couple of programs. But for some reason it
does not compile under Windows.

Here's the code:

--------
#include <stdarg.h>
#include <stdio.h>

static int dbg_lvl = 0;

int dlevel(int lvl)
{
int prvlvl = dbg_lvl;
if(lvl < 0 || lvl 7)
return -1;
dbg_lvl = lvl;
return prvlvl;
}

int dprintf(int lvl, const char *fmt, ...)
{
va_list ap;
int rv=0;
static char *s[] = {
"NONE", "INFO", "NOTICE", "WARNING",
"ERROR", "CRITICAL", "ALERT", "EMERG"
};

if(lvl <= dbg_lvl)
{
va_start(ap, fmt);
rv = fprintf(stderr, "%s: ", s[lvl]);
if(rv 0) rv += vfprintf(stderr, fmt, ap);
va_end(ap);
}
return rv;
}

#define TEST

#ifdef TEST
int main(void)
{
int i;
dlevel(7);

for(i = 0; i <= 8; i++)
dprintf(i, "debug msg at level %d\n", i);

return 0;
}
#endif
--------

The error message from the Windows compiler is:

--------
Error dbg.c: 16 redefinition of 'dprintf'
Error c:\Programme\lcc\include\stdio.h: 133 Previous definition of 'dprintf' here
2 errors, 0 warnings
1 error
--------

I tried my best, but I can't find the problem.

Thanks,
Erwin Lindemann


user923005
Guest
 
Posts: n/a
#2: Jan 23 '08

re: Where is the error in this code?


On Jan 22, 3:19*pm, Erwin Lindemann <elind...@wupp.invalidwrote:
Quote:
Hi group!
>
I found the following code in a free unix program and used it
successfully in a couple of programs. But for some reason it
does not compile under Windows.
>
Here's the code:
>
--------
#include <stdarg.h>
#include <stdio.h>
>
static int dbg_lvl = 0;
>
int dlevel(int lvl)
{
* int prvlvl = dbg_lvl;
* if(lvl < 0 || lvl 7)
* * return -1;
* dbg_lvl = lvl;
* return prvlvl;
>
}
>
int dprintf(int lvl, const char *fmt, ...)
{
* va_list ap;
* int rv=0;
* static char *s[] = {
* * "NONE", "INFO", "NOTICE", "WARNING",
* * "ERROR", "CRITICAL", "ALERT", "EMERG"
* };
>
* if(lvl <= dbg_lvl)
* {
* * va_start(ap, fmt);
* * rv = fprintf(stderr, "%s: ", s[lvl]);
* * if(rv 0) rv += vfprintf(stderr, fmt, ap);
* * va_end(ap);
* }
* return rv;
>
}
>
#define TEST
>
#ifdef TEST
int main(void)
{
* int i;
* dlevel(7);
>
* for(i = 0; i <= 8; i++)
* * dprintf(i, "debug msg at level %d\n", i);
>
* return 0;}
>
#endif
--------
>
The error message from the Windows compiler is:
>
--------
Error dbg.c: 16 *redefinition of 'dprintf'
Error c:\Programme\lcc\include\stdio.h: 133 *Previous definition of 'dprintf' here
2 errors, 0 warnings
1 error
--------
>
I tried my best, but I can't find the problem.
Call your routine Dprintf() instead of dprintf().
Keith Thompson
Guest
 
Posts: n/a
#3: Jan 23 '08

re: Where is the error in this code?


Randy Howard <randyhoward@FOOverizonBAR.netwrites:
Quote:
On Tue, 22 Jan 2008 17:31:02 -0600, Walter Roberson wrote
(in article <fn5ubm$cdo$1@canopus.cc.umanitoba.ca>):
Quote:
>Windows has a non-standard routine named dprintf() that it declares
>in its stdio.h . (There is also a dprintf() that is in the GNU glibc2
>library.)
>
So they are broken in much the same way lcc-win32 is, as seen in a
recent thread.
Actually, I think the stdio.h file is provided by the compiler, not by
Windows. Note that the file name in the compiler's error message was
"c:\Programme\lcc\include\stdio.h".

The identifier "dprintf" is available for user code; if the
implementation interferes with this in conforming mode, that's a bug.
The OP should contact the compiler vendor.

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Richard Bos
Guest
 
Posts: n/a
#4: Jan 23 '08

re: Where is the error in this code?


user923005 <dcorbit@connx.comwrote:
Quote:
On Jan 22, 3:19 pm, Erwin Lindemann <elind...@wupp.invalidwrote:
Quote:
The error message from the Windows compiler is:

--------
Error dbg.c: 16 redefinition of 'dprintf'
Error c:\Programme\lcc\include\stdio.h: 133 Previous definition of 'dprintf' here
^^^
Quote:
Call your routine Dprintf() instead of dprintf().
That's the wrong solution. The right solution is to use a conforming
implementation. This is the second such gaffe we've seen in a week.

Richard
Keith Thompson
Guest
 
Posts: n/a
#5: Jan 23 '08

re: Where is the error in this code?


rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:
Quote:
user923005 <dcorbit@connx.comwrote:
>
Quote:
>On Jan 22, 3:19 pm, Erwin Lindemann <elind...@wupp.invalidwrote:
Quote:
The error message from the Windows compiler is:
>
--------
Error dbg.c: 16 redefinition of 'dprintf'
Error c:\Programme\lcc\include\stdio.h: 133 Previous definition
of 'dprintf' here
^^^
>
Quote:
>Call your routine Dprintf() instead of dprintf().
>
That's the wrong solution. The right solution is to use a conforming
implementation. This is the second such gaffe we've seen in a week.
I agree that changing the name isn't a good solution, but it's a
reasonable short-term workaround. It's a lot easier than switching to
another compiler (which might have bugs of its own).

On the other hand, if you were thinking of switching compilers anyway,
this could be one more reason to do so.

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
user923005
Guest
 
Posts: n/a
#6: Jan 23 '08

re: Where is the error in this code?


On Jan 23, 9:14*am, Keith Thompson <ks...@mib.orgwrote:
Quote:
r...@hoekstra-uitgeverij.nl (Richard Bos) writes:
Quote:
user923005 <dcor...@connx.comwrote:
>
Quote:
Quote:
On Jan 22, 3:19 pm, Erwin Lindemann <elind...@wupp.invalidwrote:
The error message from the Windows compiler is:
>
Quote:
Quote:
--------
Error dbg.c: 16 redefinition of 'dprintf'
Error c:\Programme\lcc\include\stdio.h: 133 Previous definition
of 'dprintf' here
* * * * * * * * * * * *^^^
>
Quote:
Quote:
Call your routine Dprintf() instead of dprintf().
>
Quote:
That's the wrong solution. The right solution is to use a conforming
implementation. This is the second such gaffe we've seen in a week.
>
I agree that changing the name isn't a good solution, but it's a
reasonable short-term workaround. *It's a lot easier than switching to
another compiler (which might have bugs of its own).
>
On the other hand, if you were thinking of switching compilers anyway,
this could be one more reason to do so.
Also, if the compiler is invoked in conforming mode and acts in a non-
conforming manner, issue a defect report to the compiler vendor with
enough information to reproduce the problem. Generally speaking, this
is a good way to increase the quality of software tools of any sort.
Closed Thread