C++ linking problems with new & delete operator, and oth

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

Moderators: cheriff, TyRaNiD

Post Reply
cooleyandy
Posts: 41
Joined: Sat Jul 02, 2005 10:12 am

C++ linking problems with new & delete operator, and oth

Post by cooleyandy »

When compiling my c++ program, I've gotten some strange errors.

main.o: In function `spr_file::destroy()':
main.cpp:(.text+0x44c): undefined reference to `operator delete(void*)'
main.o: In function `spr_file::create(unsigned int)':
main.cpp:(.text+0x474): undefined reference to `operator new[](unsigned int)'
main.o: In function `spr_file::load(char*)':
main.cpp:(.text+0x5ac): undefined reference to `__srget'
main.o: In function `game::destroy_level()':
main.cpp:(.text+0x914): undefined reference to `operator delete(void*)'
main.o: In function `game::show_split_in(int, char*)':
main.cpp:(.text+0x2088): undefined reference to `operator new[](unsigned int)'
main.cpp:(.text+0x2328): undefined reference to `operator delete(void*)'
main.o: In function `game::show_split_out(int, char*)':
main.cpp:(.text+0x24b8): undefined reference to `operator new[](unsigned int)'
main.cpp:(.text+0x2860): undefined reference to `operator delete(void*)'
main.o: In function `game::show_scroll_down(int, char*)':
main.cpp:(.text+0x2904): undefined reference to `operator new[](unsigned int)'
main.cpp:(.text+0x2a5c): undefined reference to `operator delete(void*)'
main.o: In function `game::create_level(unsigned int)':
main.cpp:(.text+0x4288): undefined reference to `operator new[](unsigned int)'
main.o: In function `game::load_high_score(char*)':
main.cpp:(.text+0x4490): undefined reference to `fscanf'
main.o: In function `game::play_boss_level()':
main.cpp:(.text+0x77a0): undefined reference to `operator new[](unsigned int)'
main.cpp:(.text+0x7898): undefined reference to `operator delete(void*)'
scn320.o: In function `scnpalette::~scnpalette()':
scn320.cpp:(.text+0x14): undefined reference to `operator delete(void*)'
scn320.o: In function `scn320::~scn320()':
scn320.cpp:(.text+0x34): undefined reference to `operator delete(void*)'
scn320.cpp:(.text+0x3c): undefined reference to `operator delete(void*)'
scn320.cpp:(.text+0x58): undefined reference to `operator delete(void*)'
scn320.o:scn320.cpp:(.text+0x60): more undefined references to `operator delete(
void*)' follow
scn320.o: In function `scnpalette::scnpalette()':
scn320.cpp:(.text+0x134): undefined reference to `operator new[](unsigned int)'
scn320.o: In function `scn320::scn320(int)':
scn320.cpp:(.text+0x1d0): undefined reference to `operator new[](unsigned int)'
scn320.cpp:(.text+0x1dc): undefined reference to `operator new[](unsigned int)'
scn320.cpp:(.text+0x1f8): undefined reference to `cos'
scn320.cpp:(.text+0x234): undefined reference to `sin'

I'm not sure if it helped, but I also tried editing build.mak and using the newlib instead via "USE_NEWLIB_LIBC = 1" but I got more errors

/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(fstatr.o): In fun
ction `_fstat_r':
../../../../../newlib/libc/reent/fstatr.c:62: undefined reference to `fstat'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(openr.o): In func
tion `_open_r':
../../../../../newlib/libc/reent/openr.c:59: undefined reference to `open'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(sbrkr.o): In func
tion `_sbrk_r':
../../../../../newlib/libc/reent/sbrkr.c:60: undefined reference to `sbrk'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(timer.o): In func
tion `_times_r':
../../../../../newlib/libc/reent/timer.c:64: undefined reference to `times'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(timer.o): In func
tion `_gettimeofday_r':
../../../../../newlib/libc/reent/timer.c:108: undefined reference to `gettimeofd
ay'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(writer.o): In fun
ction `_write_r':
../../../../../newlib/libc/reent/writer.c:58: undefined reference to `write'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(closer.o): In fun
ction `_close_r':
../../../../../newlib/libc/reent/closer.c:53: undefined reference to `close'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(lseekr.o): In fun
ction `_lseek_r':
../../../../../newlib/libc/reent/lseekr.c:58: undefined reference to `lseek'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(readr.o): In func
tion `_read_r':
../../../../../newlib/libc/reent/readr.c:58: undefined reference to `read'

and this is when I used the newlib.

My questions is, are some functions not developed yet, such as the new and delete operator, and fgetc(), fscanf().
beatwho
Posts: 28
Joined: Wed Dec 15, 2004 4:58 pm

Post by beatwho »

I also get those errors when I use the USE_NEWLIB_LIBC = 1 option. although I too am using new and delete and never had a problem with them.

as a guess, check youre including the right headers (and linking with the right libs). Im at work so cant check at the moment if i did anything special.
cooleyandy
Posts: 41
Joined: Sat Jul 02, 2005 10:12 am

Post by cooleyandy »

Okay, I've edited the build.mak again and found out about how to link different libraries. so now I've linked most of the libraries including the kitchen sink. I'm still however coming up with the following errors.

$ make
psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall -L. -L/usr/local/
pspdev/psp/sdk/lib main.o scn320.o keyboard.o sfx.o -lpspdebug -lpsplibc -lpspkernel -lstdc++ -lm -lg -lpspglue -lc -lsupc++ -liberty -o sdktest.elf
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libg.a(sbrkr.o): In function `_sbrk_r':
../../../../../newlib/libc/reent/sbrkr.c:60: undefined reference to `sbrk'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libg.a(writer.o): In function `_write_r':
../../../../../newlib/libc/reent/writer.c:58: undefined reference to `write'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libg.a(closer.o): In function `_close_r':
../../../../../newlib/libc/reent/closer.c:53: undefined reference to `close'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libg.a(fstatr.o): In fun
ction `_fstat_r':
../../../../../newlib/libc/reent/fstatr.c:62: undefined reference to `fstat'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libg.a(lseekr.o): In fun
ction `_lseek_r':
../../../../../newlib/libc/reent/lseekr.c:58: undefined reference to `lseek'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libg.a(readr.o): In func
tion `_read_r':
../../../../../newlib/libc/reent/readr.c:58: undefined reference to `read'
collect2: ld returned 1 exit status
make: *** [sdktest.elf] Error 1

Which libraries is suppose to contain the functions "lseek, read" etc...?
Yardbird
Posts: 11
Joined: Tue May 31, 2005 4:56 am

Post by Yardbird »

The library order is important (see also this thread).

I think you should use -lstdc++ -lc -lpspglue.
Also, according to mrbrown, you should use libpsplibc, or libc + libpspglue; not both together.

Doing this, I only have 1 error (after updating with svn):

Code: Select all

/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libstdc++.a(pure.o):../../../../libstdc++-v3/libsupc++/pure.cc:54: undefined reference to `write'
collect2: ld returned 1 exit status
make: *** [.elf] Error 1
However, when I use -lpsplibc -lstdc++ -lc -lpspglue in this specific order (using both libpsplibc and libc, which I shouldn't do), even this last error disappears...

I would be glad if anyone can tell more about this...
cooleyandy
Posts: 41
Joined: Sat Jul 02, 2005 10:12 am

Post by cooleyandy »

I've tried -lstdc++ -lc -lpspglue and it actually resulted in more errors

$ make
psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall -L. -L/usr/local/
pspdev/psp/sdk/lib main.o scn320.o keyboard.o sfx.o -lpspdebug -lstdc++ -lm -
lc -lpspglue -lpspkernel -o sdktest.elf
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(fstatr.o): In fun
ction `_fstat_r':
../../../../../newlib/libc/reent/fstatr.c:62: undefined reference to `fstat'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(openr.o): In func
tion `_open_r':
../../../../../newlib/libc/reent/openr.c:59: undefined reference to `open'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(sbrkr.o): In func
tion `_sbrk_r':
../../../../../newlib/libc/reent/sbrkr.c:60: undefined reference to `sbrk'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(signalr.o): In fu
nction `_kill_r':
../../../../../newlib/libc/reent/signalr.c:61: undefined reference to `kill'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(signalr.o): In fu
nction `_getpid_r':
../../../../../newlib/libc/reent/signalr.c:96: undefined reference to `getpid'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(timer.o): In func
tion `_times_r':
../../../../../newlib/libc/reent/timer.c:64: undefined reference to `times'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(timer.o): In func
tion `_gettimeofday_r':
../../../../../newlib/libc/reent/timer.c:108: undefined reference to `gettimeofd
ay'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(writer.o): In fun
ction `_write_r':
../../../../../newlib/libc/reent/writer.c:58: undefined reference to `write'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(closer.o): In fun
ction `_close_r':
../../../../../newlib/libc/reent/closer.c:53: undefined reference to `close'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(lseekr.o): In fun
ction `_lseek_r':
../../../../../newlib/libc/reent/lseekr.c:58: undefined reference to `lseek'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(readr.o): In func
tion `_read_r':
../../../../../newlib/libc/reent/readr.c:58: undefined reference to `read'
collect2: ld returned 1 exit status
make: *** [sdktest.elf] Error 1

*screaming and tearing hair*
Yardbird
Posts: 11
Joined: Tue May 31, 2005 4:56 am

Post by Yardbird »

You should update your toolchain.
When I omit the pspglue, I get the same errors as you, but with undefined references to _read, _lseek, _fstat etc. (underscore in the beginning).
-> your toolchain is older, since you don't have the underscores.
This was also mentioned in mrbrown' s post...
cooleyandy
Posts: 41
Joined: Sat Jul 02, 2005 10:12 am

Post by cooleyandy »

My current configuration is http://forums.ps2dev.org/viewtopic.php?t=2374

How do I update the toolchain? (such as what commands I should type in).
I'm a bit green on all this command line stuff.
th0mas
Posts: 43
Joined: Sun Apr 24, 2005 1:59 am
Location: Canada
Contact:

Post by th0mas »

you're using psp-gcc. Try using psp-g++?
Yardbird
Posts: 11
Joined: Tue May 31, 2005 4:56 am

Post by Yardbird »

cooleyandy wrote:My current configuration is http://forums.ps2dev.org/viewtopic.php?t=2374

How do I update the toolchain? (such as what commands I should type in).
I'm a bit green on all this command line stuff.
You can get new stuff from the svn repository.
But regarding the toolchain, I think the psptoolchain-20050625 is still valid; if you run toolchain.sh again, it will probably download new components -> can someone verify this, I only expect this because of this post of ooPo...
cooleyandy
Posts: 41
Joined: Sat Jul 02, 2005 10:12 am

Post by cooleyandy »

th0mas wrote:you're using psp-gcc. Try using psp-g++?
Tried it, same errors.

You can get new stuff from the svn repository.
But regarding the toolchain, I think the psptoolchain-20050625 is still valid; if you run toolchain.sh again, it will probably download new components -> can someone verify this, I only expect this because of this post of ooPo...
I deleted the pspsdk folder, deleted the psp1.0+beta.tar.gz, and redownloaded the toolchain, and then followed the directions from

http://forums.ps2dev.org/viewtopic.php?t=2374

again. Same errors. Let me be a bit more detailed in what I'm compiling.
I'm compiling a program using 'make' and the project has multiple files.

main.cpp
sfx.cpp
scn320.cpp
keyboard.cpp

I've edited the MakeFile to have
main.o sfx.o scn320.o keyboard.o

I've been porting an old game and all the old compilation errors are fixed. (i.e. char far, old compiler specific funcs) but I just can't seem to get the code compiled.

Anyone who have successfully compiled a C++ program that uses new, delete, please let me know what configuration you have used (makefile and build.mak) and what libraries you included and what order you put it in.
beatwho
Posts: 28
Joined: Wed Dec 15, 2004 4:58 pm

Post by beatwho »

i have ported quite a bit of my c++ stuff over and it all works, but not with newlib, if i link with -lc etc it gives me the same errors. You will just have to live with libpspstdc++ or whatever its called and the sdk libc stuff. I have used new and delete and a bunch of other stuff fine with it.
emigree
Posts: 5
Joined: Mon May 09, 2005 5:29 am

Post by emigree »

Yes, it seems that compiling c++ programs work if you link with -lpsplibc -lstdc++. new and delete works but if you try to use any stl stuff like string, vector or whatever you get alot of link errors.
cooleyandy
Posts: 41
Joined: Sat Jul 02, 2005 10:12 am

Post by cooleyandy »

Okay, I've finally isolated the problem. It seems if I use the function getc() or fscanf(), the errors:

/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(sbrkr.o): In func
tion `_sbrk_r':
../../../../../newlib/libc/reent/sbrkr.c:60: undefined reference to `sbrk'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(writer.o): In fun
ction `_write_r':
../../../../../newlib/libc/reent/writer.c:58: undefined reference to `write'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(closer.o): In fun
ction `_close_r':
../../../../../newlib/libc/reent/closer.c:53: undefined reference to `close'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(lseekr.o): In fun
ction `_lseek_r':
../../../../../newlib/libc/reent/lseekr.c:58: undefined reference to `lseek'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(readr.o): In func
tion `_read_r':
../../../../../newlib/libc/reent/readr.c:58: undefined reference to `read'
/usr/local/pspdev/lib/gcc/psp/4.0.0/../../../../psp/lib/libc.a(fstatr.o): In fun
ction `_fstat_r':
../../../../../newlib/libc/reent/fstatr.c:62: undefined reference to `fstat'
collect2: ld returned 1 exit status
make: *** [sdktest.elf] Error 1

occurs. I've followed the other post from tommybear and got the new and delete operator working.
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

Upgrade your toolchain.
cooleyandy
Posts: 41
Joined: Sat Jul 02, 2005 10:12 am

Post by cooleyandy »

I've followed opoo's directions to upgrade the toolchain with the 07-25-2005 one. Now everything besides scanf works flawlessly but I can work around it for now. Thanks MrBrown and everyone else for all the help. Yay :-)
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

Yeah, the 07-25-2005 one will be great when its released in 19 days. :)
Post Reply