C++ not working well

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

Moderators: cheriff, TyRaNiD

Post Reply
webjeff
Posts: 66
Joined: Thu May 05, 2005 2:51 am

C++ not working well

Post by webjeff »

OK,

After many tests, It seems C++ is not being initialized properly or something is seriously wrong. Doing the exact same stuff in C works perfect, but in C++ has many errors and crashes on the PSP. Can someone please help and maybe we can get C++ working well on this.

Has anyone really tested C++ working on this platform, not just C?

Thanks
Jeff.
webjeff
Posts: 66
Joined: Thu May 05, 2005 2:51 am

Post by webjeff »

Here's the code I used:

Code: Select all

/*
 * PSP Software Development Kit - http://www.pspdev.org
 * -----------------------------------------------------------------------
 * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
 *
 * Copyright (c) 2005 Jesper Svennevid
 */

#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include <pspgu.h>


PSP_MODULE_INFO&#40;"Lights Sample", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;

#define printf	pspDebugScreenPrintf

int SetupCallbacks&#40;&#41;;

static unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; list&#91;262144&#93;;

#define BUF_WIDTH &#40;512&#41;
#define SCR_WIDTH &#40;480&#41;
#define SCR_HEIGHT &#40;272&#41;
#define PIXEL_SIZE &#40;4&#41; /* change this if you change to another screenmode */
#define FRAME_SIZE &#40;BUF_WIDTH * SCR_HEIGHT * PIXEL_SIZE&#41;
#define ZBUF_SIZE &#40;BUF_WIDTH SCR_HEIGHT * 2&#41; /* zbuffer seems to be 16-bit? */

volatile int g_exit=0;

class TestLogger
&#123;
public&#58;
	TestLogger&#40;&#41; &#123; &#125;;
	~TestLogger&#40;&#41; &#123; &#125;;
	void Log&#40;&#41; &#123;FILE *file,*file2;
 			file = fopen&#40;"ms0&#58;/PSP/GAME/logtest.txt", "wb"&#41;;
			file2 = fopen&#40;"ms0&#58;/PSP/GAME/logtest2.txt", "wb"&#41;;
			fwrite&#40;"test data\n", 1, 10, file&#41;;
			fwrite&#40;"test data\n", 1, 10, file2&#41;;
			fclose&#40;file&#41;;
			fclose&#40;file2&#41;;
		 &#125;;
&#125;;

int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;

FILE *file;
FILE *file2;

	SetupCallbacks&#40;&#41;;

	sceKernelDcacheWritebackAll&#40;&#41;;
	pspDebugScreenInit&#40;&#41;;
	//sceGuStart&#40;GU_DIRECT,list&#41;;
	//sceGuInit&#40;&#41;;

	pspDebugScreenPrintf&#40;"init logger\n"&#41;;
	//logger.Log&#40;&#41;;
	//g_logger.Init&#40;"ms0&#58;/PSP/GAME/log.txt"&#41;;
	//pspDebugScreenPrintf&#40;"init content\n"&#41;;

	//g_ContentManager.Init&#40;"ms0&#58;/PSP/GAME"&#41;;

	//g_logger.Log&#40;Logger&#58;&#58;LL_INFO,"TEST","TEST "&#41;;
	//g_logger.SetLog&#40; NULL&#41;;
	pspDebugScreenPrintf&#40;"write test file\n"&#41;;
	file = fopen&#40;"ms0&#58;/PSP/GAME/logtest.txt", "wb"&#41;;
	file2 = fopen&#40;"ms0&#58;/PSP/GAME/logtest2.txt", "wb"&#41;;
	fwrite&#40;"test data\n", 1, 10, file&#41;;
	fwrite&#40;"test data\n", 1, 10, file2&#41;;
	fclose&#40;file&#41;;
	fclose&#40;file2&#41;;
	while &#40;g_exit==0&#41;
	&#123;
		/*sceGuStart&#40;GU_DIRECT,list&#41;;

		sceGuClearColor&#40;0x554433&#41;;
		sceGuClearDepth&#40;0&#41;;
		sceGuClear&#40;GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT&#41;;


		sceGuFinish&#40;&#41;;
		sceGuSync&#40;0,0&#41;;

		sceDisplayWaitVblankStart&#40;&#41;;
		sceGuSwapBuffers&#40;&#41;;*/
	&#125;

	//sceGuTerm&#40;&#41;;

	sceKernelExitGame&#40;&#41;;
	return 0;
&#125;

/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
	//sceKernelExitGame&#40;&#41;;
	g_exit=1;
	return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
	int cbid;

	cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
	sceKernelRegisterExitCallback&#40;cbid&#41;;

	sceKernelSleepThreadCB&#40;&#41;;

	return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41;
&#123;
	int thid = 0;

	thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
	if&#40;thid >= 0&#41;
	&#123;
		sceKernelStartThread&#40;thid, 0, 0&#41;;
	&#125;

	return thid;
&#125;

When we call the functions for reading the file directly in C it works fine. As soon as we start to make a class and compile using the stdc++ lib we crash. We also are seeing some weird stuff with C++ in general. I was hoping someone can test or get working a C++ example, I think it would help out our community very nicely, not just me :)

Thanks again guys,
Jeff.
User avatar
Agoln
Posts: 326
Joined: Wed Jun 08, 2005 3:14 am
Location: Fort Wayne, IN

Post by Agoln »

Ever try adding 'extern C' to C++ code?
Lego of my Ago!
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

Agoln wrote:Ever try adding 'extern C' to C++ code?
Er, what?
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

webjeff wrote:When we call the functions for reading the file directly in C it works fine. As soon as we start to make a class and compile using the stdc++ lib we crash. We also are seeing some weird stuff with C++ in general.
Where does it crash? What type of exception is it? What function does it crash in (psp-objdump -d or psp-addr2line can be of great help here)? Can you provide a slimmed-down piece of code that causes the crash (the code you posted doesn't look complete enough for someone else to repro it)?

No one has reported any strangeness using C++, but it's possible there's something going on in the tools. We need a better idea of what's happening though :).
User avatar
Thanhda
Posts: 331
Joined: Sat Apr 09, 2005 2:08 am
Location: Canada
Contact:

Post by Thanhda »

I've had many problems with C++ in the past.

First Step, make sure you have the lastest toolchain.
Second Step, modify the Make file. Add these lines

USE_PSPSDK_LIBC = 1
CFLAGS = -O2 -G0 -Wall

Should work fine.

Also, if you still have problems add

extern "C" {
//Code
};

here example of Makefile

Code: Select all

TARGET = copy
OBJS = copy.o

INCDIR =
CFLAGS = -02 -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

USE_PSPSDK_LIBC = 1

LIBDIR =
LDFLAGS =
LIBS= -lpspgum -lpspgu -lm

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = CopyImage Sample

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak
To test this out, try running the sdk/gu/copy/ app. modify the Makefile. and add a basic class in the copy.cpp file.

Code: Select all

#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>

#include <pspgu.h>

#ifndef __cplusplus
extern "C"
&#123;
#endif

PSP_MODULE_INFO&#40;"CopyImage Sample", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;

#define printf  pspDebugScreenPrintf

class test
&#123;
        public&#58;
        void hey&#40;&#41;;
&#125;;

void test&#58;&#58;hey&#40;&#41; &#123; &#125;
....
....
....
int main&#40;int argc, char**argv&#41;
&#123;
    test *t=new test;
    t->hey&#40;&#41;;
    ...
&#125;
...
....
#ifndef __cplusplus
&#125;; //end extern "C"
#endif
heres a example of code that is writting in C++ for the PSP, compiles fine, and works fine on the psp

http://tmpstore.free.fr/FileAssistant/v ... c.php?t=21

edit:

I've also tried this with the Light project, works perfectly fine. let me know if you have any problems.
There are 10 types of people in the world: Those who understand binary, and those who don't...
Krevnik
Posts: 71
Joined: Wed Mar 09, 2005 12:07 pm

Post by Krevnik »

You could probably just wrap the main function in the extern C, IIRC as well.
Post Reply