`

implicit declaration of function `sleep' 问题

阅读更多
A very simple test program, no locking or other precautions:

/* Program name: testscreen1.c */
/* SDL screen test */

/* Always needed for SDL programs: */
# include <SDL.h>

/* For (at)exit: */
# include <stdlib.h>

int main()
{
  SDL_Surface *screen;
  if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
        exit(1);
  }
  atexit(SDL_Quit);
 
  screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
    if ( screen == NULL ) {
        fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
        exit(1);
    }
 
  printf("Set window of 640x480 at %d bits per pixel mode\n",
        screen->format->BitsPerPixel);
 
}

This defines a screen structure (from /usr/include/SDL/SDL_video.h), a special sort of variable that holds all kinds of information about the window we will be drawing.

Then we initialize the video subsystem, and again we specify to quit graciously whenever the time should come.

Then we give the physical dimensions of the screen, the colour depth and the type of surface we're drawing. From the documentation in the SDL_video.h we know that this declaration defines a surface in system memory (as opposed to one in video memory).

To the terminal we print a message, and then we quit.

The Makefile was adapted to build a different executable:

引用
[tille@octarine ~/C/SDL/screen] cat Makefile
CC  = gcc -Wall
BIN = testscreen1
CFLAGS=`sdl-config --cflags`
LDFLAGS=`sdl-config --libs`
 
all: $(BIN)
 
$(BIN): testscreen1.o
        $(CC) testscreen1.o \
        -o $(BIN) $(LDFLAGS)
 
clean:
        @rm -f *.o
        @rm $(BIN)

Type make (it gives a warning about non-void functions, because I tend to forget the return(0) statement, see the end of this section) and run your program.

That was too fast!
Even on this slow machine, so we will tell the program to wait a bit, using the sleep function:

/* Program name: testscreen2.c */
/* SDL screen test 2 */

/* Always needed with SDL: */
# include <SDL.h>

/* For general functions: */
# include <stdlib.h>

int main()
{
  SDL_Surface *screen;
  if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
        exit(1);
  }
  atexit(SDL_Quit);
                                                                                 
  screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
    if ( screen == NULL ) {
        fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
        exit(1);
    }
                                                                                 
  printf("Set window of 640x480 at %d bits per pixel mode\n",
        screen->format->BitsPerPixel);

/* Show the window a bit longer: */
  sleep(5);
                                                                                 
  printf("Quitting SDL.\n");
}

Now we can start using the proverbial "edit the Makefile" cliche, again change the file names. Preferable even make a new directory for each program that you are working on.

Now if you run make like this, it will not work, because the sleep function is not defined in any of the libraries we included up until now. It will go like this:

引用
[tille@octarine ~/C/SDL/screen/test2] make
gcc -Wall `sdl-config --cflags`   -c -o testscreen2.o testscreen2.c
testscreen2.c: In function `main':
testscreen2.c:23: warning: implicit declaration of function `sleep'
testscreen2.c:26: warning: control reaches end of non-void function
gcc -Wall testscreen2.o \
-o testscreen2 `sdl-config --libs`

But never despair, we can depend on the man pages:

引用
[tille@octarine ~/C/SDL/screen/test2] apropos sleep
Tcl_Sleep            (3)  - delay execution for a given number of milliseconds
Tcl_Sleep [Sleep]    (3)  - delay execution for a given number of milliseconds
apmsleep             (1)  - go into suspend or standby mode and wake-up later
nanosleep            (2)  - pause execution for a specified time
sleep                (1)  - delay for a specified amount of time
sleep                (3)  - Sleep for the specified number of seconds
usleep               (1)  - sleep some number of microseconds
usleep               (3)  - suspend execution for microsecond intervals

And from man 3 sleep:

引用
SLEEP(3)                   Linux Programmer's Manual                  SLEEP(3)


NAME
       sleep - Sleep for the specified number of seconds
                                                                                     
SYNOPSIS
       #include <unistd.h>
                                                                                     
       unsigned int sleep(unsigned int seconds);
                                                                                     
DESCRIPTION
       sleep()  makes  the  current  process  sleep until seconds seconds have
       elapsed or a signal arrives which is not ignored.
                                                                                     
RETURN VALUE
       Zero if the requested time has elapsed, or the number of  seconds  left
       to sleep.       
                                                       


So we have to include as it says, and our program will look like this:

/* Program name: testscreen2.c */
/* SDL screen test 2 */
 
/* Always needed for SDL programs: */
# include <SDL.h>

/* For atexit: */
# include <stdlib.h>
 
/* For the sleep function: */
# include <unistd.h>
 
int main()
{
  SDL_Surface *screen;
  if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
        exit(1);
  }
  atexit(SDL_Quit);
 
  screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
    if ( screen == NULL ) {
        fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
        exit(1);
    }
 
  printf("Set window of 640x480 at %d bits per pixel mode\n",
        screen->format->BitsPerPixel);
 
  sleep(5);
 
  printf("Quitting SDL.\n");
}

If you run make now, all should go reasonably well:

引用
[tille@octarine ~/C/SDL/screen/test2] make clean
[tille@octarine ~/C/SDL/screen/test2] make
gcc -Wall `sdl-config --cflags`   -c -o testscreen2.o testscreen2.c
testscreen2.c: In function `main':
testscreen2.c:34: warning: control reaches end of non-void function
gcc -Wall testscreen2.o \
-o testscreen2 `sdl-config --libs`

Warnings do not stop a program from being compiled, errors do, so we will get an executable file out of this. To get rid of the warnings, add return(0) at the end of the program, so that main returns a value, and rerun make.

And if you run the program, it should stay on your screen a bit longer now, and output this in your terminal window:

引用
[tille@octarine ~/C/SDL/screen/test2] ls
Makefile  testscreen2*  testscreen2.c  testscreen2.o

[tille@octarine ~/C/SDL/screen/test2] ./testscreen2
Set window of 640x480 at 16 bits per pixel mode
Quitting SDL.

原文
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics