尝试捕获在共享库中不起作用? [英] Try-catch doesn't work in shared library?

查看:86
本文介绍了尝试捕获在共享库中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这就像我的其他问题,但这是另一回事,即使它与之相关)

(This is like my other question but this one is for another thing, even if it's related)

我的项目遇到了很大的问题。我有一个处理XML并可以引发异常的库。并且,使用它来创建配置文件类显示了我的第一个错误:库中根本没有处理异常,并且没有任何异常。

I've got a big issue in my project. I've got a library which handle XML and can throw exception. And, using it for creating a configuration file class show my first bug : exceptions aren't handled in the library, at all, and with every exception.

在库中我写了:

try {
    throw std::exception();
}
catch (...)
{
    printf("caught\n");
}

但是,异常未处理,请调用 std :: terminate 立即:

But, the exception isn't handled and call std::terminate immediately :

terminate called after throwing an instance of 'std::exception'
  what():  std::exception

编译标志是最简单的一个: -fPIC -std = c ++ 11 -g -Wall 用于库,而 -std = c ++ 11 -g -Wall 用于可执行文件(以及库和变体版本定义)。另外,我在Linux(准确地说是Linux Mint)下使用的是G ++ 5.4.0。

The compilation flags are the simplest one : -fPIC -std=c++11 -g -Wall for the library, and -std=c++11 -g -Wall for the executable (plus the libraries and variant build defines). Also, I'm using G++ 5.4.0, under Linux (Linux Mint to be precise).

这是我的主要

#include "ns/core/C_Configuration.hpp"

#include <iostream>

using namespace std;
using namespace ns;


int
main (int argc, char** argv)
{
    try {
        C_Configuration c ("test.xml");
        c.load ("test.xml");
    } catch (const std::exception& ex) {
        cout << ex.what () << endl;
    } catch (...) {
        cout << "Caught." << endl;
    }


    return 0;
}

C_Configuration.hpp

#include <string>
#include <exception>


namespace ns
{


    class C_Configuration
    {

    public:

        C_Configuration (std::string);


        bool load (std::string file);

    };


} // namespace ns

C_Configuration.cpp

#include "ns/core/C_Configuration.hpp"

#include <cstdio>

using namespace std;


namespace ns
{



C_Configuration::C_Configuration (string)
{ }



bool
C_Configuration::load (string file)
{

    try {
        throw exception();
    } catch (const exception& ex) {
        printf ("In C_Configuration : %s\n", ex.what ());
    } catch (...) {
        printf ("In C_Configuration : caught\n");
    }


    return true;
}


} // namespace ns

Buid命令:

g++ -m64 -g -shared -fPIC -std=c++11 -o libns_framework.so C_Configuration.cpp
g++ -m64 -g -L. -o exe main.cpp -lns_framework 

注意: ,但它按预期方式工作,但异常是在库中捕获的,而不是在我的主项目中。如果您想进行更多调查,可以在此处查看我的项目代码。

Note : I give this example, but it works as expected, the exception is caught in the library, not like in my main project. If you want to investigate more, you can check my project code here.

问题是在什么时候:


  • try-catch块位于内部 库;

  • try-catch块位于库外部中;

  • The try-catch block is inside the library ;
  • The try-catch block is outside the library ;

在任何情况下,都会在库中 中引发异常。但是,抛出在外部的异常捕获在可执行代码中:

In any case, the exception is thrown inside the library. But, exception thrown outside are caught in the executable code :

int
main (int argc, char** argv)
{
    try {
        throw 1;
    } catch (...) {
        cout << "Caught" << endl;
    }

    // Useless code

    return 0;
}

这段代码只写了抓到输出中。

This code just write Caught in the output.

所以,我的问题很简单:库中是否不处理C ++异常,还是只是忘记了编译标志?我需要在可执行代码中说,例外情况很好。

So, my question is simple : Is C++ exception not handled within libraries, or I just forgot a compilation flag ? I need to say in the executable code, the exceptions work fine.

感谢您的帮助。

编辑:哦,天哪,我的天哪。问题解决了。在我的构建配置的最深层部分中,用 ld 代替了 g ++ 。现在,异常正常。谢谢您的帮助。

EDIT : Oh god, my bad. Problem solved. Into the deepest part of my build configuration, an ld took the place of g++. Now the exception is working fine. Thanks for you help.

推荐答案

简单。切勿在C ++中使用 ld 。我将项目中的所有 ld 命令更改为 g ++ ,但似乎我忘记了此库。

Simple. Never use ld with C++. I changed all ld commands in my project to g++ but seems I forgot for this library.

简而言之,我使用的是 ld 来构建我的库,但是使用的是 g ++ 主要可执行文件。因此,异常在可执行文件中有效,但在库中无效,因为 ld 不包括处理异常系统的C ++库。

In short, I was using ld for building my library but g++ for the main executable. So the exceptions worked in the executable but not in the library because ld does not includes the C++ libraries which handle the exception system.

这篇关于尝试捕获在共享库中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆