Forum Stats

  • 3,741,291 Users
  • 2,248,405 Discussions
  • 7,861,725 Comments

Discussions

Sun Studio linking gcc libs: exceptions do not work

I need to build an application with Sun Studio. This application uses a shared library which can only be build with Gnu C++. The shared lib has a C Interface, so that the code is callable by the Sun Compiler (this is to avoid name mangling issues, see also this question).

Everything besides exception handling works fine. When an exception is thrown in the shared library, the program segfaults. This happens only when the main program is compiled using the Sun Studio Compiler. Compiling the minimal example below with the Gnu C++ compiler, the program works fine and the shared lib detects the exception.

Plan A: link dynamically Here is an illustration of the setup:

<span class="pln" style="background: transparent;"><span style="font-family: terminal, monaco;">GCC                         SOLARIS STUDIO</span><br/><span style="font-family: terminal, monaco;">                  shared</span><br/><span style="font-family: terminal, monaco;">clayer</span></span><span style="font-family: terminal, monaco;"><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">so         </span><span class="pun" style="background: transparent;"><-----</span><span class="pln" style="background: transparent;">    application<br/></span><span class="pun" style="background: transparent;">(</span><span class="kwd" style="color: #00008b; background: transparent;">no</span><span class="pln" style="background: transparent;"> exceptions</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;">             </span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">uses exceptions sol studio</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;"><br/>   </span><span class="pun" style="background: transparent;">|</span><span class="pln" style="background: transparent;"><br/>   </span><span class="pun" style="background: transparent;">|</span><span class="pln" style="background: transparent;"> </span><span class="kwd" style="color: #00008b; background: transparent;">use</span><span class="pln" style="background: transparent;"> flag </span><span class="pun" style="background: transparent;">-</span><span class="kwd" style="color: #00008b; background: transparent;">static</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">-</span><span class="kwd" style="color: #00008b; background: transparent;">static</span><span class="pun" style="background: transparent;">-</span><span class="pln" style="background: transparent;">libstdc</span><span class="pun" style="background: transparent;">++</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">-</span><span class="kwd" style="color: #00008b; background: transparent;">static</span><span class="pun" style="background: transparent;">-</span><span class="pln" style="background: transparent;">lib</span><span class="pun" style="background: transparent;">-</span><span class="pln" style="background: transparent;">gcc<br/>   v<br/>gcc_only_lib</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">so<br/>libstdc</span><span class="pun" style="background: transparent;">++.</span><span class="pln" style="background: transparent;">so<br/></span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">uses gcc exceptions</span></span><span style="background: transparent; font-family: terminal, monaco;">)</span>

Result: segmentation violation once an exception is thrown


---------------------------------------------------------------

Here is a minimal example to reproduce the problem:

exampleMain.cpp:

<span class="com" style="color: #808080; background: transparent;">#include</span><span class="pln" style="background: transparent;"> </span><span class="str" style="color: #800000; background: transparent;"><clayer.h></span><span class="pln" style="background: transparent;"> </span><span class="com" style="color: #808080; background: transparent;">#include</span><span class="pln" style="background: transparent;"> </span><span class="str" style="color: #800000; background: transparent;"><stdio.h></span><span class="pln" style="background: transparent;">  </span>
<span class="pln" style="background: transparent;"></span><span class="kwd" style="color: #00008b; background: transparent;">int</span><span class="pln" style="background: transparent;"> main</span><span class="pun" style="background: transparent;">(</span><span class="kwd" style="color: #00008b; background: transparent;">int</span><span class="pln" style="background: transparent;"> argc</span><span class="pun" style="background: transparent;">,</span><span class="pln" style="background: transparent;"> </span><span class="kwd" style="color: #00008b; background: transparent;">char</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">**</span><span class="pln" style="background: transparent;">argv</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">{</span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;">  </span><span class="kwd" style="color: #00008b; background: transparent;">if</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">(!</span><span class="pln" style="background: transparent;">clayerCall</span><span class="pun" style="background: transparent;">())</span><span class="pln" style="background: transparent;"> printf</span><span class="pun" style="background: transparent;">(</span><span class="str" style="color: #800000; background: transparent;">"got exception\n"</span><span class="pun" style="background: transparent;">);</span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;">  </span><span class="kwd" style="color: #00008b; background: transparent;">else</span><span class="pln" style="background: transparent;"> printf</span><span class="pun" style="background: transparent;">(</span><span class="str" style="color: #800000; background: transparent;">"OK\n"</span><span class="pun" style="background: transparent;">);</span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;"></span><span class="pun" style="background: transparent;">}</span>

shared lib header:

<span class="kwd" style="color: #00008b; background: transparent;">extern</span><span class="pln" style="background: transparent;"> </span><span class="str" style="color: #800000; background: transparent;">"C"</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">{</span><span class="pln" style="background: transparent;">  </span><span class="kwd" style="color: #00008b; background: transparent;">bool</span><span class="pln" style="background: transparent;"> clayerCall</span><span class="pun" style="background: transparent;">();</span><span class="pln" style="background: transparent;">  </span><span class="pun" style="background: transparent;">}</span><span class="pln" style="background: transparent;"> </span><span class="com" style="color: #808080; background: transparent;">// end extern "C" </span>

shared lib source:

<span class="com" style="color: #808080; background: transparent;">#include</span><span class="pln" style="background: transparent;"> </span><span class="str" style="color: #800000; background: transparent;">"clayer.h"</span><span class="pln" style="background: transparent;">  </span>
<span class="pln" style="background: transparent;"></span><span class="com" style="color: #808080; background: transparent;">#include</span><span class="pln" style="background: transparent;"> </span><span class="str" style="color: #800000; background: transparent;"><exception></span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;"></span><span class="com" style="color: #808080; background: transparent;">#include</span><span class="pln" style="background: transparent;"> </span><span class="str" style="color: #800000; background: transparent;"><stdexcept></span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;"></span><span class="com" style="color: #808080; background: transparent;">#include</span><span class="pln" style="background: transparent;"> </span><span class="str" style="color: #800000; background: transparent;"><stdio.h></span><span class="pln" style="background: transparent;">  </span>
<span class="pln" style="background: transparent;"></span><span class="kwd" style="color: #00008b; background: transparent;">extern</span><span class="pln" style="background: transparent;"> </span><span class="str" style="color: #800000; background: transparent;">"C"</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">{</span><span class="pln" style="background: transparent;">  </span>
<span class="pln" style="background: transparent;">     </span><span class="kwd" style="color: #00008b; background: transparent;">bool</span><span class="pln" style="background: transparent;"> clayerCall</span><span class="pun" style="background: transparent;">()</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">{</span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;">     </span><span class="kwd" style="color: #00008b; background: transparent;">try</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">{</span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;">          </span><span class="kwd" style="color: #00008b; background: transparent;">throw</span><span class="pln" style="background: transparent;"> std</span><span class="pun" style="background: transparent;">::</span><span class="pln" style="background: transparent;">runtime_error</span><span class="pun" style="background: transparent;">(</span><span class="str" style="color: #800000; background: transparent;">"hhh"</span><span class="pun" style="background: transparent;">);</span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;">          </span><span class="kwd" style="color: #00008b; background: transparent;">return</span><span class="pln" style="background: transparent;"> </span><span class="kwd" style="color: #00008b; background: transparent;">true</span><span class="pun" style="background: transparent;">;</span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;">     </span><span class="pun" style="background: transparent;">}</span><span class="pln" style="background: transparent;"> </span><span class="kwd" style="color: #00008b; background: transparent;">catch</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">std</span><span class="pun" style="background: transparent;">::</span><span class="pln" style="background: transparent;">exception </span><span class="pun" style="background: transparent;">&</span><span class="pln" style="background: transparent;">ex</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">{</span><span class="pln" style="background: transparent;"> </span><span class="kwd" style="color: #00008b; background: transparent;">return</span><span class="pln" style="background: transparent;"> </span><span class="kwd" style="color: #00008b; background: transparent;">false</span><span class="pun" style="background: transparent;">;</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">}</span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;"></span><span class="pun" style="background: transparent;">}</span><span class="pln" style="background: transparent;">  </span>
<span class="pln" style="background: transparent;"></span><span class="pun" style="background: transparent;">}</span><span class="pln" style="background: transparent;"> </span><span class="com" style="color: #808080; background: transparent;">// end extern c</span>

The cmake files look like this:

for the executable

<span class="pln" style="background: transparent;">project</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">exampleMain</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;">cmake_minimum_required</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">VERSION </span><span class="lit" style="color: #800000; background: transparent;">2.8</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;"></span><span class="kwd" style="color: #00008b; background: transparent;">set</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">CMAKE_BUILD_TYPE </span><span class="typ" style="color: #2b91af; background: transparent;">Debug</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;">add_definitions</span><span class="pun" style="background: transparent;">(-</span><span class="pln" style="background: transparent;">m64 </span><span class="pun" style="background: transparent;">-</span><span class="pln" style="background: transparent;">fPIC</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;">  </span>
<span class="pln" style="background: transparent;">include_directories</span><span class="pun" style="background: transparent;">(../oracle_example</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;">  </span>
<span class="pln" style="background: transparent;">link_directories </span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">../oracle_example</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;">  </span>
<span class="pln" style="background: transparent;">add_executable</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">example exampleMain</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">cpp</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;">target_link_libraries</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;"> example stdc</span><span class="pun" style="background: transparent;">++</span><span class="pln" style="background: transparent;"> clayer </span><span class="pun" style="background: transparent;">)</span>

for the library

<span class="pln" style="background: transparent;">project</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">clayer</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;">cmake_minimum_required</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">VERSION </span><span class="lit" style="color: #800000; background: transparent;">2.8</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;">cmake_policy</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">VERSION </span><span class="lit" style="color: #800000; background: transparent;">2.8</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;"> </span>
<span class="pln" style="background: transparent;"></span><span class="kwd" style="color: #00008b; background: transparent;">set</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">CMAKE_BUILD_TYPE </span><span class="typ" style="color: #2b91af; background: transparent;">Debug</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;">  </span>
<span class="pln" style="background: transparent;">add_library</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;"> clayer SHARED clayer</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">cpp </span><span class="pun" style="background: transparent;">)</span>
Tagged:

Answers

  • This question really belongs in the Solaris Studio C/C++/Fortran Compilers forum:

    https://community.oracle.com/community/server_%26_storage_systems/systems-development-and-management-tools/application_d…

    You don't show how you are building your program, and you don't say which version of Studio you are using, or the platform you are using. All of this data is important to understand your issue.

    You are mixing binary code created by the Studio C++ compiler with code built with g++. That will work only under these conditions:

    - You must use the -compat=g option with Studio C++ on every CC command, compiling and linking. (If  you are using Studio 12.4, you can use -std=c++03 or -std=c++11 instead.)

    - The g++ compiler must be compatible with the version expected by the Studio C++ compiler, which depends on the Studio version.

    - Only shared libraries (.so files) created by g++ can be linked, not .o or .a files.

    - The compiler (CC or g++)  that builds the main program must be used to link the final program.

    If you meet all these conditions and the code still fails, please show

    - the Studio version (run the command "CC -V"),

    - the g++ version (run "g++ -v"),

    - the platform (operating system and version, and whether it is Sparc or x86), and

    - show how you build the program.

This discussion has been closed.