English 中文(简体)
不要把静态连接的C++图书馆排除在外?
原标题:Unsafe to throw exceptions from statically linked C++ libraries?

我听说,在C++图书馆提供例外情形可能非常危险,特别是对于DLs而言,特别是如果与不同的汇编者一起汇编电话码和图书馆的话。 是否有这方面的真相? 只要我坚持固定的图书馆,这是否安全? 请注意,我只谈论图书馆内的例外情形的内部使用问题,我想把它们深入到打电话的法典中:

仅澄清: Say I got a 汇编成文的静态图书馆,其中界定了Foo类:

class Foo
{
public:
    // Constructor
    Foo()
    {
        /* ... Do stuff ... */        
        if (stuffwentwrong)
            throw(123); // We throw an integer error code (to make it simple) 
    }
};

有些人则这样说:

try 
{
    Foo foo_object;
}
catch (int i)
{
    std::cout << "Oh bum. Code: " << i; 
}

这是否安全?

最佳回答

尤其是,如果用不同的汇编者汇编电话码和图书馆,

You generally can t mix different C++ compilers that do not have compatible ABI. So, for example, you can t throw exception from library compiled with MSVC and try to catch it with GCC.

但否则,你一般没有问题。

注:

MSVC有几种不兼容的例外情况模式,没有混合。

问题回答

关于海合会,至少有一种情况是,从海合会生成的共享图书馆收集例外情形可能会有问题,即,在“<条码>上<>第>条形状/可操作的类型”从共享图书馆出口时,“<条码>隐藏的到违约时。 GCC Visibility Wiki 网页详细描述了这一问题以及如何防止这一问题。

我不相信Windows DLs有类似问题,但似乎有可能。

如果你想放弃分配的例外情况,如果DLL的消费者试图免除分配的豁免,你就将失败。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签