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:
GCC SOLARIS STUDIO
shared
clayer.so <----- application
(no exceptions) (uses exceptions sol studio)
|
| use flag -static -static-libstdc++ -static-lib-gcc
v
gcc_only_lib.so
libstdc++.so
(uses gcc exceptions)
Result: segmentation violation once an exception is thrown
---------------------------------------------------------------
Here is a minimal example to reproduce the problem:
exampleMain.cpp:
#include <clayer.h> #include <stdio.h>
int main(int argc, char **argv) {
if (!clayerCall()) printf("got exception\n");
else printf("OK\n");
}
shared lib header:
extern "C" { bool clayerCall(); } // end extern "C"
shared lib source:
#include "clayer.h"
#include <exception>
#include <stdexcept>
#include <stdio.h>
extern "C" {
bool clayerCall() {
try {
throw std::runtime_error("hhh");
return true;
} catch (std::exception &ex) { return false; }
}
} // end extern c
The cmake files look like this:
for the executable
project(exampleMain)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_BUILD_TYPE Debug)
add_definitions(-m64 -fPIC)
include_directories(../oracle_example)
link_directories ( ../oracle_example)
add_executable(example exampleMain.cpp)
target_link_libraries( example stdc++ clayer )
for the library
project(clayer)
cmake_minimum_required(VERSION 2.8)
cmake_policy(VERSION 2.8)
set(CMAKE_BUILD_TYPE Debug)
add_library( clayer SHARED clayer.cpp )