forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C++ not working well

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
webjeff



Joined: 05 May 2005
Posts: 66

PostPosted: Fri Aug 26, 2005 8:24 am    Post subject: C++ not working well Reply with quote

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.
Back to top
View user's profile Send private message
webjeff



Joined: 05 May 2005
Posts: 66

PostPosted: Fri Aug 26, 2005 8:47 am    Post subject: Reply with quote

Here's the code I used:

Code:

/*
 * 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("Lights Sample", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);

#define printf   pspDebugScreenPrintf

int SetupCallbacks();

static unsigned int __attribute__((aligned(16))) list[262144];

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

volatile int g_exit=0;

class TestLogger
{
public:
   TestLogger() { };
   ~TestLogger() { };
   void Log() {FILE *file,*file2;
          file = fopen("ms0:/PSP/GAME/logtest.txt", "wb");
         file2 = fopen("ms0:/PSP/GAME/logtest2.txt", "wb");
         fwrite("test data\n", 1, 10, file);
         fwrite("test data\n", 1, 10, file2);
         fclose(file);
         fclose(file2);
       };
};

int main(int argc, char* argv[])
{

FILE *file;
FILE *file2;

   SetupCallbacks();

   sceKernelDcacheWritebackAll();
   pspDebugScreenInit();
   //sceGuStart(GU_DIRECT,list);
   //sceGuInit();

   pspDebugScreenPrintf("init logger\n");
   //logger.Log();
   //g_logger.Init("ms0:/PSP/GAME/log.txt");
   //pspDebugScreenPrintf("init content\n");

   //g_ContentManager.Init("ms0:/PSP/GAME");

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

      sceGuClearColor(0x554433);
      sceGuClearDepth(0);
      sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);


      sceGuFinish();
      sceGuSync(0,0);

      sceDisplayWaitVblankStart();
      sceGuSwapBuffers();*/
   }

   //sceGuTerm();

   sceKernelExitGame();
   return 0;
}

/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
   //sceKernelExitGame();
   g_exit=1;
   return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
   int cbid;

   cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
   sceKernelRegisterExitCallback(cbid);

   sceKernelSleepThreadCB();

   return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
   int thid = 0;

   thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
   if(thid >= 0)
   {
      sceKernelStartThread(thid, 0, 0);
   }

   return thid;
}



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.
Back to top
View user's profile Send private message
Agoln



Joined: 08 Jun 2005
Posts: 326
Location: Fort Wayne, IN

PostPosted: Fri Aug 26, 2005 9:38 am    Post subject: Reply with quote

Ever try adding 'extern C' to C++ code?
_________________
Lego of my Ago!
Back to top
View user's profile Send private message AIM Address
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Fri Aug 26, 2005 9:41 am    Post subject: Reply with quote

Agoln wrote:
Ever try adding 'extern C' to C++ code?


Er, what?
Back to top
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Fri Aug 26, 2005 9:45 am    Post subject: Reply with quote

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 :).
Back to top
View user's profile Send private message
Thanhda



Joined: 09 Apr 2005
Posts: 331
Location: Canada

PostPosted: Fri Aug 26, 2005 1:09 pm    Post subject: Reply with quote

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:

TARGET = copy
OBJS = copy.o

INCDIR =
CFLAGS = -02 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)

USE_PSPSDK_LIBC = 1

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

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = CopyImage Sample

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/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:

#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"
{
#endif

PSP_MODULE_INFO("CopyImage Sample", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);

#define printf  pspDebugScreenPrintf

class test
{
        public:
        void hey();
};

void test::hey() { }
....
....
....
int main(int argc, char**argv)
{
    test *t=new test;
    t->hey();
    ...
}
...
....
#ifndef __cplusplus
}; //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/viewtopic.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...
Back to top
View user's profile Send private message Visit poster's website
Krevnik



Joined: 09 Mar 2005
Posts: 71

PostPosted: Fri Aug 26, 2005 11:29 pm    Post subject: Reply with quote

You could probably just wrap the main function in the extern C, IIRC as well.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group