Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Compile error

Reply
Thread Tools

Compile error

 
 
Kaz Kylheku
Guest
Posts: n/a
 
      01-16-2012
On 2012-01-16, Keith Thompson <(E-Mail Removed)> wrote:
> Jorgen Grahn <(E-Mail Removed)> writes:
>> On Sun, 2012-01-15, Keith Thompson wrote:
>>> dspfun <(E-Mail Removed)> writes:
>>>> I get a compile/preprocessor error when trying to call/use the
>>>> following line:
>>>> openlog (__FUNCTION__, 0x02 | 0x01 | 0x08, (1<<3));
>>>>
>>>>
>>>> The openlog function is declared as:
>>>>
>>>> extern void openlog (__const char *__ident, int __option, int
>>>> __facility);
>>>
>>> Is "__const" a macro for "const"? If so, why not just use "const"?
>>> If it's to cater to compilers that don't support "const", I hardly think
>>> that's necessary these days.
>>>
>>> Why are the parameter names reserved identifiers? (The implementation
>>> is free to define "__ident" as a macro, which would break the
>>> declaration.)

>>
>> To be fair to Linux glibc (from where he must have copied the above),
>> the manual page uses the normal names.

>
> So, dspfun, where did the identifiers __ident et al come from?


Wow, this insipid buffoon is still going on about this, when it's been clear to
everyone that this declaration is from a system header that is making use of a
namespace reserved for implementors.

If the implementation of a standard header has a declaration like

extern int fputc(int ch, FILE *stream); /* no underscores */

this creates a problem if a program does something like this:

#define ch blah()
#include <stdio.h> /* now we have int fputc(int blah(), FILE *stream). */

This #define is allowed, because ch is not a keyword, and it is not an
identifier that is reserved with respect to this header file. It is the
header that is wrong by using unreserved identifiers in a way that is visible
to the program.

One solution is to not use names:

extern int fputc(int, FILE *);

but maybe library implementors sometimes want names there, which can sometimes
be helpful. Also, this issue occurs with struct and union member names, where
you can't omit the name:

struct stat {
dev_t st_dev; /* standard member; user cannot #define st_dev */

/*...*/

void *__padding[2]; /* for some kernel issue or whatever ... */

/*...*/
};

The library writer doesn't have to worry that user programs will
#define __padding ... because that is equivalent to sticking a fork into a
toaster.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-16-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) (Ben Pfaff) writes:
> Keith Thompson <(E-Mail Removed)> writes:
>> Jorgen Grahn <(E-Mail Removed)> writes:
>>> On Sun, 2012-01-15, Keith Thompson wrote:
>>>> dspfun <(E-Mail Removed)> writes:
>>>>> I get a compile/preprocessor error when trying to call/use the
>>>>> following line:
>>>>> openlog (__FUNCTION__, 0x02 | 0x01 | 0x08, (1<<3));
>>>>>
>>>>>
>>>>> The openlog function is declared as:
>>>>>
>>>>> extern void openlog (__const char *__ident, int __option, int
>>>>> __facility);
>>>>
>>>> Is "__const" a macro for "const"? If so, why not just use "const"?
>>>> If it's to cater to compilers that don't support "const", I hardly think
>>>> that's necessary these days.
>>>>
>>>> Why are the parameter names reserved identifiers? (The implementation
>>>> is free to define "__ident" as a macro, which would break the
>>>> declaration.)
>>>
>>> To be fair to Linux glibc (from where he must have copied the above),
>>> the manual page uses the normal names.

>>
>> So, dspfun, where did the identifiers __ident et al come from?

>
> On a glibc system:
>
> blp@blp:~$ grep -r __ident /usr/include
> /usr/include/sys/syslog.h:extern void openlog (__const char *__ident, int __option, int __facility);
> blp@blp:~$


Ok, that makes some sense, given that /usr/include/sys/syslog.h is
part of the implementation.

The name of a parameter in a function declaration (as opposed to a
definition) is ignored; there's no way in C for a caller to refer
to a parameter by name. The authors of syslog.h decided to use
reserved identifiers to avoid clashing with user-defined macros.
For example, if they had written:

extern void openlog (__const char *ident, int option, int facility);

and a user wrote:

#define ident 42
#include <sys/syslog.h>

then the declaration would be a syntax error.

The lesson here is that system header files are intended for the
compiler, not necessarily for human readers (unless you have a
specific need to dive into the gory details); they can have all sorts
of implementation-defined cruft that's of no interest to most users.
The way to get the declaration for openlog() is to read the man page,
which says:

#include <syslog.h>

void openlog(const char *ident, int option, int facility);

As for __const, that's a gcc-specific "alternate keyword".
It probably goes back to the pre-ANSI days when some compilers didn't
support the "const" keyword. It's equivalent (for gcc) to "const",
and there's no reason to use it or refer to it in your own code.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
dspfun
Guest
Posts: n/a
 
      01-17-2012
On 16 Jan, 23:17, Keith Thompson <(E-Mail Removed)> wrote:
> (E-Mail Removed) (Ben Pfaff) writes:
> > Keith Thompson <(E-Mail Removed)> writes:
> >> Jorgen Grahn <(E-Mail Removed)> writes:
> >>> On Sun, 2012-01-15, Keith Thompson wrote:
> >>>> dspfun <(E-Mail Removed)> writes:
> >>>>> I get a compile/preprocessor error when trying to call/use the
> >>>>> following line:
> >>>>> openlog (__FUNCTION__, 0x02 | 0x01 | 0x08, (1<<3));

>
> >>>>> The openlog function is declared as:

>
> >>>>> extern void openlog (__const char *__ident, int __option, int
> >>>>> __facility);

>
> >>>> Is "__const" a macro for "const"? *If so, why not just use "const"?
> >>>> If it's to cater to compilers that don't support "const", I hardly think
> >>>> that's necessary these days.

>
> >>>> Why are the parameter names reserved identifiers? *(The implementation
> >>>> is free to define "__ident" as a macro, which would break the
> >>>> declaration.)

>
> >>> To be fair to Linux glibc (from where he must have copied the above),
> >>> the manual page uses the normal names.

>
> >> So, dspfun, where did the identifiers __ident et al come from?

>
> > On a glibc system:

>
> > * * blp@blp:~$ grep -r __ident /usr/include
> > * * /usr/include/sys/syslog.h:extern void openlog (__const char *__ident, int __option, int __facility);
> > * * blp@blp:~$

>
> Ok, that makes some sense, given that /usr/include/sys/syslog.h is
> part of the implementation.
>
> The name of a parameter in a function declaration (as opposed to a
> definition) is ignored; there's no way in C for a caller to refer
> to a parameter by name. *The authors of syslog.h decided to use
> reserved identifiers to avoid clashing with user-defined macros.
> For example, if they had written:
>
> * * extern void openlog (__const char *ident, int option, int facility);
>
> and a user wrote:
>
> * * #define ident 42
> * * #include <sys/syslog.h>
>
> then the declaration would be a syntax error.
>
> The lesson here is that system header files are intended for the
> compiler, not necessarily for human readers (unless you have a
> specific need to dive into the gory details); they can have all sorts
> of implementation-defined cruft that's of no interest to most users.
> The way to get the declaration for openlog() is to read the man page,
> which says:
>
> * * #include <syslog.h>
>
> * * void openlog(const char *ident, int option, int facility);
>
> As for __const, that's a gcc-specific "alternate keyword".
> It probably goes back to the pre-ANSI days when some compilers didn't
> support the "const" keyword. *It's equivalent (for gcc) to "const",
> and there's no reason to use it or refer to it in your own code.
>
> --
> Keith Thompson (The_Other_Keith) (E-Mail Removed) *<http://www.ghoti.net/~kst>
> * * Will write code for food.
> "We must do something. *This is something. *Therefore, we must do this."
> * * -- Antony Jay and Jonathan Lynn, "Yes Minister"


Hi,

Thanks all for your help!

Here is more info regarding where the __const etc comes from in my
code:

My .c-file uses the macro TRACE_OBJ(GROUP,TRACE_OBJ,MSG) and this
macro is declared in the file

/usr/include/xxx_trace_obj.h

as

#define TRACE_OBJ(GROUP,TRACE_OBJ,MSG) LOG(LOG_INFO, STR("TRACE_OBJ:
%s", #TRACE_OBJ))

LOG_INFO is defined as
#define LOG_INFO 6

and STR is defined as
-------------------------
static char buf_s[1024];

static char* STR(const char *fmt, ...)
{
va_list args;

__builtin_va_start(args,fmt);
(void)vsprintf(buf_s, fmt, args);
__builtin_va_end(args);
return (char*)buf_s;
}
-------------------------

The LOG macro is declared in the file

/usr/include/slog.h

as

#define LOG(level, msg) do {\
openlog (__FUNCTION__, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);\
syslog (level, "%s", msg);\
closelog ();\
} while(0)


slog.h above includes the file
/usr/include/syslog.h"

which only contains

#include <sys/syslog.h>

and sys/syslog.h contains the declaration of openlog

/* Open connection to system logger.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern void openlog (__const char *__ident, int __option, int
__facility);

Brs,
Markus
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compile directive for conditional compile for Java 1.4 versus Java 5 timjowers Java 7 02-02-2011 12:08 AM
How to compile the following source code in VC6// I have error inVC++6 but compile ok in GCC fAnSKyer C++ 2 06-07-2009 07:57 AM
computation at compile time i.e. compile time functions usingtemplates Carter C++ 2 03-04-2009 06:43 PM
Compile versus not compile (VS 2005)?? stupid48@gmail.com ASP .Net 1 04-11-2008 08:24 PM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM



Advertisments