【问题标题】:C++ exception and ld symbol warningC++ 异常和 ld 符号警告
【发布时间】:2013-03-02 09:17:27
【问题描述】:

我正在使用 C++ 创建异常,我有以下测试代码:

#include <iostream>
#include <stdexcept>
#include <new>
using namespace std;

class Myerror : public runtime_error {
    private: 
        string errmsg;
    public:
        Myerror(const string &message): runtime_error(message) { }
};

int main(int argc, char *argv[]) {
    throw Myerror("wassup?");
}

我正在编译这个:

icpc -std=c++11 -O3 -m64

在编译时我收到了这个 ld 警告:

ld:警告:在 _main 中直接访问全局弱符号 __ZN7MyerrorD1Ev 表示弱符号在运行时不能被覆盖。这可能是由于不同的翻译单元正在 使用不同的可见性设置编译。

如果我使用 g++ 而不是 icpc,我不会收到此警告。

我无法理解这意味着什么,以及导致此警告产生的原因。代码按预期运行,但是我想取消正在发生的事情。

【问题讨论】:

  • #include &lt;string&gt;了吗?
  • 你试过用-fvisibility=hidden编译吗?
  • @jotep 包括字符串并没有改变任何东西。
  • @user2155932 谢谢,有帮助!命令做了什么?为什么符号可见性仅在上面的示例中是一个问题,而对于我编译的任何其他代码都没有?
  • @deepak 好吧,看起来就像它所说的那样:正在使用不同的可见性设置编译不同的翻译单元。很可能,您的某些库(可能是 libstdc++)已使用 -fvisibility=hidden 编译。 g++ 默认以隐藏可见性编译。

标签: c++ exception ld icc


【解决方案1】:

尝试以下方法:

#include <iostream>
#include <stdexcept>
#include <new>
using namespace std;

class Myerror : public runtime_error {
    public:
        Myerror(const string &message) throw(): runtime_error(message) { }
        virtual ~Myerror() throw() {}
};

int main(int argc, char *argv[]) {
    throw Myerror("wassup?");
}

为什么需要未使用的字符串 errmsg?

【讨论】:

    猜你喜欢
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 2021-06-11
    • 1970-01-01
    • 2021-11-18
    • 2022-11-11
    • 2023-03-17
    相关资源
    最近更新 更多