NickThePowerful / iphone-dev

Automatically exported from code.google.com/p/iphone-dev
0 stars 0 forks source link

C++ exceptions broken #48

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I don't seem to be able to build C++ successfully applications with the
toolchain.

Attempting to link a C++ program first produces the error:
/usr/local/bin/arm-apple-darwin-ld: can't locate file for: -lstdc++

If I manually create a symlink from /usr/lib/libstdc++.6.0.4.dylib to
/usr/lib/stdc++.dylib, I get past this error.  Apparently the arm linker is
not recognizing the default lib version like the native one does.

After twiddling the library like this, a more severe error appears:
/usr/local/bin/arm-apple-darwin-ld: Undefined symbols:
__Unwind_Resume

An analysis of libgcc_s.1.dylib shows that the OS X desktop version
contains the correct symbol, but the arm version contains the symbol
__Unwind_SjLj_Resume (and other similar symbols) instead.  I assume this is
related to enabling sjlj exceptions for ObjC, but that compiler option
doesn't apply to C++ code.

Because of this, I cannot link C++ apps - at least those that use exceptions.

Original issue reported on code.google.com by roger.a....@gmail.com on 20 Sep 2007 at 4:37

GoogleCodeExporter commented 9 years ago
To test, try building the RtAudio library 
(http://www.music.mcgill.ca/~gary/rtaudio/)
and their "probe" example (http://www.music.mcgill.ca/~gary/rtaudio/probe.html)

Configure the library with:
./configure --host=arm-apple-darwin AR=/usr/local/bin/arm-apple-darwin-ar

Original comment by roger.a....@gmail.com on 20 Sep 2007 at 4:43

GoogleCodeExporter commented 9 years ago
On digging into this more, it's clear that g++ is emitting code for standard
exceptions, rather than SJLJ exceptions, even though I've enabled SJLJ 
exceptions as
shown in the instructions.  The Objective-C++ compiler appears to do the same.

Oddly, I don't see either symbol set (__Unwind_SjLj_* or __Unwind_*) in sample
Objective-C object files, even though I'm using exceptions in them.

Original comment by roger.a....@gmail.com on 20 Sep 2007 at 9:39

GoogleCodeExporter commented 9 years ago
I got the same error. Is there a workaround or something? Is there somebody 
working on this one?

Thanks for the great tool, btw!

Christian

Original comment by chbee...@googlemail.com on 23 Sep 2007 at 7:47

GoogleCodeExporter commented 9 years ago
Ok, this is a tough one. I've known for some time that C++ exceptions are 
broken: this is why you see the 
enable-sjlj-exceptions stuff. There isn't a whole lot that can be done about it 
right now; LLVM doesn't support 
SjLj-type exceptions at all. I've provided a fix that will enable the 
exceptions to link, but as we have to use SjLj 
exceptions on the ARM (unless someone wants to reverse engineer the ARM's 
zero-cost exception mechanism) 
we're pretty much stuck here unless someone wants to do a lot of work to 
implement SJLJ-type exceptions in 
LLVM-GCC. Sorry.

Original comment by nightwat...@gmail.com on 23 Sep 2007 at 9:03

GoogleCodeExporter commented 9 years ago
No problem, I was afraid that might be the case.  I would guess that internally 
Apple
isn't giving much thought to C++ on the iPhone, and I wouldn't either - if I 
didn't
have existing apps to migrate to it.

Thanks for the explanation, and the link fix.

Original comment by roger.a....@gmail.com on 23 Sep 2007 at 9:39

GoogleCodeExporter commented 9 years ago
Not sure whether it is related, but I found that enabling exceptions for streams
causes programs to abort although the stream is in the "good" state when the 
program
is run without enabled exceptions. A catch() block inside Apple's own
libstdc++.6.0.4.dylib which is not entered as it should be?

Here's an example:
----------------------------------------------------------
#include <fstream>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    ofstream out;
    out.open(argv[1]);
    cout << (out.good() ? "good" : "bad") << endl;
    out.exceptions(ios_base::badbit|ios_base::failbit|ios_base::eofbit);
    out << "test\n";
    cout << (out.good() ? "good" : "bad") << endl;

    return 0;
}
----------------------------------------------------------
When run it fails with:
terminate called after throwing an instance of 'std::ios_base::failure'
  what():  basic_ios::clear
Abort trap

Original comment by patrick....@googlemail.com on 7 Oct 2007 at 6:52

GoogleCodeExporter commented 9 years ago
ubuntu feisty

$ cat a.cpp
int main()
{
}
$ arm-apple-darwin-c++ a.cpp
/usr/local/bin/arm-apple-darwin-ld: can't locate file for: -lstdc++
collect2: ld returned 1 exit status

Original comment by parad...@gmail.com on 9 Oct 2007 at 4:22

GoogleCodeExporter commented 9 years ago
regarding comment #6:
Looks like any exception raised will just terminate the app:

#include <stdio.h>
int main()
{
  try { throw 1; } catch(int i) {}
  printf("still alive...\n");
  return 0;
}

compile and run:

$ ./foo
terminate called after throwing an instance of 'int'
zsh: abort      ./foo

Original comment by j...@mac.com on 26 Oct 2007 at 2:53

GoogleCodeExporter commented 9 years ago
I want to know how to comiple c++ code with Objective C. I just wrote a short 
c++ 
code as followings:

test.h
=========================
class My
{
public:
void test_hello();
};

test.c
============================
#include "test.h"
void::test_hello()
{
}

In the helloApplication.m,I add   
#include "test.h"

And,I create a class in 

- (void) applicationDidFinishLaunching: (id) unused
{
    .....

 My my;
my.test_hello();

}

When compiling it,it gets error!

In file included from HelloApplication.m:26:
hello1.h:6: error: syntax error before 'my'
hello1.h:7: error: syntax error before '{' token
hello1.h:12: error: syntax error before '}' token

Original comment by cctvZh...@gmail.com on 24 Jan 2008 at 8:51

GoogleCodeExporter commented 9 years ago
I was able to solve this issue by linking to gcc_eh
nm /usr/local/lib/gcc/arm-apple-darwin/4.0.1/libgcc_eh.a for more details :)

Original comment by dlapr...@gmail.com on 21 Feb 2008 at 5:47

GoogleCodeExporter commented 9 years ago
 dlaprise is right. the problem is with linking libgcc_eh.a
To solve this, use:
LIBFLAGS += -L"/usr/local/arm-apple-darwin/lib"
instead of:
LIBFLAGS += -L/Developer/SDKs/MacOSX10.4u.sdk/usr/lib

I created the toolchain env using this tutorial:
http://ansani.it/2008/08/12/build-the-toolchain-for-iphone-20x-on-macosx-105x/ 

I attached a working application (an example I found on the net) and a makefile.

Goodluck,
Asaf Pinhassi

Original comment by pinha...@gmail.com on 22 Jan 2009 at 2:50

Attachments: