Page 1 of 1

LASTARG undeclared

Posted: Mon Mar 24, 2003 6:54 am
by Scalpel
I'm working on getting the (...) to work in functions.

I've studied stdarg.h, and have my own args.h that is pretty similar.

The problem is this; when I try to compile I get this error:

Code: Select all

sources/libc/printf.c: In function `printf':
sources/libc/printf.c:10: `AP' undeclared (first use in this function)
sources/libc/printf.c:10: (Each undeclared identifier is reported only once
sources/libc/printf.c:10: for each function it appears in.)
sources/libc/printf.c:10: `LASTARG' undeclared (first use in this function)
sources/libc/printf.c:10: `TYPE' undeclared (first use in this function)
I've searched through the sources for Linux v0.01, Moebius, Giggle and a few others, but can't find anyone who have set these anywhere.

Re:LASTARG undeclared

Posted: Mon Mar 24, 2003 7:49 am
by Pype.Clicker
cannot be solved without printf code, imho ...

Re:LASTARG undeclared

Posted: Mon Mar 24, 2003 8:02 am
by Scalpel
Well, the printf just looks like this (doesn't do anything)

Code: Select all

#include <args.h>

void printf(char *msg, ...)     //perhaps make this static??
{
        char curchar = *msg;
        char* ch;
        char* str;

        va_list args;
        va_start(args,msg);
}
and my args.h looks like this:

Code: Select all

#ifndef __ARGS_H
#define __ARGS_H

#define va_start ( AP, LASTARG ) \
        (AP = ((va_list) & (LASTARG) + VA_SIZE(LASTARG)))

#define va_arg                  (AP, TYPE)      (AP += __va_rounded_size (TYPE), * ((TYPE*)(AP - __va_rounded_size(TYPE))))
#define __va_rounded_size       (TYPE)          (((sizeof(TYPE) + sizeof(int)-1)) / sizeof (int)) * sizeof(int))
#define VA_SIZE                 (TYPE)          ((sizeof(TYPE) + sizeof(STACKITEM)-1) & ~(sizeof(STACKITEM)-1))

#define STACKITEM int

#define va_end                  (AP)

typedef unsigned char *va_list;

#endif
The error comes when the compiler meets the last line in my printf.c file.

Code: Select all

va_start(args,msg)

Re:LASTARG undeclared

Posted: Mon Mar 24, 2003 8:16 am
by Pype.Clicker
it looks like the compiler doesn't like your multi-line macros (it may occur when a dos-based file is given to a unix GCC ... try to put the whole macro on a single line and see what happens ...

you could also try to invoke "gcc -E print.c" to see what the preprocessor have made to your code ...

Re:LASTARG undeclared

Posted: Mon Mar 24, 2003 9:45 am
by Scalpel
The args.h file actually looked like this to begin with, with va_start on one line:

Code: Select all

#define va_start                (AP, LASTARG)   (AP = ((va_list) & (LASTARG) + VA_SIZE(LASTARG)))
with the macro on one line, but it still produces the same error.

And the command: gcc -E -I./includes sources/libc/printf.c
gives:

Code: Select all

void printf(char *msg, ...)
{
(...cut away...)

        va_list args;
        (AP, LASTARG)   (AP = ((va_list) & (LASTARG) + (TYPE)           ((sizeof(TYPE) + sizeof(int )-1) & ~(sizeof(int )-1)) (LASTARG))) (args,msg);

(...cut away...)
}
Can't see anything wrong with that... ???

Re:LASTARG undeclared

Posted: Mon Mar 24, 2003 10:11 am
by Pype.Clicker
i do. va_args should have been a 2-arguments macro, and now it's a non-argument macro which expansion starts with (AP, ...)

Try to remove all the spaces va_args and the first '(' character. It should fix your problem ...

Re:LASTARG undeclared

Posted: Mon Mar 24, 2003 2:37 pm
by Scalpel
I'm sorry, but I don't understand what you mean. Could you explain further...?

Re:LASTARG undeclared

Posted: Mon Mar 24, 2003 7:46 pm
by Unspoken_Magi
Try this:

Code: Select all

#define va_start ( void AP, void LASTARG ) \
        (AP = ((va_list) & (LASTARG) + VA_SIZE(LASTARG)))
Not sure if that would work... maybe replace "void" with your data type.
???

Re:LASTARG undeclared

Posted: Tue Mar 25, 2003 1:33 am
by distantvoices
You won't need the void-words in a macro definition:

the expression defined with
#define va_start (AP,LASTARG ) \
(AP = ((va_list) & (LASTARG) + VA_SIZE(LASTARG)))
is a macro.

if you write somewhere in your code:

va_start(args,msg)

it is replaced with this:

args = ((va_list) & (msg) + VA_SIZE(msg));

at VA_SIZE, the c precompiler expands the corresponding macro.

Thats a fine thing with these macros but do not muddle them up with real functions!

stay safe.

Re:Re:LASTARG undeclared

Posted: Tue Mar 25, 2003 1:51 am
by Pype.Clicker
Scalpel wrote:
I'm sorry, but I don't understand what you mean. Could you explain further...?
if you have

Code: Select all

#define min(x,y) (x<y?x:y)
the preprocessor will replace min(foo,bar) by (foo<bar?foo:bar).

Now if you have

Code: Select all

#define YELLOW (0xff,0xff,0x00)
it will replace fill YELLOW with fill (0xff,0xff,0x00)

The preprocessor is sensible to white space and do only limited syntax analyzis, so for it,

Code: Select all

#define va_start     ( PA, START) ...
is different from

Code: Select all

 #define va_start(PA,  START) ... 
in the way that the first one is just replacing any occurence of the word "va_start" by everything that is after the first space until the end of the line, and the second one will replace occurences of va_start( ... , ... ) by the pattern where argument susbstitutions have been applied.

Hope this is clearer.
If not, you should experiment it, and make some foo/bar tries with the preprocessor until its behaviour becomes clear ...