[newbie-ish] gcc can't find SDL_Surface?

Hey, I completely new to SDL (been around the block a few times
with C though ;^) ). I didn’t see this addressed in the FAQs or in
the mailing list archive so I figured I’d go ahead and post it.
Using the 1.1.1-1 version of SDL with

[orion at localhost orion]$ gcc -v
Reading specs from /usr/lib/gcc-lib/i586-mandrake-linux/2.95.3/specs
gcc version 2.95.3 19991030 (prerelease)

on (code included below after signature for brevity), produces this error
message from gcc (grumbly c compiler? =) ):

[orion at localhost SDL]$ gcc -o sdltrial -lSDL -lpthread sdltrial.c
sdltrial.c: In function main': sdltrial.c:26: parse error before*‘
sdltrial.c:27: `screen’ undeclared (first use in this function)
sdltrial.c:27: (Each undeclared identifier is reported only once
sdltrial.c:27: for each function it appears in.)

What’s up with that? (yeah, I know, prerelease compiler, but hey, it’s
worked for ~4 kernel compiles without a hitch)

(To save you reading through the code if you wish, which is basically
lifted straight from the introduction on devolution.com which I’m trying
to work through, the part it’s complaining about is:
SDL_Surface *screen; (line 26)
screen = SDL_SetVideoMode(800,600,16, SDL_HWSURFACE);
)

Am I screwing up the #include statements somehow? The odd thing is, it
seems to be finding the previous SDL stuff OK (SDL_Init et al.).

Thanks in advance!

Mike

Michael Orion Jackson*
TAMS Class of 96/UT Class of 200?*
Random Quote:*
Is it rational to lust after inanimate machines this *
much? (search for SGI or Silicon Graphics on eBay…)
*******************************************************

/* compiled like this: gcc -o filename -lSDL -lpthread filename.c /
/
SDL library version is 1.1.1-1 */

#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>

void ShowBMP (char*,SDL_Surface*,int, int);

int main(int argc, char* argv[]) {

    char file[12] = "myimage.bmp";

    /* First, init. the library. */

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
            fprintf(stderr, "Unable to init SDL: %S\n", SDL_GetError());
            exit(1);
    } else {
            fprintf(stderr, "SDL initialized.\n");
    }
    atexit(SDL_Quit);

    /* Next, init. the video display plane: */

    SDL_Surface *screen; /* <--- this is the line it complains about */
    screen = SDL_SetVideoMode(800,600,16, SDL_HWSURFACE);
    if (screen == NULL) {
            fprintf(stderr, "Unable to set 800x600x16bpp: %s\n", SDL_GetError());
            exit(1);
    }

    /* Next, put something on it: (has to be a BMP for now, adjust
 * file name above) 
 */

    ShowBMP(file,screen,0,0);

    return 0;

}

void ShowBMP(char *file, SDL_Surface *screen, int x, int y) {

    SDL_Surface *image;
    SDL_Rect dest;

    image = SDL_LoadBMP(file);
    if (image == NULL) {
            fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
            return;
    }

    dest.x = x;
    dest.y = y;
    dest.w = image->w;
    dest.h = image->h;
    SDL_BlitSurface(image, NULL, screen, &dest);

    SDL_UpdateRects(screen, 1, &dest);

}

Am I screwing up the #include statements somehow?

I think so :slight_smile:

The odd thing is, it seems to be finding the previous SDL stuff OK
(SDL_Init et al.).

You include “SDL/SDL.h”, which contains SDL_Init, so that’s ok. But SDL.h
does things like this:
#include “SDL_video.h"
This would contain SDL_Surface, which now is missing because the include-dir
of SDL is not given to gcc. Would SDL.h include “SDL/SDL_video.h” etc then
it would be ok. You have to add “-I/usr/local/include/SDL” (or whatever it
is) to the gcc-command-line, that should fix it. Also have a look at the
"sdl-config”-program.

All the best,
robOn Sat, Mar 18, 2000 at 11:48:31AM -0600, Michael Orion Jackson wrote:

hi`

I never used C but I think a feature added to C++ is that you can
declare variables anywhere whereof with C you have to declare them at
the beginning of a scope. Simply try compiling with g++ or move the
declaration to the beginning of the function.

Michael Orion Jackson wrote:>

    /* Next, init. the video display plane: */

    SDL_Surface *screen; /* <--- this is the line it complains about */
    screen = SDL_SetVideoMode(800,600,16, SDL_HWSURFACE);


Daniel Vogel My opinions may have changed,
666 @ http://grafzahl.de but not the fact that I am right

I never used C but I think a feature added to C++ is that you can
declare variables anywhere whereof with C you have to declare them at
the beginning of a scope.

Yes, and a very nice feature it is, if used with caution :wink:
I should have looked at his code a bit longer.

Simply try compiling with g++ or move the declaration to the beginning of
the function.

I just converted a bunch of "new"s to mallocs and inserted a dozen “struct”-
keywords to keep c-compliance myself. Does everybody have c++ compilers
nowadays, and on other platforms too ? I’d say it’s safer to use straight
c if possible, like in this case.

All the best,
robOn Sat, Mar 18, 2000 at 07:45:49PM +0100, Daniel Vogel wrote:

I never used C but I think a feature added to C++ is that you can
declare variables anywhere whereof with C you have to declare them at
the beginning of a scope.

The new C standard (C99) accepts this as well, but it is still missing in gcc.