By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,958 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,958 IT Pros & Developers. It's quick & easy.

Where is the error in this code?

P: n/a
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

Jan 22 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Jan 22, 3:19*pm, Erwin Lindemann <elind...@wupp.invalidwrote:
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().
Jan 23 '08 #2

P: n/a
Randy Howard <ra*********@FOOverizonBAR.netwrites:
On Tue, 22 Jan 2008 17:31:02 -0600, Walter Roberson wrote
(in article <fn**********@canopus.cc.umanitoba.ca>):
>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) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 23 '08 #3

P: n/a
user923005 <dc*****@connx.comwrote:
On Jan 22, 3:19 pm, Erwin Lindemann <elind...@wupp.invalidwrote:
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
^^^
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
Jan 23 '08 #4

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
user923005 <dc*****@connx.comwrote:
>On Jan 22, 3:19 pm, Erwin Lindemann <elind...@wupp.invalidwrote:
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
^^^
>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) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 23 '08 #5

P: n/a
On Jan 23, 9:14*am, Keith Thompson <ks...@mib.orgwrote:
r...@hoekstra-uitgeverij.nl (Richard Bos) writes:
user923005 <dcor...@connx.comwrote:
On Jan 22, 3:19 pm, Erwin Lindemann <elind...@wupp.invalidwrote:
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
* * * * * * * * * * * *^^^
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.
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.
Jan 23 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.