LASTARG undeclared

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Scalpel

LASTARG undeclared

Post 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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:LASTARG undeclared

Post by Pype.Clicker »

cannot be solved without printf code, imho ...
Scalpel

Re:LASTARG undeclared

Post 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)
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:LASTARG undeclared

Post 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 ...
Scalpel

Re:LASTARG undeclared

Post 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... ???
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:LASTARG undeclared

Post 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 ...
Scalpel

Re:LASTARG undeclared

Post by Scalpel »

I'm sorry, but I don't understand what you mean. Could you explain further...?
Unspoken_Magi

Re:LASTARG undeclared

Post 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.
???
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:LASTARG undeclared

Post 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.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Re:LASTARG undeclared

Post 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 ...
Post Reply