New To Linux Trying To Compile


dpoarch

Member
Joined
Dec 26, 2004
Messages
196
Age
37
Location
california
Website
Visit site
So I have linux and I installed ooPo's toolchain. I can compile files with gp2x-gcc <filename.c> but I dont know how to include the sdl library so I cannot get the demo on the wiki to compile. I tried using -L to include the path where the libs are and -lSDL but I can't seem to get it to work. When I used just the regular gcc compiler and sdl and I can usually just type -lSDL to get it compile. Can someone fill in what im doing wrong?
 
I don't use that toolchain but you can use sdl-config to add the libraries and headers you need, just add this:

Code:
gcc <filename.c> `sdl-config --cflags --libs`

That's for the host PC though, you'll need to use the cross compilers sdl-config, maybe it's named gp2x-sdl-config, you'll need to check. And the back-quotes (`) that don't show up very well are important, they tell the shell to execute that comand and add the output as arguments.

If your toolchain needs to build static add -s --static:

Code:
gp2x-gcc <filename.c> `gp2x-sdl-config --cflags --libs` -s --static

You're probably better off using a Makefile, here is an example (for the GPH toolchain):

Code:
# =	 =	   =	   =	   =	   =	   =	   =	   =	   =
# Example SDL Makefile for Linux.
# =	 =	   =	   =	   =	   =	   =	   =	   =	   =

# Enter name of program and the extention you want for the GP2x here:
PROGRAM=sdldemo
EXT=gpe

# Enter a list of your source files here:
SOURCES=\
sdldemo.c

# If you want additional compiler options or additional libraries add
# them here:
EXTRA_CFLAGS=-W
EXTRA_LDFLAGS=

GP2X_EXTRA_CFLAGS=-O3 -ffast-math -fomit-frame-pointer -mcpu=arm920t
GP2X_EXTRA_LDFLAGS=

# =	 =	   =	   =	   =	   =	   =	   =	   =	   =
# Toolchain:
# =	 =	   =	   =	   =	   =	   =	   =	   =	   =

# Decide which toolchain to use:

ifdef gp2x
		CC=arm-gp2x-linux-gcc
		CFLAGS=-c -Wall $(EXTRA_CFLAGS) $(GP2X_EXTRA_CFLAGS) `arm-gp2x-linux-sdl-config --cflags`
		LDFLAGS=$(EXTRA_LDFLAGS) $(GP2X_EXTRA_LDFLAGS) `arm-gp2x-linux-sdl-config --libs`
		EXECUTABLE=$(PROGRAM).$(EXT)
else
		CC=gcc
		CFLAGS=-c -Wall $(EXTRA_CFLAGS) `sdl-config --cflags`
		LDFLAGS=$(EXTRAN_LDFLAGS) `sdl-config --libs`
		EXECUTABLE=$(PROGRAM)
endif


# =	 =	   =	   =	   =	   =	   =	   =	   =	   =
# Compile:
# =	 =	   =	   =	   =	   =	   =	   =	   =	   =

# make				  Build for host (PC).
#
# make clean			Clean directory ready for fresh build.
#
# make gp2x			 Build for GP2x (run make clean first).


all: pc

pc:
		make build

gp2x:
		make build gp2x=1


# =	 =	   =	   =	   =	   =	   =	   =	   =	   =
# Build project based on architecture:
# =	 =	   =	   =	   =	   =	   =	   =	   =	   =

OBJECTS=$(SOURCES:.c=.o)

.c.o:
		$(CC) $< $(CFLAGS) -o $@

$(EXECUTABLE): $(OBJECTS)
		$(CC) $(OBJECTS) $(LDFLAGS) -o $@

build: $(SOURCES) $(EXECUTABLE)

clean:
		rm -fr *.o
		rm -f $(PROGRAM) $(PROGRAM).$(EXT)

You'll probably just need to change the executable filenames to get the above to work on Linux. Hope this helps!
 
ok thank you for the response I am trying to just do it on the terminal though before I make a make file. I have the sdl-config file in my gp2xdev folder but my computer defults to my normal lib files for the pc. I tried to change the directory to the gp2x one but it doesnt work. I will keep messing around but that was a big help.
 
dpoarch posted on Feb 2 2007 at 08:36 PM said:
So I have linux and I installed ooPo's toolchain. I can compile files with gp2x-gcc <filename.c> but I dont know how to include the sdl library so I cannot get the demo on the wiki to compile. I tried using -L to include the path where the libs are and -lSDL but I can't seem to get it to work. When I used just the regular gcc compiler and sdl and I can usually just type -lSDL to get it compile. Can someone fill in what im doing wrong?

Few more make examples in this thread:
http://www.gp32x.de/board/index.php?s=&am...st&p=345770

May be a simple example game here: http://gp2x.fullsack.com/cocoon/gp2x/index.xml#download
inside each is a file named source.zip, both built with warnings as errors fine with ooPo's toolcain. Hope that helps, I feel your pain, can be very frustrating at first.

dpoarch posted on Feb 3 2007 at 03:21 AM said:
ok thank you for the response I am trying to just do it on the terminal though before I make a make file. I have the sdl-config file in my gp2xdev folder but my computer defults to my normal lib files for the pc. I tried to change the directory to the gp2x one but it doesnt work. I will keep messing around but that was a big help.

You probably won't get very far without make, it is your friend and can save you massive amounts of work and grief.
 
Last edited by a moderator:
I think the reason why nothing is working is because when ever I use sdl-config it defaults to the one in usr/bin not the one in usr/local/gp2xdev/bin. I dont know if I should change the directories inside the sdl-config file inside usr/bin or what cause I dont know how to make it pick the other one.

edit: changed sdl-config to gp2x-sdl-config and got the demo file to compile but when I try to run it it just freezes.
 
dpoarch posted on Feb 3 2007 at 10:27 PM said:
I think the reason why nothing is working is because when ever I use sdl-config it defaults to the one in usr/bin not the one in usr/local/gp2xdev/bin. I dont know if I should change the directories inside the sdl-config file inside usr/bin or what cause I dont know how to make it pick the other one.

edit: changed sdl-config to gp2x-sdl-config and got the demo file to compile but when I try to run it it just freezes.

Try to setup a telnet session as described in the wiki. Then you can try writing to stdout and track progress of execution.

From my limited experience, it seems that the gp2xmenu program has to be executed after your own software finishes or else it will appear to freeze. So if you got an error before anything was drawn and you didn't execute gp2xmenu on exit you'll see a frozen black screen. Another reason why setting up telnet is cool is because you can run the gp2xmenu through the telnet session to recover if necessary.


Using the gp2x-sdl-config is the right choice, by the way.

Hope that helps some.
 
Last edited by a moderator:
I looked at the telnet instructions but those are for windows. Is the last paragraph for linux? I cannot tell if they are talking about the gp2x linux or pc linux. and I do not really know what that means
 
Last edited by a moderator:
dpoarch posted on Feb 4 2007 at 06:27 AM said:
I think the reason why nothing is working is because when ever I use sdl-config it defaults to the one in usr/bin not the one in usr/local/gp2xdev/bin. I dont know if I should change the directories inside the sdl-config file inside usr/bin or what cause I dont know how to make it pick the other one.

edit: changed sdl-config to gp2x-sdl-config and got the demo file to compile but when I try to run it it just freezes.

Does your toolchain use the GP2x's existing default libraries? Or does it need it's own ones?

If it needs it's own ones, then you need to add -s --static to the end of your gcc arguments to build a static executable. Otherwise when you try to run it it'll bomb out due to missing libraries.

Try both of the following methods to generate a GP2x executable:

Code:
gp2x-gcc sdldemo.c `gp2x-sdl-config --cflags --libs` -s --static
This one will (try to) build a staticly linked executable, if it succeeds then that's probably what you want.

Code:
gp2x-gcc sdldemo.c `gp2x-sdl-config --cflags --libs`
This one will (try to) build a dynamicly linked executable and will require the same set of libraries on the GP2x as on the cross compiler on your PC. The (old?) GPH toolchain needs this for instance.

(Tweak the above to get it to compile for your toolchain - I'm guessing as I don't use it!)

Here is my first GP2x SDL C test program, it should draw a colour cube and then after a button press return to the GP2x menu.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <SDL.h>

#define GP2X_PRODUCTION_BUILD

#define LOCK(s)   {if(SDL_MUSTLOCK(s))(void)SDL_LockSurface(s);}
#define UNLOCK(s) {if(SDL_MUSTLOCK(s))(void)SDL_UnlockSurface(s);}

void DrawScene(SDL_Surface *scene);
void PutPixel(SDL_Surface *surface, Uint16 x, Uint16 y, Uint8 r, Uint8 g
 , Uint8 b);


/* SDL Demo program:
 */
int main()
{
		SDL_Surface *screen_ptr = NULL;
		SDL_Joystick *joystick_ptr = NULL;
		SDL_Event event;

		Uint32 col;

		int done = 0;

		/* Initialize SDL:
		 */
		if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
				fprintf(stderr, "Couldn't initialize SDL: %s\n"
				 , SDL_GetError());
				return 1;
		}

		if ( (screen_ptr=SDL_SetVideoMode(320, 240, 8, SDL_SWSURFACE) )
		 == NULL) {
				fprintf(stderr, "Couldn't set video mode: %s\n"
				 , SDL_GetError());
				return 1;
		}

		if ( (joystick_ptr=SDL_JoystickOpen(0) ) == NULL) {
				fprintf(stderr, "Couldn't open the joystick: %s\n"
				 , SDL_GetError());
				return 1;
		}

		/* Hide cursor:
		 */
		SDL_ShowCursor(SDL_DISABLE);

		/* Initialize screen:
		 */
		col = SDL_MapRGB(screen_ptr->format, 0xff, 0x00, 0xff);
		SDL_FillRect(screen_ptr, NULL, col);
		SDL_UpdateRect(screen_ptr, 0,0,0,0);

		/* Draw nice scene:
		 */
		DrawScene(screen_ptr);
		SDL_UpdateRect(screen_ptr, 0,0,0,0);

		/* Wait for keypress or term:
		 */
		while (!done)
		{
				SDL_WaitEvent(&event);
				switch (event.type)
				{
						case SDL_JOYBUTTONDOWN:
						case SDL_QUIT:
								done=1;
								break;
				}

				/* Be nice:
				 */
				SDL_Delay(10);
		}

		/* Close SDL:
		 */
		SDL_Quit();

		/* Return to GP2x menu:
		 */
#ifdef GP2X_PRODUCTION_BUILD
		chdir("/usr/gp2x");
		execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);
#endif

		/* Otherwise quit for PC:
		 */
		return 0;
}

/* Fancy background:
 */
void DrawScene(SDL_Surface *scene)
{
		int x,y;

		LOCK(scene);
		for(x=0; x< scene->w; x++)
				for(y=0; y< scene->h; y++)
						PutPixel(scene, x, y,
								(( (255.0 / scene->w) * (x + 0.0) ) / 16) * 16,
								(( (255.0 / scene->h) * (y + 0.0) ) / 16) * 16,
								0
								);
		UNLOCK(scene);
}

/* The mandatory putpixel routine from www.libsdl.org:
 */
void PutPixel(SDL_Surface *surface, Uint16 x, Uint16 y, Uint8 r, Uint8 g
			  , Uint8 b){

		Uint32 color = SDL_MapRGB(surface->format, r, g, b);

		switch (surface->format->BytesPerPixel){

		case 1: /* 8-bpp */
		{
		Uint8 *bufp;
		bufp = (Uint8 *)surface->pixels + y*surface->pitch + x;
		*bufp = color;
		}
		break;

	case 2: // Probably 15-bpp or 16-bpp
	  {
		Uint16 *bufp;
		bufp = (Uint16 *)surface->pixels + y*surface->pitch/2 + x;
		*bufp = color;
	  }
	  break;
	case 3: // Slow 24-bpp mode, usually not used
	  {
		Uint8 *bufp;
		bufp = (Uint8 *)surface->pixels + y*surface->pitch + x * 3;
		if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
		{
		  bufp[0] = color;
		  bufp[1] = color >> 8;
		  bufp[2] = color >> 16;
		} else {
		  bufp[2] = color;
		  bufp[1] = color >> 8;
		  bufp[0] = color >> 16;
		}
	  }
	  break;
	case 4: // Probably 32-bpp
	  {
		Uint32 *bufp;
		bufp = (Uint32 *)surface->pixels + y*surface->pitch/4 + x;
		*bufp = color;
	  }
	  break;
  }
}

It IS all sadly a bit painful to get started on the GP2x, but if you keep at it you'll crack it in the end and then have tons of fun! :)

** Edit: Helps if I copy&paste the WHOLE program, not just main() :rolleyes:
 
Last edited by a moderator:
dpoarch posted on Feb 4 2007 at 10:45 PM said:
I looked at the telnet instructions but those are for windows. Is the last paragraph for linux? I cannot tell if they are talking about the gp2x linux or pc linux. and I do not really know what that means

Make sure the GP2x is configured:

System->Menu Extension = ON
System->USB HOST = ON (or ALWAYS ON)
System->USB Network = ON (or ALWAYS ON)
IP = 10.1.0.1
FTP, Telnet Server = ON (or ALWAYS ON)

Exit from the system menu and restart the GP2x (just to be sure).

Startup your GP2x again and once booted up, plug the (PC connected) USB cable into the GP2x. Then as root on the PC type:

Code:
ifconfig -a

You should see an entry for usb0 (or something simular) if you're lucky.

If you do, then congratulations, you have a network connection! You can then configure it (as root) with:

Code:
ifconfig usb0 10.1.0.2 up

then
Code:
telnet 10.1.0.1
from the PC should connect to the GP2x.

If you don't get a network device entry listed with ifconfg, then type
Code:
dmesg | tail
and that should show you what's going on. Then read the wiki article very carefully and all others associated with the USB cable etc. It could be that you just need to replace the kernel module on the GP2x (detailed in the wiki page), and I'll assume you have a working USB cable!

Good luck and have fun!
 
Last edited by a moderator:
Fru.T Bunn i used your sdl demo and it compiled under both static and dynamic but only ran when compiled statically. Both of them go to a purple screen but the dynamic compiled one freezes there. Thank you for your help. Finally getting somewhere. I still am going to set up the telnet I couldn't get it to work earlier.
 
The programs probably aren't freezing, they are just exiting for some reason.

If you don't run the menu after the program exits, the screen will be left in whatever state it was on exit.

If you want to find out what is happening, run it from telnet.
 
Back
Top