Getting g++ to work

Discuss the development of software, tools, libraries and anything else that helps make ps2dev happen.

Moderators: cheriff, Herben

Lousyphreak
Posts: 6
Joined: Tue Jan 25, 2005 5:28 pm

Getting g++ to work

Post 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
Last edited by Lousyphreak on Thu Jan 27, 2005 5:15 am, edited 1 time in total.
pixel
Posts: 791
Joined: Fri Jan 30, 2004 11:43 pm

Post 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.
pixel: A mischievous magical spirit associated with screen displays. The computer industry has frequently borrowed from mythology. Witness the sprites in computer graphics, the demons in artificial intelligence and the trolls in the marketing department.
Lousyphreak
Posts: 6
Joined: Tue Jan 25, 2005 5:28 pm

Post 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
boomint
Posts: 80
Joined: Tue Apr 13, 2004 2:21 am
Location: Sheffield, UK

Post 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
--( someone stole my sig! )--
Lousyphreak
Posts: 6
Joined: Tue Jan 25, 2005 5:28 pm

Post 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 ;)
BraveDog
Posts: 29
Joined: Thu Dec 30, 2004 1:16 am
Location: Cleveland

Post 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
Lousyphreak
Posts: 6
Joined: Tue Jan 25, 2005 5:28 pm

Post 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
mharris
Posts: 155
Joined: Sun Jan 25, 2004 2:26 pm
Location: Annapolis, MD, USA

Post 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...)
rinco
Posts: 255
Joined: Fri Jan 21, 2005 2:12 pm
Location: Canberra, Australia

Post 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 */
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post 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.
"He was warned..."
pixel
Posts: 791
Joined: Fri Jan 30, 2004 11:43 pm

Post 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...
pixel: A mischievous magical spirit associated with screen displays. The computer industry has frequently borrowed from mythology. Witness the sprites in computer graphics, the demons in artificial intelligence and the trolls in the marketing department.
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

Sounds like you need to add the linkfile:

-T$PS2SDK/ee/startup/linkfile
User avatar
Neil Stevens
Posts: 79
Joined: Thu Jan 27, 2005 2:22 pm
Location: California
Contact:

Post 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.
pixel
Posts: 791
Joined: Fri Jan 30, 2004 11:43 pm

Post by pixel »

Just a hint: -lsyscall isn't useful anymore.
pixel: A mischievous magical spirit associated with screen displays. The computer industry has frequently borrowed from mythology. Witness the sprites in computer graphics, the demons in artificial intelligence and the trolls in the marketing department.
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

And baggy pants went out of style with MC Hammer...
"He was warned..."
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

Shiny!
OptiRoc
Posts: 22
Joined: Mon Feb 02, 2004 12:26 am
Location: Sweden

Post 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)
User avatar
Neil Stevens
Posts: 79
Joined: Thu Jan 27, 2005 2:22 pm
Location: California
Contact:

Post 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.
OptiRoc
Posts: 22
Joined: Mon Feb 02, 2004 12:26 am
Location: Sweden

Post 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?
User avatar
Neil Stevens
Posts: 79
Joined: Thu Jan 27, 2005 2:22 pm
Location: California
Contact:

Post 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.
OptiRoc
Posts: 22
Joined: Mon Feb 02, 2004 12:26 am
Location: Sweden

Post 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..
pixel
Posts: 791
Joined: Fri Jan 30, 2004 11:43 pm

Post by pixel »

The obvious error is you didn't #include <stdio.h> in your test.cpp file...
pixel: A mischievous magical spirit associated with screen displays. The computer industry has frequently borrowed from mythology. Witness the sprites in computer graphics, the demons in artificial intelligence and the trolls in the marketing department.
OptiRoc
Posts: 22
Joined: Mon Feb 02, 2004 12:26 am
Location: Sweden

Post 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..).
User avatar
Neil Stevens
Posts: 79
Joined: Thu Jan 27, 2005 2:22 pm
Location: California
Contact:

Post by Neil Stevens »

C is not C++. C lets you get away with junk that C++ disallows.
Post Reply