Objective-C on the MinPSPW SDK

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Heimdall
Posts: 245
Joined: Thu Nov 10, 2005 1:29 am
Location: Netherlands
Contact:

Objective-C on the MinPSPW SDK

Post by Heimdall »

As of build 0.8.8 there is a new compiler included and a sample obj-c project.

https://sourceforge.net/project/showfil ... _id=628720

The example code:

Code: Select all

#include <objc/Object.h>

@interface Greeter&#58;Object
&#123;
 /* This is left empty on purpose&#58;
  ** Normally instance variables would be declared here,
  ** but these are not used in our example.
  */
&#125;

- &#40;void&#41;greet;

@end


#include <pspkernel.h>
#include <pspdebug.h>
#include <pspctrl.h>

/* Define printf, just to make typing easier */
#define printf  pspDebugScreenPrintf

@implementation Greeter

- &#40;void&#41;greet
&#123;
	printf&#40;"Hello, World from Obj-C!\n"&#41;;
&#125;

@end

/* Define the module info section */
PSP_MODULE_INFO&#40;"template", 0, 1, 1&#41;;

/* Define the main thread's attribute value &#40;optional&#41; */
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER | THREAD_ATTR_VFPU&#41;;

int main&#40;void&#41;
&#123;
	id myGreeter;
	myGreeter=&#91;Greeter new&#93;;

	&#91;myGreeter greet&#93;;

	&#91;myGreeter free&#93;;

	/* Non Objective-C code to allow the app to end */
	SceCtrlData pad;
	
	printf&#40;"\nPress X to quit.\n"&#41;;
	for &#40;;;&#41;
	&#123;
		sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
		if &#40;pad.Buttons & PSP_CTRL_CROSS&#41;
			break;
		sceDisplayWaitVblankStart&#40;&#41;;
	&#125;
	sceKernelExitGame&#40;&#41;;
	
	return 0;
&#125;
So for the iPhone fans there is no excuse to drop it and make cool apps for the PSP :)
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Cool! Maybe the toolchain script can be altered to compile obj-c as well.
Heimdall
Posts: 245
Joined: Thu Nov 10, 2005 1:29 am
Location: Netherlands
Contact:

Post by Heimdall »

I never built the official script but i think the patch is easy, on the download part, it is necessary to download the gcc objc component and on the build script enable it by:

Code: Select all

--enable-languages="c,c++,objc"
This builds but it won't link, I had to change my script with something like:

CFLAGS="-G0" \
../../gcc-4.3.2/configure \
--enable-languages="c,c++,objc" \
... and all the other flags...

Then I added the sample code to the SDK:

Code: Select all

Index&#58; pspsdk/src/base/build.mak
===================================================================
--- pspsdk/src/base/build.mak	&#40;revision 2432&#41;
+++ pspsdk/src/base/build.mak	&#40;working copy&#41;
@@ -45,6 +45,11 @@
 CFLAGS += -D_PSP_FW_VERSION=$&#40;PSP_FW_VERSION&#41;
 CXXFLAGS += -D_PSP_FW_VERSION=$&#40;PSP_FW_VERSION&#41;
 
+# Objective-C selection. All Objective C code must be linked against libobjc.a
+ifeq &#40;$&#40;USE_OBJC&#41;,1&#41;
+LIBS     &#58;= $&#40;LIBS&#41; -lobjc
+endif
+
 ifeq &#40;$&#40;BUILD_PRX&#41;,1&#41;
 LDFLAGS  &#58;= $&#40;addprefix -L,$&#40;LIBDIR&#41;&#41; -specs=$&#40;PSPSDK&#41;/lib/prxspecs -Wl,-q,-T$&#40;PSPSDK&#41;/lib/linkfile.prx $&#40;LDFLAGS&#41;
 EXTRA_CLEAN += $&#40;TARGET&#41;.elf
@@ -198,6 +203,9 @@
 %.c&#58; %.exp
 	psp-build-exports -b $< > $@
 
+%.o&#58; %.m
+	$&#40;CC&#41; $&#40;CFLAGS&#41; -c -o $@ $<
+
 clean&#58; 
 	-rm -f $&#40;FINAL_TARGET&#41; $&#40;EXTRA_CLEAN&#41; $&#40;OBJS&#41; $&#40;PSP_EBOOT_SFO&#41; $&#40;PSP_EBOOT&#41; $&#40;EXTRA_TARGETS&#41;
 
Index&#58; pspsdk/src/base/build_prx.mak
===================================================================
--- pspsdk/src/base/build_prx.mak	&#40;revision 2432&#41;
+++ pspsdk/src/base/build_prx.mak	&#40;working copy&#41;
@@ -37,6 +37,11 @@
 
 CFLAGS += -D_PSP_FW_VERSION=$&#40;PSP_FW_VERSION&#41;
 
+# Objective-C selection. All Objective C code must be linked against libobjc.a
+ifeq &#40;$&#40;USE_OBJC&#41;,1&#41;
+LIBS     &#58;= $&#40;LIBS&#41; -lobjc
+endif
+
 # Library selection.  By default we link with Newlib's libc.  Allow the
 # user to link with PSPSDK's libc if USE_PSPSDK_LIBC is set to 1.
 
@@ -80,6 +85,9 @@
 %.c&#58; %.exp
 	psp-build-exports -b $< > $@
 
+%.o&#58; %.m
+	$&#40;CC&#41; $&#40;CFLAGS&#41; -c -o $@ $<
+
 clean&#58; $&#40;EXTRA_CLEAN&#41;
 	-rm -f $&#40;FINAL_TARGET&#41; $&#40;TARGET&#41;.elf $&#40;OBJS&#41;
 
Index&#58; pspsdk/src/samples/Makefile.am
===================================================================
--- pspsdk/src/samples/Makefile.am	&#40;revision 2432&#41;
+++ pspsdk/src/samples/Makefile.am	&#40;working copy&#41;
@@ -81,7 +81,8 @@
 	utility/systemparam \
 	utility/osk \
 	me/basic \
-	wlan
+	wlan \
+	obj-c
 
 all&#58;
 
Index&#58; pspsdk/src/samples/obj-c/main.m
===================================================================
--- pspsdk/src/samples/obj-c/main.m	&#40;revision 0&#41;
+++ pspsdk/src/samples/obj-c/main.m	&#40;revision 0&#41;
@@ -0,0 +1,61 @@
+#include <objc/Object.h>
+
+@interface Greeter&#58;Object
+&#123;
+ /* This is left empty on purpose&#58;
+  ** Normally instance variables would be declared here,
+  ** but these are not used in our example.
+  */
+&#125;
+
+- &#40;void&#41;greet;
+
+@end
+
+
+#include <pspkernel.h>
+#include <pspdebug.h>
+#include <pspctrl.h>
+
+/* Define printf, just to make typing easier */
+#define printf  pspDebugScreenPrintf
+
+@implementation Greeter
+
+- &#40;void&#41;greet
+&#123;
+	printf&#40;"Hello, World from Obj-C!\n"&#41;;
+&#125;
+
+@end
+
+/* Define the module info section */
+PSP_MODULE_INFO&#40;"template", 0, 1, 1&#41;;
+
+/* Define the main thread's attribute value &#40;optional&#41; */
+PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER | THREAD_ATTR_VFPU&#41;;
+
+int main&#40;void&#41;
+&#123;
+	id myGreeter;
+	myGreeter=&#91;Greeter new&#93;;
+
+	&#91;myGreeter greet&#93;;
+
+	&#91;myGreeter free&#93;;
+
+	/* Non Objective-C code to allow the app to end */
+	SceCtrlData pad;
+	
+	printf&#40;"\nPress X to quit.\n"&#41;;
+	for &#40;;;&#41;
+	&#123;
+		sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
+		if &#40;pad.Buttons & PSP_CTRL_CROSS&#41;
+			break;
+		sceDisplayWaitVblankStart&#40;&#41;;
+	&#125;
+	sceKernelExitGame&#40;&#41;;
+	
+	return 0;
+&#125;
Index&#58; pspsdk/src/samples/obj-c/Makefile.sample
===================================================================
--- pspsdk/src/samples/obj-c/Makefile.sample	&#40;revision 0&#41;
+++ pspsdk/src/samples/obj-c/Makefile.sample	&#40;revision 0&#41;
@@ -0,0 +1,19 @@
+TARGET = ObjC
+OBJS = main.o
+
+USE_OBJC=1
+
+INCDIR =
+CFLAGS = -G0 -Wall -O2
+CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
+ASFLAGS = $&#40;CFLAGS&#41;
+
+LIBDIR =
+LDFLAGS =
+
+EXTRA_TARGETS = EBOOT.PBP
+PSP_EBOOT_TITLE = ObjC Sample
+
+PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
+include $&#40;PSPSDK&#41;/lib/build.mak
+

Heimdall
Posts: 245
Joined: Thu Nov 10, 2005 1:29 am
Location: Netherlands
Contact:

Post by Heimdall »

I'm working on a patch to enable also Objective-C++. The compiler seems to work, i'm running tests and will generate a binary build in the coming days.
Post Reply