Page 1 of 2

Getting g++ to work

Posted: Tue Jan 25, 2005 8:19 pm
by Lousyphreak
ive done some ps2 toolchain installs the past days (1 on linux, 2 on cygwin) but with neither g++ was working...

everything works fine with c, i made testfiles with a plain main(), adding a class, and using new. plain main() works (using ee-g++), but as soon as i add a c++ feature the linker complains about unresolved symbols.

ive even tried replacing ee-gcc with ee-g++ as the linker but neither works. ill give you error messages when i come home, currently the only thing i remember is CreateSema and fioRead and the likes (iirc they are in libkernel, i also has strlen missing which should be also there)

thx in advance

Posted: Tue Jan 25, 2005 8:32 pm
by pixel
Well, first, use ee-gcc as linker. ee-g++ has its specfiles a bit messed up. Second, you have to tell us exactly what are your errors AND your makefile (that is, we need to exactly know the ee-gcc command you're running in order to do the linking). Otherwise, we will only be able to provide unprecise and unuseful help.

Posted: Wed Jan 26, 2005 12:08 am
by Lousyphreak
i forgot to mention that i'm at work so i dont have access to my dev pc's, but in the meantime i installed ps2dev here@work and voila the exact same errors.

1. i installed the ps2sdk found here: http://ps2dev.org/kb.x?T=1081
2. set paths accordingly
3. cd to ps2dev\ps2lib\ee\samples\hello_c++
4. make (the makefiles are the default makefiles supplied with ps2dev)

resulting ee-gcc/g++ calls (including error):

Code: Select all

ee-g++ -D_EE -O2 -G0 -Wall  -IH:\prog\ps2dev\gcc/ee/include -IH:\prog\ps2dev\gcc/ee/lib/gcc-lib/ee/3.2.2/include -IH:\prog\ps2dev\gcc/ee/ee/include -IH:\prog\ps2dev\ps2lib/ee/include -IH:\prog\ps2dev\ps2lib/common/include -I.  -c hello.cpp-o hello.o
ee-gcc -TH:\prog\ps2dev\ps2lib/ee/startup/linkfile -LH:\prog\ps2dev\ps2lib/ee/lib       \
         hello.o -lkernel -o hello.elf
hello.o(.data+0x11): In function `main':
hello.cpp: undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
make: *** [hello.elf] Error 1

if i replace ee-gcc with ee-g++ for the linker (interpreted __gxx_personality_v0 as a hint) and adding -lc to the end it looks more promising:

Code: Select all

ee-g++ -TH:\prog\ps2dev\ps2lib/ee/startup/linkfile -LH:\prog\ps2dev\ps2lib/ee/lib       \
         hello.o -lkernel -o hello.elf -lc
/cygdrive/h/prog/ps2dev/gcc/ee/bin/../lib/gcc-lib/ee/3.2.2/../../../../ee/lib/libc.a(sbrk.o)(.text+0x8): In function `sbrk':
../../../../../../newlib/libc/sys/ps2/sbrk.c:8: undefined reference to `ps2_sbrk'
/cygdrive/h/prog/ps2dev/gcc/ee/bin/../lib/gcc-lib/ee/3.2.2/../../../../ee/lib/libc.a(close.o)(.text+0x8): In function `close':
../../../../../../newlib/libc/sys/ps2/close.c:6: undefined reference to `fioClose'
/cygdrive/h/prog/ps2dev/gcc/ee/bin/../lib/gcc-lib/ee/3.2.2/../../../../ee/lib/libc.a(lseek.o)(.text+0x8): In function `lseek':
../../../../../../newlib/libc/sys/ps2/lseek.c:8: undefined reference to `fioLseek'
/cygdrive/h/prog/ps2dev/gcc/ee/bin/../lib/gcc-lib/ee/3.2.2/../../../../ee/lib/libc.a(read.o)(.text+0x8): In function `read':
../../../../../../newlib/libc/sys/ps2/read.c:6: undefined reference to `fioRead'

collect2: ld returned 1 exit status
make: *** [hello.elf] Error 1

though i got my 7+ years of c++ programming on my back already im fairly new to the gnu toolchain. thats why i think the 2nd version is the "better" one, but still, i checked libkernel and the missing functions where there (simple grep ;) ) it is also linking to it (removing libkernel.a results in a "-lkernel not found" message) so im a bit at the end of my wisdom.

my first post was actually just a check if there was a known problem with g++ (if even the samples dont work).

hope this helps

Posted: Wed Jan 26, 2005 12:23 am
by boomint
You're using an old version that includes ps2lib rather that ps2sdk.

You'd be better off (and probably better supported!) using the toolchain.sh script found in the stable section on ooPo's site :

http://www.oopo.net/consoledev

Posted: Wed Jan 26, 2005 5:22 pm
by Lousyphreak
now built with toolchain.sh:

Code: Select all

bash-2.05b$ ee-g++ test.cpp -D_EE -O2  -c
bash-2.05b$ ee-gcc -T/usr/local/ps2dev/ps2sdk/ee/startup/linkfile -L$PS2SDK/ee/lib test.o -lkernel -lstdc++ -lc -lkernel -lsyscall
/usr/local/ps2dev/ee/lib/gcc-lib/ee/3.2.2/libgcc.a(unwind-dw2.o)(.text+0x388): In function `extract_cie_info':
../../gcc/unwind-dw2.c:226: undefined reference to `strlen'
/usr/local/ps2dev/ee/lib/gcc-lib/ee/3.2.2/libgcc.a(unwind-dw2-fde.o)(.text+0x5d4): In function `get_cie_encoding':
../../gcc/unwind-dw2-fde.c:271: undefined reference to `strlen'
collect2: ld returned 1 exit status
so i built the ps2sdk in a subdir again and linked strlen.o to the executable, and suddenly it works:

Code: Select all

bash-2.05b$ ee-gcc -T/usr/local/ps2dev/ps2sdk/ee/startup/linkfile -L$PS2SDK/ee/lib test.o -lkernel -lstdc++ -lc -lkernel -lsyscall ps2sdk/ee/libc/obj/strlen.o
so now im curious: why isnt the ps2sdk libc used (i guess i can switch off the newlib libc), ans why is strlen not in libkernel (atleast i think it should be)?

oh btw, test.cpp:

Code: Select all

class x
{
public:
int muh;
int xy(){};
};

int main()
{
    x *y=new x();
    return 0;
}
i havent tested the resulting elf, for now i'm happy that it builds ;)

Posted: Thu Jan 27, 2005 1:40 am
by BraveDog
I have had this problem with libc functions as well. As a work around I added '-lc' in again at the end of libraries to be linked in.
Seemed to get rid of the problem.
-bd

Posted: Thu Jan 27, 2005 8:59 am
by Lousyphreak
the -lc workaround doesnt work for me :(
i just search for the compiled .o's which contain the needed function and add them to the linker, currently there are 13 files i need to link to manually. (im toying with scummvm using an old ps2 port and getting it up2date)

for now its working but still it bugs me why i need to link to objs which should already be in the used libs

Posted: Thu Jan 27, 2005 2:08 pm
by mharris
Lousyphreak wrote:

Code: Select all

hello.o(.data+0x11): In function `main':
hello.cpp: undefined reference to `__gxx_personality_v0'
Try adding this to your Makefile

Code: Select all

EE_CXXFLAGS += -fno-exceptions
This disables the extra code g++ adds to support C++ exceptions. Obviously, you won't be able to use exceptions if this flag is turned on (but your code will be smaller and faster...)

Posted: Tue Feb 01, 2005 1:45 am
by rinco
I would try overriding the new/delete operators, ie:

Code: Select all

void* operator new(size_t size) { return malloc(size); }
void operator delete(void* data) { if (data != 0) free(data); }
/* thanks #ps2dev */

void *operator new[]( size_t n )
    { return ::operator new[](n); }
void operator delete[]( void *p, size_t )
    { ::operator delete[](p); }
void operator delete[]( void *p )
    { ::operator delete[](p); }
/* thanks google */

Posted: Tue Feb 01, 2005 4:21 am
by mrbrown
Thenend wrote: I use these lines to compile and link:

ee-gcc main.cpp -D_EE -O0 -c -Wall -W -EL -G0 -mips3 -nostdlib
The -nostdlib line is useless here because you're compling an object module not a program. You generally don't want to use -nostdlib unless you know what you are doing.

Take out the -mips3 line, it's wrong for EE and it's possible it's what's causing your problems.
Thenend wrote: ee-gcc -L$PS2SDK/ee/lib main.o -lkernel -lstdc++ -lc -lkernel -lsyscall strlen.o g2.o dma_asm.o gs_asm.o ps2_asm.o -o main.elf
The critical error here is that you don't specify ps2sdk's crt0.o or linkfile. Now I'm 99% certain your program died because it's using newlib's crt0.o, which won't work on PS2.

Refer to the ps2sdk Makefiles or other programs/libraries in CVS for examples of the linker step to use for C++ programs.

Posted: Tue Feb 01, 2005 8:20 pm
by pixel
Thenend wrote:Yes, I suspected the problem was crt0.o but when I tried to link the ps2sdk one, I get redefinition errors and can't figure out how to take the newlib's crt0.o out of my program :-(
If you'd tell the errors, maybe we could work that out...

Posted: Tue Feb 01, 2005 11:12 pm
by ooPo
Sounds like you need to add the linkfile:

-T$PS2SDK/ee/startup/linkfile

Posted: Wed Feb 02, 2005 12:48 am
by Neil Stevens
If you add crt0.o to your link line, you have to add -mno-crt0 or you'll have a conflict.

Try adding both, while using the linkfile too.

Posted: Wed Feb 02, 2005 1:48 am
by pixel
Just a hint: -lsyscall isn't useful anymore.

Posted: Wed Feb 02, 2005 2:54 am
by mrbrown
And baggy pants went out of style with MC Hammer...

Posted: Wed Feb 02, 2005 3:59 am
by ooPo
Shiny!

Posted: Mon Feb 07, 2005 1:10 pm
by OptiRoc
I just tried to compile some basic C++ code and ran into the same problem: undefined reference to `strlen'.

Is rolling up and link an own strlen.o the best solution, or is there a cleaner way?

(btw, I just cooked up a fresh ps2dev environment using ooPo's script)

Posted: Mon Feb 07, 2005 10:49 pm
by Neil Stevens
Note that the supplied linker is dumb, in the sense that it doesn't do anything smart or tricky. List your libraries so that they all their dependencies to their right, and put the libraries to the right of your object files.

Posted: Tue Feb 08, 2005 12:00 am
by OptiRoc
OK. Using the makefile included with ps2sdk (Makefile.eeglobal_cpp) the linker command expands to this:

ee-gcc -T/usr/local/ps2dev/ps2sdk/ee/startup/linkfile L/usr/local/ps2dev/ps2sdk/ee/lib -o test.elf test.o -lc -lkernel -lstdc++ -lc -lkernel -lsyscall

Which won't resolve the reference to strlen. I was hoping that "-lc" would sort it out just fine. $PS2SDK/ee/lib/libc.a seem to feature an "strlen" function and it should be included with the above line, shouldn't it?

Posted: Tue Feb 08, 2005 1:25 am
by Neil Stevens
Oh, Makefile.eeglobal_cpp never worked for me, either. I just used Makefile.eeglobal and added -lstdc++ to my EE_LIBS. Haven't run into a problem yet.

Posted: Tue Feb 08, 2005 12:00 pm
by OptiRoc
After some tinkering I can build some C++ code, using both templates, new, etc. Printf() won't work though, with the exact same settings as when building the sample code (except the use of ee-g++ for compiling instead of ee-gcc).

When building the example, gcc throwns a warning:
hello.c:27: warning: implicit declaration of function `printf'

When using gcc I get an error instead:
test.cpp:29: `printf' undeclared (first use this function)


This is how the commands look. Any obvious errors?
ee-g++ -D_EE -O2 -G0 -Wall -I/usr/local/ps2dev/ps2sdk/ee/include -I/usr/local/ps2dev/ps2sdk/common/include -I. -c test.cpp -o test.o

ee-gcc -mno-crt0 -T/usr/local/ps2dev/ps2sdk/ee/startup/linkfile -s -L/usr/local/ps2dev/ps2sdk/ee/lib -o test.elf /usr/local/ps2dev/ps2sdk/ee/startup/crt0.o test.o -lstdc++ -lc -lgcc -lsyscall -lc -lkernel


Are you supposed to link libgcc as well? Can't see it mentioned anywhere here, though it solved the compilation problems for me..

Posted: Tue Feb 08, 2005 5:25 pm
by pixel
The obvious error is you didn't #include <stdio.h> in your test.cpp file...

Posted: Tue Feb 08, 2005 10:04 pm
by OptiRoc
Too true. :)

But neither did the example in $PS2SDK/samples/hello! I'm curious how printf() got declared there.

By the way, after some further tinkering I link the following libraries at the end of the linker command: -lkernel -lstdc++ -lc -lgcc -lkernel

I saw your comment that syscalls wasn't used anymore so I cut that one out, together with one "-lc" which seemed necessary after the "-lsyscalls" -- and put another "-lkernel" in front (which was suggested somewhere, I believe..).

Posted: Tue Feb 08, 2005 10:42 pm
by Neil Stevens
C is not C++. C lets you get away with junk that C++ disallows.