Discussions
Categories
- 197.1K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.7K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 555 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.3K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 466 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
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>
Answers
-
This question really belongs in the Solaris Studio C/C++/Fortran Compilers forum:
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.