以下是我所做的代码片段,有些身体可以帮助我,我错误地编码了它:
#include<iostream>
using namespace std;
void modifyName(string &name)
{
size_t sep = string::npos;
sep = name.find_first_of(".");
if(sep != string::npos) { name[sep] = '\0'; }
}
int main()
{
string name("test.rtl");
string someName("test");
modifyName(name);
if( someName == name ) //Failing??
cout<<"MATCHED"<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这是我做的,我想优雅地处理这个异常:
code_snippet:my.cpp
#include<iostream>
extern "C" void some_func()
{
throw "(Exception Thrown by some_func!!)";
}
Run Code Online (Sandbox Code Playgroud)
code_snippet:exception.c
#include <stdio.h>
extern void some_func();
int so_main()
{
some_func();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
从上面的两个片段我使用以下命令创建了一个shared_object libexception.so:
g++ -c -fPIC src/my.cpp
gcc -c -ansi -fPIC src/exception.c
g++ -fPIC -shared -o libexception.so
Run Code Online (Sandbox Code Playgroud)
然后我从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 …Run Code Online (Sandbox Code Playgroud)