Simple SDL Problem

Hi,

Can anyone tell me why the program below doesn’t compile?

#include <stdlib.h>
#include "SDL.h"

main(int argc, char *argv[])
{
	if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
	{
    	fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
    	exit(1);
	}
	atexit(SDL_Quit);

	SDL_Surface *screen;
	screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
	if ( screen == NULL )
	{
    	fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
    	exit(1);
	}
}

When I run "gcc sdltest.c -o sdltest sdl-config --cflags --libs sdl" I get the error below…

Usage: sdl-config [--prefix[=DIR]] [--exec-prefix[=DIR]] [--version] [--libs] [--cflags]
sdlinit.c: In function `main':
sdlinit.c:13: parse error before `*'
sdlinit.c:14: `screen' undeclared (first use in this function)
sdlinit.c:14: (Each undeclared identifier is reported only once
sdlinit.c:14: for each function it appears in.)

Can anyone help?

Darrell

This is really a C problem, but…

Hi,

Can anyone tell me why the program below doesn’t compile?

#include <stdlib.h>
#include “SDL.h”

main(int argc, char *argv[])
{
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, “Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

  SDL_Surface *screen;
          screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);

In C, you can’t declare new variables anywhere within your functions.
They need to go at the top (just below “{” after ‘main(…)’ :slight_smile: )

When I run "gcc sdltest.c -o sdltest sdl-config --cflags --libs sdl" I get the error below…

Usage: sdl-config [–prefix[=DIR]] [–exec-prefix[=DIR]] [–version] [–libs] [–cflags]

Also, you’re calling ‘sdl-config’ with an unknown argument (“sdl” after
"–libs"), hence this Usage error…

sdlinit.c: In function main': sdlinit.c:13: parse error before*’

(See above :slight_smile: )

sdlinit.c:14: `screen’ undeclared (first use in this function)
sdlinit.c:14: (Each undeclared identifier is reported only once
sdlinit.c:14: for each function it appears in.)

(Caused by the above parse error)

-bill!On Mon, Feb 25, 2002 at 06:14:17PM +0000, Darrell Blake wrote:

The ‘sdl’ option to ‘sdl-config’ doesn’t exist, so it’s choking on that,
first of all.
Do it as:

gcc sdltest.c -o sdltest sdl-config --cflags --libs

Another issue I’ve sometimes seen with gcc is that they want you to
declare your variables before anything else is done, so you may want to
move that ‘SDL_Surface *screen;’ to the beginning of main(). Not
entirely sure of the reason for this, I never felt like looking it up.–
Michael Vieths
Foeclan at Visi.com

On 25 Feb 2002, Darrell Blake wrote:

Hi,

Can anyone tell me why the program below doesn’t compile?

#include <stdlib.h>
#include “SDL.h”

main(int argc, char *argv[])
{
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, “Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

  SDL_Surface *screen;
	screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
	if ( screen == NULL )
  {
    	fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
    	exit(1);
	}

}

When I run "gcc sdltest.c -o sdltest sdl-config --cflags --libs sdl" I get the error below…

Usage: sdl-config [–prefix[=DIR]] [–exec-prefix[=DIR]] [–version] [–libs] [–cflags]
sdlinit.c: In function main': sdlinit.c:13: parse error before*‘
sdlinit.c:14: `screen’ undeclared (first use in this function)
sdlinit.c:14: (Each undeclared identifier is reported only once
sdlinit.c:14: for each function it appears in.)

Can anyone help?

Darrell

Maybe because in C, all declarations of variables must occur before any
functions have been executed or assignments have been made.

Try putting the declaration for screen before the SDL_Init command.

SteveOn February 25, 2002 02:44 pm, you wrote:

Hi,

Can anyone tell me why the program below doesn’t compile?

#include <stdlib.h>
#include “SDL.h”

main(int argc, char *argv[])
{
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, “Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

  SDL_Surface *screen;
	screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
	if ( screen == NULL )
  {
    	fprintf(stderr, "Unable to set 640x480 video: %s\n",

SDL_GetError()); exit(1);
}
}

When I run "gcc sdltest.c -o sdltest sdl-config --cflags --libs sdl"
I get the error below…

Usage: sdl-config [–prefix[=DIR]] [–exec-prefix[=DIR]] [–version]
[–libs] [–cflags] sdlinit.c: In function main': sdlinit.c:13: parse error before*‘
sdlinit.c:14: `screen’ undeclared (first use in this function)
sdlinit.c:14: (Each undeclared identifier is reported only once
sdlinit.c:14: for each function it appears in.)

Can anyone help?

Darrell

Hello,

I’ve compiled your code sucessfully under win32 with dev-c++, but you have
forgotten the return type of main(it’s the only one thing I’ve changed).
So, the problem might be in the command line for compiling your project?

@+

Hi,

Can anyone tell me why the program below doesn’t compile?

#include <stdlib.h>
#include “SDL.h”

main(int argc, char *argv[])
{
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, “Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

SDL_Surface *screen;
screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
if ( screen == NULL )
{
fprintf(stderr, “Unable to set 640x480 video: %s\n”,
SDL_GetError());
exit(1);
}
}

When I run "gcc sdltest.c -o sdltest sdl-config --cflags --libs sdl" I
get the error below…

Usage: sdl-config [–prefix[=DIR]] [–exec-prefix[=DIR]] [–version]
[–libs] [–cflags]
sdlinit.c: In function main': sdlinit.c:13: parse error before*‘
sdlinit.c:14: `screen’ undeclared (first use in this function)
sdlinit.c:14: (Each undeclared identifier is reported only once
sdlinit.c:14: for each function it appears in.)

Can anyone help?

Darrell

GCC is a C compiler, the above code is not legal C. It is legal C++ however
(as far as I can tell) so it should compile with G++. To make it legal C (to
compile with GCC) move the SDL_Surface *screen; line to the top of the main
function. C++ allows “inline variables” (not sure if this is the right
term), in C you must specify variables at the top of a function or at the
global scope.

Dirk Gerrits

No. It’s just because he’s using a C compiler, not a C++ compiler.
In C++, I believe you can delare new variables int i; wherever you
char c; want float f; :slight_smile:

-bill!On Mon, Feb 25, 2002 at 01:32:25PM -0500, Paflecanif at aol.com wrote:

Hello,

I’ve compiled your code sucessfully under win32 with dev-c++, but you have
forgotten the return type of main(it’s the only one thing I’ve changed).
So, the problem might be in the command line for compiling your project?

ok, thanks! i didn’t know!
thanks a lot!
@+

Try adding a return value to main()

Hi,

Can anyone tell me why the program below doesn’t compile?

#include <stdlib.h>
#include “SDL.h”

main(int argc, char *argv[])
{
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, “Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

SDL_Surface *screen;
screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
if ( screen == NULL )
{
fprintf(stderr, “Unable to set 640x480 video: %s\n”,
SDL_GetError());
exit(1);
}
}

When I run "gcc sdltest.c -o sdltest sdl-config --cflags --libs sdl" I
get the error below…

Usage: sdl-config [–prefix[=DIR]] [–exec-prefix[=DIR]] [–version]
[–libs] [–cflags]> sdlinit.c: In function main': sdlinit.c:13: parse error before*‘
sdlinit.c:14: `screen’ undeclared (first use in this function)
sdlinit.c:14: (Each undeclared identifier is reported only once
sdlinit.c:14: for each function it appears in.)

Can anyone help?

Darrell


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

GCC is a C compiler, the above code is not legal C. It is legal C++ however

GCC is the GNU Compiler Collection.
It currently contains compilers for C,C++,Fortran,Java and others.
So it’s not C.

(as far as I can tell) so it should compile with G++. To make it legal C (to

The gcc frontend decides by file-endings how sources should be compiled.
the g++ frontend therefore is just to let the compiler interprete the
source as c++ code independently on the file-ending.

compile with GCC) move the SDL_Surface *screen; line to the top of the main
function. C++ allows “inline variables” (not sure if this is the right
term), in C you must specify variables at the top of a function or at the
global scope.

Dirk Gerrits


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Zordan E.

compile with GCC) move the SDL_Surface *screen; line to the top of the
main
function. C++ allows “inline variables” (not sure if this is the right
term), in C you must specify variables at the top of a function or at
the
global scope.

Also not true. In C variables must be declared at the beginning of the
local scope or
in global space. It has nothing to do with functions per se.

int main ()
{
int i = 0;
printf ("…");/* whatever/*
{
int j = 0;
}
}

thats also legal in C last time I checked.>> Dirk Gerrits


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Zordan E.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

GCC is a C compiler, the above code is not legal C. It is legal C++
however

GCC is the GNU Compiler Collection.
It currently contains compilers for C,C++,Fortran,Java and others.
So it’s not C.

(as far as I can tell) so it should compile with G++. To make it legal C
(to

The gcc frontend decides by file-endings how sources should be compiled.
the g++ frontend therefore is just to let the compiler interprete the
source as c++ code independently on the file-ending.

compile with GCC) move the SDL_Surface *screen; line to the top of the
main

function. C++ allows “inline variables” (not sure if this is the right
term), in C you must specify variables at the top of a function or at
the

global scope.

Dirk Gerrits

Zordan E.

Well I’m mostly a Windows user so sorry for my Linux ignorance. :slight_smile:
But the line he used to compile was:
"gcc sdltest.c -o sdltest sdl-config --cflags --libs sdl"
and that clearly compiles in C not C++ right?

Dirk Gerrits

"gcc sdltest.c -o sdltest sdl-config --cflags --libs sdl"
and that clearly compiles in C not C++ right?

Yes, that compiles as C, not C++.

AND…you need to take “sdl” off the end of that command:

gcc sdltest.c -o sdltest sdl-config --cflags --libs

(The other things people said about where you declare variables in C
vs C++ still hold true. Move your variables to the top of the block, etc.)

–ryan.

compile with GCC) move the SDL_Surface *screen; line to the top of the
main
function. C++ allows “inline variables” (not sure if this is the right
term), in C you must specify variables at the top of a function or at
the
global scope.

Also not true. In C variables must be declared at the beginning of the
local scope or
in global space. It has nothing to do with functions per se.

int main ()
{
int i = 0;
printf ("…");/* whatever/*
{
int j = 0;
}
}

thats also legal in C last time I checked.

Mainly correct, but since the C99 standard, declaring variables
in the middle of the code is now legal C.

gcc is improving C99 support in the 3.X.X build line, please
check http://gcc.gnu.org/gcc-3.0/c99status.html for more
information.


Paulo Pinto (aka Moondevil in demoscene)
pjmlp_pt at yahoo.comhttp://www.progtools.org
"If you think education is expensive, try ignorance" - Derek Bok


Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

----- Original Message -----
From: leimbacd@bellsouth.net (David Leimbach)
To:
Sent: Wednesday, February 27, 2002 5:18 AM
Subject: Re: [SDL] Simple SDL Problem

Also not true. In C variables must be declared at the beginning of the
local scope or
in global space. It has nothing to do with functions per se.

int main ()
{
int i = 0;
printf ("…");/* whatever/*
{
int j = 0;
}
}

thats also legal in C last time I checked.

Mainly correct, but since the C99 standard, declaring variables
in the middle of the code is now legal C.

There are MANY new additions to C99. Variable length arrays are one of
them yet I don’t know any implementations of C99 to be “done”. Nor do I
believe anyone should start
writing all their C code in C99 standard conformant style since its less
likely to be portable. Other than that you are 100% correct.

gcc is improving C99 support in the 3.X.X build line, please
check http://gcc.gnu.org/gcc-3.0/c99status.html for more
information.

I have gcc-3.0.4 on my FreeBSD box. Thanks for the link all the same!> —

Paulo Pinto (aka Moondevil in demoscene)
pjmlp_pt at yahoo.comhttp://www.progtools.org
"If you think education is expensive, try ignorance" - Derek Bok


Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl