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++ problem

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



Joined: 01 Feb 2006
Posts: 26
Location: Sydney

PostPosted: Thu Feb 09, 2006 10:38 pm    Post subject: C++ problem Reply with quote

I'm trying to write some C++ library classes, so i can reuse them for all of my projects, and having some trouble. For some reason it refuses to link, and i cannot see what it is.

Here is source, and error, can anyone help.

main.cpp
Code:

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

#include "PspExitHandler.h"

PSP_MODULE_INFO("testClass_cpp", 0, 1, 1);

int main()
{
    pspDebugScreenInit();
    PspExitHandler::SetupCallbacks();
           
    pspDebugScreenPrintf("Hello World");
    sceKernelSleepThread();
    return(0);
}


PspExitHandler.cpp
Code:

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

class PspExitHandler
{
    public:
           
        static int SetupCallbacks(void)
       {
            int thid = sceKernelCreateThread("update_thread", PspExitHandler::callbackThread, 0x11, 0xFA0, 0, 0);

            if (thid >= 0) sceKernelStartThread(thid, 0, 0);
            return(0);       
        }
   
    private:
           
       static int exit_callback(int arg1, int arg2, void * common)
       {
            sceKernelExitGame();
            return(0);
       }
   
       static int callbackThread(SceSize args, void *argp)
       {
            int cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
            sceKernelRegisterExitCallback(cbid);

            sceKernelSleepThreadCB();
            return(0);               
       }
};


PspExitHandler.h
Code:

class PspExitHandler
{
    public:

        static int SetupCallbacks(void);

    private:
 
        static int exit_callback(int arg1, int arg2, void * common);
       static int callbackThread(SceSize args, void *argp);
};


makefile
Code:

TARGET = testClass
OBJS = main.o PspExitHandler.o

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


LIBDIR =
LDFLAGS =
LIBS = -lstdc++ -lpng -lz -lm -lpspgu

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Test Class

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak


Compile output
Code:

psp-g++ -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall  -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall  -fno-exceptions -fno-rtti   -c -o main.o main.cpp
psp-g++ -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall  -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall  -fno-exceptions -fno-rtti   -c -o PspExitHandler.o PspExitHandler.cpp
psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall   -L. -L/usr/local/pspdev/psp/sdk/lib   main.o PspExitHandler.o -lstdc++ -lpng -lz -lm -lpspgu  -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspsdk -lc -lpspnet -lpspnet_inet -lpspnet_apctl -lpspnet_resolver -lpsputility -lpspuser -lpspkernel -o testClass.elf

main.o In function `main':
main.cpp(.text+0x10): undefined reference to `PspExitHandler::SetupCallbacks()'

collect2: ld returned 2 exit status
make: *** [testClass.elf] Error 1
Back to top
View user's profile Send private message
Dr. Vegetable



Joined: 14 Nov 2005
Posts: 171
Location: Boston, Massachusetts

PostPosted: Thu Feb 09, 2006 11:13 pm    Post subject: Reply with quote

I don't see any obvious syntactical probems with your code. However, I am able to get it to link if I modify your PspExitHandler.cpp file to look like this:

Code:

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

//static
int PspExitHandler::SetupCallbacks(void)
{
    int thid = sceKernelCreateThread("update_thread", PspExitHandler::callbackThread, 0x11, 0xFA0, 0, 0);

    if (thid >= 0) sceKernelStartThread(thid, 0, 0);
        return(0);       
}
   
//static
int PspExitHandler::exit_callback(int arg1, int arg2, void * common)
{
    sceKernelExitGame();
    return(0);
}
   
//static
int PspExitHandler::callbackThread(SceSize args, void *argp)
{
    int cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
    sceKernelRegisterExitCallback(cbid);

    sceKernelSleepThreadCB();
    return(0);               
}


Note that this code is identical to yours, except that I have moved the static function definitions "out-of-line."

As a matter of style, I usually try to avoid having more than one copy of a class declaration, and it is possible that the compiler is somehow deciding that these are two different physical classes. By using the same .h file both for references to your class and for the definition of your class, you avoid potential problems keeping the two files in sync.

Hope this helps!
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger
ipsp



Joined: 01 Feb 2006
Posts: 26
Location: Sydney

PostPosted: Thu Feb 09, 2006 11:28 pm    Post subject: Reply with quote

Dr. Vegetable wrote:


As a matter of style, I usually try to avoid having more than one copy of a class declaration, and it is possible that the compiler is somehow deciding that these are two different physical classes. By using the same .h file both for references to your class and for the definition of your class, you avoid potential problems keeping the two files in sync.

Hope this helps!


What you said makes perfect sense and helps alot, I'm personally having trouble adjusting from Java and C, to C++ but that helped heaps.

Thanks
Back to top
View user's profile Send private message
seventh



Joined: 21 Jan 2006
Posts: 11

PostPosted: Fri Feb 10, 2006 5:57 am    Post subject: Reply with quote

The problem is, your .cpp file does not only provide implementation, it also provides interface. In theory, you should have

MyClass.h

Code:

class MyClass {

 public : static void my_function( void );
};


MyClass.cpp
Code:

#include "MyClass.h"

void MyClass::my_function( void ) { /* ... */ }


It is just that you don't use C++ the good way, nothing to do with specific PSP programming
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