ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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.
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.