これが私がしたことです、私はこの例外を優雅に処理したいと思います:
code_snippet:my.cpp
#include<iostream>
extern "C" void some_func()
{
throw "(Exception Thrown by some_func!!)";
}
code_snippet:exception.c
#include <stdio.h>
extern void some_func();
int so_main()
{
some_func();
return 0;
}
上記の2つのスニペットから、次のコマンドを使用してshared_objectlibexception.soを作成しました。
g++ -c -fPIC src/my.cpp
gcc -c -ansi -fPIC src/exception.c
g++ -fPIC -shared -o libexception.so
次に、main.cpp code_snippet:main.cppから関数so_mainを呼び出しました。
#include<iostream>
#include <dlfcn.h>
using namespace std;
extern "C" void some_func();
int main()
{
int (*fptr)() = 0;
void *lib = dlopen("./libexception.so", RTLD_LAZY);
if (lib) {
*(void **)(&fptr) = dlsym(lib, "so_main");
try{
if(fptr) (*fptr)(); <-- problem lies here
//some_func(); <-- calling this directly won't crash
}
catch (char const* exception) {
cout<<"Caught exception :"<<exception<<endl;
}
}
return 0;
}
最終コマンド:g ++ -g -ldl -o main.exe src / main.cpp -L lib / -lexception
main.exeを実行するとクラッシュします。誰かが問題の場所とこのクラッシュの発生を回避する方法を検出するのを手伝ってもらえますか(一部の.SOがextern c ++関数を呼び出すアーキテクチャがすでにあるため、変更できません。main.cppでのみ処理します) )。