LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-10-2004, 09:37 AM   #1
unholy
Member
 
Registered: Sep 2003
Location: Eire
Distribution: Ubuntu 7.10
Posts: 344

Rep: Reputation: 30
gcc cant find SDL.h, but its there!


When I do 'gcc example1.c', I get the following output:
Code:
[unholy@localhost example1]$ gcc example1.c
example1.c:10:17: SDL.h: No such file or directory
...
locate SDL.h yields:
Code:
/usr/include/SDL/SDL.h
This seems bizzare to me. Maybe it's a good time to mention that I'm using Mandrake 9.2

I hope someone can help, I'm getting interested in this area.

Thanks in advance,
unholy
 
Old 01-10-2004, 09:47 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Did you pass -lSDL as an option to gcc?
 
Old 01-10-2004, 09:52 AM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Oh ya... I think you will need a -lpthread to go along with that if you compiled SDL with pthreads support... which you probably did.
 
Old 01-10-2004, 09:56 AM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Sorry for the million replies.... but make sure you do an #include <SDL/SDL.h> not just an include <SDL.h> as well.
 
Old 01-10-2004, 11:57 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
According to the SDL doc's the best (most compatible) way to include SDL.h is using double quotes instead of < and >:
Code:
#include "SDL.h"
Then compile, and in this example also link with:
Code:
gcc -g -Wall `sdl-config --cflags`  `sdl-config --libs`-o yourprog yourprog.c
Run "sdl-config --cflags" from the shell to see what happens here. It outputs the commandline option for gcc to look for SDL.h in the rights place on the system that runs it. Likewise, try: "sdl-config --libs". This outputs where the library file are.

"sdl-config --cflags" is only needed for compiling, and "sdl-config --libs" is only needed when linking. The example above compiles and links in one step, so it needs both.

Last edited by Hko; 01-10-2004 at 12:06 PM.
 
Old 01-10-2004, 05:41 PM   #6
unholy
Member
 
Registered: Sep 2003
Location: Eire
Distribution: Ubuntu 7.10
Posts: 344

Original Poster
Rep: Reputation: 30
Wow - and thanks jtshaw and Hko! Theres more to it than I thought. Changing the #include to "" instead of <> didnt make a difference in this instance.

Changing the SDL.h to SDL/SDL.h did work, but the libraries included by that file couldnt be found so I dont thind thats the best way to go.

I ran sdl-config with the options given by Hko, and things are better, but still I have compilation errors. Heres the output.

Code:
[unholy@localhost example1]$ gcc -g -Wall -D_REENTRANT -lSDL -lpthread -o example1 example1.c
example1.c: In function `DrawPicture':
example1.c:86: warning: unused variable `r'
example1.c:86: warning: unused variable `g'
example1.c:86: warning: unused variable `b'
example1.c: In function `main':
example1.c:128: warning: unsigned int format, pointer arg (arg 2)
/home/unholy/tmp/ccGQh3CS.o(.text+0x38): In function `setpixel':
/home/unholy/programming/example1/example1.c:23: undefined reference to `SDL_LockSurface'
/home/unholy/tmp/ccGQh3CS.o(.text+0x6a):/home/unholy/programming/example1/example1.c:28: undefined reference to `SDL_MapRGB'
/home/unholy/tmp/ccGQh3CS.o(.text+0x1f6):/home/unholy/programming/example1/example1.c:79: undefined reference to `SDL_UnlockSurface'
/home/unholy/tmp/ccGQh3CS.o(.text+0x27f): In function `DrawPicture':
/home/unholy/programming/example1/example1.c:96: undefined reference to `SDL_Flip'
/home/unholy/tmp/ccGQh3CS.o(.text+0x2a5): In function `main':
/home/unholy/programming/example1/example1.c:110: undefined reference to `SDL_Init'
/home/unholy/tmp/ccGQh3CS.o(.text+0x2b7):/home/unholy/programming/example1/example1.c:111: undefined reference to `SDL_GetError'
/home/unholy/tmp/ccGQh3CS.o(.text+0x2e2):/home/unholy/programming/example1/example1.c:117: undefined reference to `SDL_Quit'
/home/unholy/tmp/ccGQh3CS.o(.text+0x300):/home/unholy/programming/example1/example1.c:120: undefined reference to `SDL_SetVideoMode'
/home/unholy/tmp/ccGQh3CS.o(.text+0x317):/home/unholy/programming/example1/example1.c:122: undefined reference to `SDL_GetError'
/home/unholy/tmp/ccGQh3CS.o(.text+0x3ba):/home/unholy/programming/example1/example1.c:136: undefined reference to `SDL_PollEvent'
collect2: ld returned 1 exit status
Thanks again!
unholy
 
Old 01-10-2004, 05:47 PM   #7
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Is that gcc statement equivilent to the statement that Hko told you to try?
(gcc -g -Wall `sdl-config --cflags` `sdl-config --libs`-o example1 example1.c)

I know when I do a sdl-config --cflags and sdl-config --libs there is a lot more then that on my system.

If that doesn't work it is going to be hard to fix without seeing the code itself.
 
Old 01-11-2004, 07:06 AM   #8
unholy
Member
 
Registered: Sep 2003
Location: Eire
Distribution: Ubuntu 7.10
Posts: 344

Original Poster
Rep: Reputation: 30
sdl-config --cflags produces this:
Code:
[unholy@localhost unholy]$ sdl-config --cflags
-I/usr/include/SDL -D_REENTRANT
And sdl-config --libs
Code:
[unholy@localhost unholy]$ sdl-config --libs
-L/usr/lib -Wl,-rpath,/usr/lib -lSDL -lpthread
Typing the command as Hko wrote doesnt work for me
Code:
[unholy@localhost example1]$ gcc -g -Wall 'sdl-config --cflags' 'sdl-config --libs'-o example1 example1.c
                                                                                
gcc: sdl-config --cflags: No such file or directory
gcc: sdl-config --libs-o: No such file or directory
gcc: example1: No such file or directory
example1.c: In function `DrawPicture':
example1.c:86: warning: unused variable `r'
example1.c:86: warning: unused variable `g'
example1.c:86: warning: unused variable `b'
example1.c: In function `main':
example1.c:128: warning: unsigned int format, pointer arg (arg 2)
But replacing the 'sdl-config --cflags' and 'sdl-config --libs' with just the switches (that is, as I did in the above post stops the first two errors.
 
Old 01-11-2004, 07:11 AM   #9
Pres
Member
 
Registered: Jun 2002
Location: Australia
Distribution: Slack 9.1
Posts: 232

Rep: Reputation: 30
You should try the little groover under your tilde ( ~ ) instead of the apostrophes .... this guy ( ` ) not this guy ( ' ). Tricky isn't it.
 
Old 01-11-2004, 07:31 AM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Pres is right. You should use "backquotes" instead of normal, single quotes around sdl-config --cflags an sdl-config --libs.

If you are using bash as the shell (you probably do), then you can use $(sdl-config --cflags) and $(sdl-config --libs) also if that is more clear to you.

If you want your program to compile without errors on other systems, you should create a Makefile and put in the sdl-config stuff in the gcc commands, and not the output sdl-config generated on your system. This is because sdl-config will/may produce different output on slackware then on debian or SuSE or whatever, because the libraries/header live (may) live in different directories on those other systems. Also, to be more compatible with other systems, it's better to use backquotes instead of the $(...) I mentioned above, because that's bash-specific, and not every system runs bash.

The same goes for "SDL.h" instead of <SDL.h>. It may not make any difference on your machine, but it may on other systems. Like I mentioned before, the SDL doc's recommend the double-quotes for best compatibility.
 
Old 01-11-2004, 07:32 AM   #11
unholy
Member
 
Registered: Sep 2003
Location: Eire
Distribution: Ubuntu 7.10
Posts: 344

Original Poster
Rep: Reputation: 30
I didnt spot that, thanks. Now my erronous output is reduced to this..
Code:
[unholy@localhost example1]$ gcc -g -Wall `sdl-config --cflags` `sdl-config --libs`-o example1 ./example1.c
gcc: example1: No such file or directory
example1.c: In function `DrawPicture':
example1.c:86: warning: unused variable `r'
example1.c:86: warning: unused variable `g'
example1.c:86: warning: unused variable `b'
example1.c: In function `main':
example1.c:128: warning: unsigned int format, pointer arg (arg 2)
 
Old 01-11-2004, 07:37 AM   #12
unholy
Member
 
Registered: Sep 2003
Location: Eire
Distribution: Ubuntu 7.10
Posts: 344

Original Poster
Rep: Reputation: 30
Hko, thanks, I am using the double quotes. I think I should post the code at this stage..
Code:
/*****************************************************
 SDL Tutorial - Example 1
 Andrew M. <andrew@textux.com>
*****************************************************/ 
   
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "SDL.h"


void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
  Uint8 *ubuff8;
  Uint16 *ubuff16;
  Uint32 *ubuff32;
  Uint32 color;
  char c1, c2, c3;
  
  /* Lock the screen, if needed */
  if(SDL_MUSTLOCK(screen)) {
    if(SDL_LockSurface(screen) < 0) 
      return;
  }
  
  /* Get the color */
  color = SDL_MapRGB( screen->format, r, g, b );
  
  /* How we draw the pixel depends on the bitdepth */
  switch(screen->format->BytesPerPixel) 
    {
    case 1: 
      ubuff8 = (Uint8*) screen->pixels;
      ubuff8 += (y * screen->pitch) + x; 
      *ubuff8 = (Uint8) color;
      break;

    case 2:
      ubuff8 = (Uint8*) screen->pixels;
      ubuff8 += (y * screen->pitch) + (x*2);
      ubuff16 = (Uint16*) ubuff8;
      *ubuff16 = (Uint16) color; 
      break;  

    case 3:
      ubuff8 = (Uint8*) screen->pixels;
      ubuff8 += (y * screen->pitch) + (x*3);
      

      if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
	c1 = (color & 0xFF0000) >> 16;
	c2 = (color & 0x00FF00) >> 8;
	c3 = (color & 0x0000FF);
      } else {
	c3 = (color & 0xFF0000) >> 16;
	c2 = (color & 0x00FF00) >> 8;
	c1 = (color & 0x0000FF);	
      }

      ubuff8[0] = c3;
      ubuff8[1] = c2;
      ubuff8[2] = c1;
      break;
      
    case 4:
      ubuff8 = (Uint8*) screen->pixels;
      ubuff8 += (y*screen->pitch) + (x*4);
      ubuff32 = (Uint32*)ubuff8;
      *ubuff32 = color;
      break;
      
    default:
      fprintf(stderr, "Error: Unknown bitdepth!\n");
    }
  
  /* Unlock the screen if needed */
  if(SDL_MUSTLOCK(screen)) {
    SDL_UnlockSurface(screen);
  }
}


void DrawPicture(SDL_Surface* screen) {
  // Draw all the colors...
  int r = 255, g = 0, b = 255;
  int x, y;
  
  
  for(x = 0; x < screen->w; x++ ) {
    for( y = 0; y < screen->h; y++ ) {
      setpixel(screen, x, y, x, y, 0);
    }
  }
  
  SDL_Flip(screen);
}


int main(int argc, char* argv[])
{
  /* Declare Variables */
  SDL_Surface *screen;
  SDL_Event event;
  
  int exitkey = 0;
  
  
  /* Initialize SDL, exit if there is an error. */
  if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    fprintf(stderr, "Could not initialize SDL: %s\n", 
	    SDL_GetError());
    return -1;
  }
  
  /* When the program is through executing, call SDL_Quit */
  atexit(SDL_Quit);
  
  /* Grab a surface on the screen */
  screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE|SDL_ANYFORMAT);
  if( !screen ) {
    fprintf(stderr, "Couldn't create a surface: %s\n",
	    SDL_GetError());
    return -1;
  }
  
  /* Print Some info */
  printf("Created SDL Surface: %x\n", screen);
  printf("\t BPP \t\t : %d\n", screen->format->BytesPerPixel );
  printf("\t XRes \t\t :%d\n", screen->w );
  printf("\t YRes \t\t :%d\n", screen->h );
  
  
  while(!exitkey) {
    DrawPicture(screen);
    while(SDL_PollEvent(&event)) {
      switch( event.type ) 
	{
	case SDL_QUIT:
	  exitkey = 1;
	  printf("Got quit event!\n");
	  break;
	case SDL_KEYDOWN:
	  printf("Key hit - exiting!\n");
	  exitkey = 1;
	  break;
	}
    }
  }
  
  return 0;
}
 
Old 01-11-2004, 07:56 AM   #13
Pres
Member
 
Registered: Jun 2002
Location: Australia
Distribution: Slack 9.1
Posts: 232

Rep: Reputation: 30
Quote:
Originally posted by unholy
I didnt spot that, thanks. Now my erronous output is reduced to this..
Code:
[unholy@localhost example1]$ gcc -g -Wall `sdl-config --cflags` `sdl-config --libs`-o example1 ./example1.c
gcc: example1: No such file or directory
example1.c: In function `DrawPicture':
example1.c:86: warning: unused variable `r'
example1.c:86: warning: unused variable `g'
example1.c:86: warning: unused variable `b'
example1.c: In function `main':
example1.c:128: warning: unsigned int format, pointer arg (arg 2)
If you put a space between the `sdl-config --libs` and the -o example1, well that solves one problem. The rest are warnings, delete r, g and b definitions if they bother you, that's three more gone.

Don't see why there's a problem with "int main(int argc, char* argv[])" ... still, just a warning, and you got on -Wall, so, I wouldn't worry.
 
Old 01-11-2004, 08:09 AM   #14
unholy
Member
 
Registered: Sep 2003
Location: Eire
Distribution: Ubuntu 7.10
Posts: 344

Original Poster
Rep: Reputation: 30
Ah, ok I put in the space, but now I get a lot of errors..
Code:
[unholy@localhost example1]$ gcc -g -Wall `sdl-config --cflags` `sdl-config --libs` -o example1 example1.c
example1.c: In function `DrawPicture':
example1.c:86: warning: unused variable `r'
example1.c:86: warning: unused variable `g'
example1.c:86: warning: unused variable `b'
example1.c: In function `main':
example1.c:128: warning: unsigned int format, pointer arg (arg 2)
/home/unholy/tmp/ccieMzEh.o(.text+0x38): In function `setpixel':
/home/unholy/programming/example1/example1.c:23: undefined reference to `SDL_LockSurface'
/home/unholy/tmp/ccieMzEh.o(.text+0x6a):/home/unholy/programming/example1/example1.c:28: undefined reference to `SDL_MapRGB'
/home/unholy/tmp/ccieMzEh.o(.text+0x1f6):/home/unholy/programming/example1/example1.c:79: undefined reference to `SDL_UnlockSurface'
/home/unholy/tmp/ccieMzEh.o(.text+0x27f): In function `DrawPicture':
/home/unholy/programming/example1/example1.c:96: undefined reference to `SDL_Flip'
/home/unholy/tmp/ccieMzEh.o(.text+0x2a5): In function `main':
/home/unholy/programming/example1/example1.c:110: undefined reference to `SDL_Init'
/home/unholy/tmp/ccieMzEh.o(.text+0x2b7):/home/unholy/programming/example1/example1.c:111: undefined reference to `SDL_GetError'
/home/unholy/tmp/ccieMzEh.o(.text+0x2e2):/home/unholy/programming/example1/example1.c:117: undefined reference to `SDL_Quit'
/home/unholy/tmp/ccieMzEh.o(.text+0x300):/home/unholy/programming/example1/example1.c:120: undefined reference to `SDL_SetVideoMode'
/home/unholy/tmp/ccieMzEh.o(.text+0x317):/home/unholy/programming/example1/example1.c:122: undefined reference to `SDL_GetError'
/home/unholy/tmp/ccieMzEh.o(.text+0x3ba):/home/unholy/programming/example1/example1.c:136: undefined reference to `SDL_PollEvent'
collect2: ld returned 1 exit status
 
Old 01-11-2004, 08:31 AM   #15
Pres
Member
 
Registered: Jun 2002
Location: Australia
Distribution: Slack 9.1
Posts: 232

Rep: Reputation: 30
Hmm well this is just an example, try this, and worry later about compatibility :

gcc example1.c -o example -lSDL -lpthread

and see what comes out ... maybe the /usr/lib is a furphy and it's really in /usr/local/lib or some such. If the undefined references keep on then go looking for libSDL.a and see where it is, and include it explicitly, by path. That's what I'd do anyway.

Last edited by Pres; 01-11-2004 at 08:35 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cant find gcc ryedunn Mandriva 3 09-14-2005 12:32 AM
Trying to program an SDL application but cannot find the SDL.h file:SuSE 9.2&KDevelop pujolasdf Linux - Newbie 4 03-13-2005 07:50 AM
Mandrake cannot find my gcc cajunaggie Linux - Newbie 2 09-30-2004 07:32 AM
Still cant find and use gcc linuxlah Linux - Software 3 06-06-2002 12:14 AM
Cant find GCC manzoor Programming 3 10-12-2001 01:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration