Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ exception and ld symbol warning

I am playing with creating exceptions in C++ and I have the following test code:

#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?");
}

I am compiling this with:

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

Upon compilation I am getting this ld warning:

ld: warning: direct access in _main to global weak symbol __ZN7MyerrorD1Ev means the weak symbol cannot be overridden at runtime. This was likely caused by different translation units being compiled with different visibility settings.

I do not get this warning if I use g++ instead of icpc.

I have not been able to understand what this means, and what is causing this warning to generate. The code runs as expected, however I'd like to undesratand what is happening.

like image 399
deepak Avatar asked Mar 14 '13 05:03

deepak


1 Answers

Try the following:

#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?");
}

Why do you need unused string errmsg?

like image 67
Milan Rusek Avatar answered Sep 30 '22 00:09

Milan Rusek