Sign in to follow this  
3Dgonewild

[SDL] in linux

Recommended Posts

Hi , i have a quick question to ask. I've made a simple sdl example in windows. How can i compile the same sdl test program in linux using the command line(i've installed ubuntu)? And what do i have to change in my code ? Thanks.

Share this post


Link to post
Share on other sites
If you've been careful you should need no changes.

IIRC, to compile a single SDL source file ( provided you have the SDL-devel libraries )

g++ -o myProgram source.cpp `sdl-config --cflags --libs`

(note the `charaters are backticks)

If you get errors then post them, and the source.

Share this post


Link to post
Share on other sites
It'd help to know which language and compiler/interpreter you're using.

Assuming C, it'd be something like:
gcc source_file_1.c source_file_2.c `sdl-config --cflags --libs` -o program_name

This is only useful for very simple programs though. Note that the ` ` are backticks, not single quotes.

On Ubuntu and other Debian-based distros, you'll need to have the GCC compiler and the libsdl1.2-dev packages installed.

Hope this helps.

Share this post


Link to post
Share on other sites
Just because i have zero experience with linux , can you tell me how to compile & run a project that has the following files/libs included ?

IDE : DEV CPP


Main.cpp
sClasses_.cpp
sClasses_.h
sLoad.cpp
sLoad.h

Linker params:
-lmingw32
-lSDLmain
-lSDL
-lSDL_image
-lSDL_mixer
-lSDL_ttf

And what about the .dll files? where do i put them ?...

Thanks for your help so far..

edit:

This will work ? :

g++ -o TEST Main.cpp sClasses_.cpp sClasses_.h sLoad.cpp sLoad.h `sdl-config --cflags --libs -lmingw32 -lSDLmain -lSDL -lSDL_image -lSDL_mixer -lSDL_ttf`

Share this post


Link to post
Share on other sites

  • The SDLmain library is needed only on Windows;

  • the mingw32 library is needed only with the MinGW32 compiler, which is a port of GCC to Windows, so it's only needed on Windows as well;

  • the -l flag is a flag for g++ (it's in fact a linker option, telling it which libraries should be linked), not for the sdl-config script, so it must be outside the backticks;

  • you don't need to specify -lSDL because sdl-config --libs does it for you;

  • you don't put DLL files anywhere. DLL files are Windows things. You need the "Linux" version of SDL, not the Windows one. With Ubuntu, there's probably some kind of graphical tool to install what you need. Otherwise, you can try:
    sudo apt-get install libsdl1.2-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev. You'll have to type your password.


Now, to build your program from the CLI, you can do:


g++ -o TEST Main.cpp sClasses_.cpp sLoad.cpp `sdl-config --cflags --libs` -lSDL_mixer -lSDL_image -lSDL_ttf


For this to work, the libSDL_image.so, libSDL_mixer.so and libSDL_ttf.so shared objects must be in a directory visible to the linker (eg /usr/lib). I'm pretty sure it's in /usr/lib on Ubuntu though, so that shouldn't cause any problem.

You'll eventually want to learn about make (or another build tool), because rebuilding everything like this is going to be extremely annoying after a moment, especially with larger projects.

Hope this helps.

EDIT: fixed typo

[Edited by - let_bound on March 6, 2007 6:40:07 AM]

Share this post


Link to post
Share on other sites
It might be worth it to look into Makefiles as well, as they can be used across multiple platforms (provided some adjustments are made) and should simplify your build process significantly.

Share this post


Link to post
Share on other sites
We recently transitioned from hand-written makefiles for linux and hand-generated projects for Windows to using CMake. It is nice because it is cross platform, and builds the project files for whatever the native tools are. In linux, it'll generate makefiles. In Windows, we use it to generate VC++2005 projects. If I ever manage to get my hands on a Mac, it'll generate XCode projects too. My hand-written makefiles were terrible, so it's nice to have decent ones generated automatically. That might be more than you are looking to do now, but we have been happy with CMake.

Share this post


Link to post
Share on other sites
Im getting this error in MAIN.cpp:

Main.cpp:5:27: error: SDL/SDL_Mixer.h: No such file or directory

Can you help please?!

[edited]
And as you can see:

Reading package lists... Done
Building dependency tree
Reading state information... Done
libsdl-mixer1.2-dev is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 17 not upgraded.




i have the latest dev version of sdl_mixer
[edited again]
And how do i run my program?!

Share this post


Link to post
Share on other sites
When i include #include "SDL.h" only , im getting this error:

Main.cpp:24: error: expected constructor, destructor, or type conversion before ‘*’ token
Main.cpp:32: error: expected constructor, destructor, or type conversion before ‘*’ token

more errors..



The code of line 24:

Mix_Chunk *BALL_JMP = NULL;



The code of line 32

TTF_Font *font = NULL;

Share this post


Link to post
Share on other sites
The most portable way of #including SDL headers is like this (source):

#include "SDL_headername.h", and not "SDL/SDL_header.h".



Then on linux use `sdl-config --cflags` to get the SDL include directories.

On windows I pass the flag -I"C:\path\to\include\SDL" to gcc so the SDL subdirectory is searchable when including files.

Share this post


Link to post
Share on other sites
I compiled the program , but when i double click on "test.o" the screen turns black for a second , and it drops me to the desktop....why is this ?!

The error:


username:~/Desktop/test ./TEST
open /dev/sequencer: No such file or directory
Err Mix_LoadWAV_RW with NULL src
Segmentation fault (core dumped)






[edited]

I did this:

sudo modprobe snd-seq-midi





And now it shows this:

Err Mix_LoadWAV_RW with NULL src
Segmentation fault (core dumped)




[EDITED.AGAIN]

I replaced
this line:

Mix_LoadWAV( "Data\\snd\\0.wav" );


with this one:

Mix_LoadWAV( "Data//snd//0.wav" );


(Plus , i replaced ALL "\\" characters with "//")

And now , im getting this only:

Segmentation fault (core dumped)



And here's how the sound init is done:
...
if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) )




[Edited by - 3Dgonewild on March 11, 2007 9:52:16 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by 3Dgonewild
I compiled the program , but when i double click on "test.o" the screen turns black for a second , and it drops me to the desktop....why is this ?!

The error:

*** Source Snippet Removed ***

[edited]

I did this:
*** Source Snippet Removed ***

And now it shows this:
*** Source Snippet Removed ***

[EDITED.AGAIN]

I replaced
this line:
*** Source Snippet Removed ***
with this one:
*** Source Snippet Removed ***
(Plus , i replaced ALL "\\" characters with "//")

And now , im getting this only:
*** Source Snippet Removed ***

And here's how the sound init is done:
*** Source Snippet Removed ***
Just jumping in here without having read the whole thread, so this may not be relevant, but...

Are you checking the return values of these functions, and using Mix_GetError() where appropriate to log information about any errors that might be occurring?

Share this post


Link to post
Share on other sites
I don't think it's the problem, but for one thing, (forward-) slashes shouldn't be escaped. Are you sure that Data/snd/0.wav (relative to the current working directory) is a valid WAV file? That is, are you sure that the path is valid and that the file is of the correct format? Remember that the filesystem is case sensitive.

Also, do what jyk said: check the return values of your function calls.

Share this post


Link to post
Share on other sites
Quote:
Original post by 3Dgonewild
Thanks for the help , but still the only error that im getting is the same...
Did you check the return values and SDL_mixer error state as suggested?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
replace // with /, it's the correct way in UNIX systems.

But I guess your problem is another. Try this. Run "gdb name_of_program", then in the prompt you get input "run". After it crashes, enter "bt" to get a backtrace.

Share this post


Link to post
Share on other sites
@jyk : yes , i did it...

@anonymous poster:

Nice...i found out that TTF(i got the font file from windows) causes the problem:


(gdb) run
Starting program: /home/***/Desktop/src/TEST
[Thread debugging using libthread_db enabled]
[New Thread -1217272144 (LWP 4802)]
[New Thread -1217791072 (LWP 4807)]
[New Thread -1228588128 (LWP 4808)]
open /dev/sequencer: No such file or directory

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1217272144 (LWP 4802)]
0xb7e70766 in TTF_SizeUNICODE () from /usr/lib/libSDL_ttf-2.0.so.0
(gdb) bt
#0 0xb7e70766 in TTF_SizeUNICODE () from /usr/lib/libSDL_ttf-2.0.so.0
#1 0xb7e7099b in TTF_RenderUNICODE_Solid () from /usr/lib/libSDL_ttf-2.0.so.0
#2 0xb7e70dc4 in TTF_RenderText_Solid () from /usr/lib/libSDL_ttf-2.0.so.0
#3 0x080496d4 in msg ()
#4 0x08049b1d in main ()




By the way , always i have to type this:

sudo modprobe snd-seq-midi




in order to fix this error:


open /dev/sequencer: No such file or directory




.....Why?!!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this