Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28486 Discussions

C++ exceptions broken when linking with ifort (Mac OSX)

Jakob_Schou_J_
Beginner
1,185 Views

I am having troubles using c++ exceptions in a mixed fortran / c++ program. I have written a minimal program that illustrates the problem. The program consist of one fortran file and one c++ file. The fortran code call a function in the c++ file. This c++ function throws and catches an exception.

We have a number of mac computers and when compiling on some computers the program works while on others (most) it doesn't. The exception does not get caught and instead the program terminates with:

libc++abi.dylib: terminating with uncaught exception of type MyExc
forrtl: error (76): Abort trap signal
Image              PC                Routine            Line        Source             
libsystem_kernel.  00007FFF8B00A867  Unknown               Unknown  Unknown

Here is the c++ code:

#include <stdio.h>

class MyExc {};

extern "C" void catch_exc_() {
  printf("catch exc\n");

  try {
    throw MyExc();
  }
  catch( ... ) {
    printf("got it\n");
  }

}

Here is the fortran code:

program test_fortran
print *, "run fortran test"
call catch_exc
end

Here is the build command lines:

ifort -fexceptions -c test_fortran.f90
clang -fexceptions -c catch_exc.cpp -o catch_exc.o
ifort catch_exc.o test_fortran.o -lc++ -fexceptions -o test_fortran

To me this seems like a compiler/tool chain problem. But maybe I am doing something wrong. Any ideas?

Thanks

0 Kudos
1 Solution
pbkenned1
Employee
1,185 Views

Yes, this works just fine with icc & ifort 15.0 on OSX 10.9.5

$ icc -V
Intel(R) C Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 15.0.0.077 Build 20141101
Copyright (C) 1985-2014 Intel Corporation.  All rights reserved.

$ ifort -V
Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 15.0.0.071 Build 20140709
Copyright (C) 1985-2014 Intel Corporation.  All rights reserved.

$ ifort -fexceptions -c test_fortran.f90
$ icc -fexceptions -c catch_exc.cpp
$ ifort catch_exc.o test_fortran.o -lc++  -o test_fortran
$ ./test_fortran
 run fortran test
catch exc
got it
$

Patrick

 

View solution in original post

0 Kudos
6 Replies
FortranFan
Honored Contributor II
1,185 Views

See this discussion: https://software.intel.com/en-us/forums/topic/506739

 

0 Kudos
Jakob_Schou_J_
Beginner
1,185 Views

Please note that I am not trying to use exceptions in fortran code. The exception is thrown and caught (or should be caught) in c++ code.

0 Kudos
pbkenned1
Employee
1,185 Views

I don't think you did anything wrong.  It just doesn't work with the exception handling table generated by the Mac clang compiler.

Just as a sanity/logic check, your examples work fine with icc/ifort on Linux:

[U535143]$ icc -V
Intel(R) C Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 15.0.1.133 Build 20141023
Copyright (C) 1985-2014 Intel Corporation.  All rights reserved.

[U535143]$ icc -fexceptions -c catch_exc.cpp
[U535143]$ ifort -fexceptions -c test_fortran.f90
[U535143]$ ifort test_fortran.o catch_exc.o -o test_fortran -lstdc++        

[U535143]$ ./test_fortran
 run fortran test
catch exc
got it
[U535143]$

Patrick

0 Kudos
Jakob_Schou_J_
Beginner
1,185 Views

Would it work on mac if I used icc instead of clang?

 

0 Kudos
pbkenned1
Employee
1,185 Views

I think it should work with icc on MAC, if not, I'd consider it a bug.  Unfortunately I don't have a MAC system to test on, but I'll ask a colleague who does to check it out.

Patrick

0 Kudos
pbkenned1
Employee
1,186 Views

Yes, this works just fine with icc & ifort 15.0 on OSX 10.9.5

$ icc -V
Intel(R) C Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 15.0.0.077 Build 20141101
Copyright (C) 1985-2014 Intel Corporation.  All rights reserved.

$ ifort -V
Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 15.0.0.071 Build 20140709
Copyright (C) 1985-2014 Intel Corporation.  All rights reserved.

$ ifort -fexceptions -c test_fortran.f90
$ icc -fexceptions -c catch_exc.cpp
$ ifort catch_exc.o test_fortran.o -lc++  -o test_fortran
$ ./test_fortran
 run fortran test
catch exc
got it
$

Patrick

 

0 Kudos
Reply