undefined reference to `__dynamic_cast' using

2020-04-30 16:02发布

问题:

I'm trying to compile a test case that employs dynamic_cast using libc++, which was compiled according to "Build on Linux using CMake and libsupc++".

#include <iostream>

struct A
{
    virtual void f(){ std::cout << "Virtual A" << std::endl; }
    virtual ~A(){}
};

struct B : public A
{
    void f() { std::cout << "Virtual B" << std::endl; }
    virtual ~B(){}
};

int main()
{
    A *a = new B;
    B *b = dynamic_cast<B *>(a);
    delete a;
}

I'm on ubuntu 13.04 and compiling with clang++ -std=c++11 -stdlib=libc++.

Why I'm getting undefined reference to '__dynamic_cast'? How to solve it?

EDIT

Looks like it has something to do with libc++abi since I'm seeing __dynamic_cast prototype in the spec. I expected the libsupc++ first build option to work without such issues... and trying to compile libc++abi on linux is another issue itself.

This seems related to Compiling with Clang using Libc++ undefined references.

If this is indeed the problem, is there a straightforward process to use libc++ on linux that can compile that short snippet, or is it just not yet supported?

回答1:

I would suggest using libcxxrt to replace the pretty Mac specific libc++abi (the website says only Darwin is supported). It provides a working implementation for BSD's Clang+libc++ setup, I believe it has been used with success on Linux as well.