c++异常和ld符号警告

C++ exception and ld symbol warning

本文关键字:符号 警告 ld 异常 c++      更新时间:2023-10-16

我正在玩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: warning:直接访问_main中的全局弱符号__ZN7MyerrorD1Ev意味着弱符号不能在运行时被覆盖。这可能是由于不同的翻译单位使用不同的可见性设置编译。

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

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

尝试如下:

#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?