Compiler has an issue with strings

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

Moderators: cheriff, TyRaNiD

Post Reply
funkmeister
Posts: 8
Joined: Wed May 10, 2006 10:25 am

Compiler has an issue with strings

Post by funkmeister »

I've been trying for days to get my script to compile using a simple string, but but simply refuses to. I've run it by several veteran coders, and referenced it against many websites, but everyone said it looks like it should compile. Any help is greatly appreciated.

Code: Select all

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

using namespace std;

PSP_MODULE_INFO&#40;"Use Strings", 0, 1, 1&#41;;
#define printf pspDebugScreenPrintf 
#define RGB&#40;r, g, b&#41; &#40;&#40;r&#41;|&#40;&#40;g&#41;<<8&#41;|&#40;&#40;b&#41;<<16&#41;&#41;
#define clearsc pspDebugScreenClear

/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
          sceKernelExitGame&#40;&#41;;
          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;

int main&#40;void&#41;
&#123;
    //string mystring = "text";
    SetupCallbacks&#40;&#41;;
    printf&#40;"Display some text %d",mystring&#41;;
    sceKernelSleepThread&#40;&#41;;
    return 0;
&#125;
The Makefile I'm using is: (I figured I'd throw in all those libs hoping one of them would work)

Code: Select all

TARGET = MakeString
OBJS = main.o

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

LIBDIR =
LIBS = -lpspgu -lpsppower -lpng -lz -lm -lmad -lpspaudiolib -lpspaudio -lpsprtc -lpsphprm -lpspusb -lpspusbstor -lpsputility -lstdc++

LDFLAGS =

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Make String

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak
funkmeister
Posts: 8
Joined: Wed May 10, 2006 10:25 am

Post by funkmeister »

Forgot two things
1) I'm aware I have the mystring line commented, I forgot to uncomment it before posting

2) the errors I get are
error: "syntax error before 'namespace'"
warning: "type defaults to 'int' declaration of 'std'"
warning: "data definition has no type or storage class"

occasionally, it refuses to recognize that <string> exists

I'm using cygwin to compile, Dev-C++ to code, and the most recent stable build of the toolchain.
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

hehe wow ...well for you all you need to do is
alter just two little thing :P
first your declaration is wrong should be

Code: Select all

string mystring&#91;&#93; = "test";
this

Code: Select all

printf&#40;"Display some text %d",mystring&#41;;
into

Code: Select all

printf&#40;"Display some text %s",mystring&#41;;
notice the %s ...that means that the argument would correspond
to a string and that printf should print that argument into a string
and not an integer (%d) which you had above ...also all these
are not needed

Code: Select all

 LIBS = -lpspgu -lpsppower -lpng -lz -lm -lmad -lpspaudiolib -lpspaudio -lpsprtc -lpsphprm -lpspusb -lpspusbstor -lpsputility -lstdc++ 
this is fine

Code: Select all

LIBS = -lstdc++
10011011 00101010 11010111 10001001 10111010
funkmeister
Posts: 8
Joined: Wed May 10, 2006 10:25 am

Post by funkmeister »

Nope, that's not the problem. The code was an except from a MUCH larger program, that's why I have all the libs on the makefile. The problem is that it doesn't recognize "using". Tried recompiling both my program AND the toolchain, but no luck.
PlayfulPuppy
Posts: 22
Joined: Fri May 05, 2006 12:04 am

Post by PlayfulPuppy »

dot_blank wrote:hehe wow ...well for you all you need to do is
alter just two little thing :P
first your declaration is wrong should be

Code: Select all

string mystring&#91;&#93; = "test";
No it shouldn't. It's a C++ string class, not a C-style string.
this

Code: Select all

printf&#40;"Display some text %d",mystring&#41;;
into

Code: Select all

printf&#40;"Display some text %s",mystring&#41;;
Should actually be this:

Code: Select all

printf&#40;"Display some text %s", mystring.c_str&#40;&#41;&#41;;
As for the compilation errors you're getting, try replacing this:

Code: Select all

#include <string.h>
with this:

Code: Select all

#include <string>
Also make sure your file has a .CPP extension, otherwise I'm fairly sure it'll try and compile it using a C compiler (Which doesn't have the 'using' keyword nor templates). Either way, make sure MAKE is executing vs-psp-g++ instead of vs-psp-gcc.
danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

Post by danzel »

If this was meant to be c++ then make sure your file is named something.cpp so that it is compiled by g++ and not gcc (you will see in the compile output which one was used)

also

Code: Select all

    //string mystring = "text";
    SetupCallbacks&#40;&#41;;
    printf&#40;"Display some text %d",mystring&#41;; 
that should be:

Code: Select all

    string mystring = "text";
    SetupCallbacks&#40;&#41;;
    printf&#40;"Display some text %s",mystring.c_str&#40;&#41;&#41;; 
and change
#include <string.h>
to
#include <string>

(NOTE: I assume you want to use c++ strings, and not c char* type strings)

Danzel.

ps. one other small thing fixing what dot_blank said:
string mystring[] = "test";
that should be
char mystring[] = "test";
if you are using c and not c++ strings, in this case keep #include <string.h>
funkmeister
Posts: 8
Joined: Wed May 10, 2006 10:25 am

Post by funkmeister »

oh...my...god....

After spending four days rearranging code, and recompiling...you're telling me my problem was the file extension?

[puts on n00b cap, draws a circle on the chalk board and put nose in it]

I was using main.c, and I'm kicking myself, since I had changed the extension to .cpp earlier, but changed it back before I attempted to implement strings since I had realized I wasn't doing anything at that point that required .cpp.

That would also explain why it would recognize <string.h>, but never <string>. Awesome, thanks everyone.
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

haha this is by far the most interesting thread
just for the fact to show that the user should first
realize that this forum is for psp DEV ...keyword is DEV
meaning that you would obviously have to know what
DEV is and how to DEV ....and big oops in my enjoyment
of this thread i over saw the mystring.c_str() :)

but in the end ....file extension is key :P
10011011 00101010 11010111 10001001 10111010
Post Reply