tpoechtrager / wclang

Cross compile source code easily for Windows with clang on Linux/Unix

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot compile with `-static` when using exceptions

dariost opened this issue · comments

When trying to compile this program:

#include <exception>
#include <iostream>

class V { virtual void foo(){} };

class A: virtual V {};

class B: public A {};

int main()
{
    B b;
    A& a = b;
    try
    {
        B& new_b = dynamic_cast<B&>(a);
    }
    catch(std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
    return 0;
}

with -static I get the following error:

$ x86_64-w64-mingw32-clang++ -o program.exe -pthread -static program.cpp 
/tmp/program-1176a8.o:(.text+0xa2): undefined reference to `__imp___dynamic_cast'
/tmp/program-1176a8.o:(.text+0xb7): undefined reference to `__imp___cxa_bad_cast'
/tmp/program-1176a8.o:(.text+0xf1): undefined reference to `__imp___cxa_begin_catch'
/tmp/program-1176a8.o:(.text+0x146): undefined reference to `__imp___cxa_end_catch'
/tmp/program-1176a8.o:(.text+0x161): undefined reference to `__imp___cxa_end_catch'
/tmp/program-1176a8.o:(.text[__clang_call_terminate]+0x7): undefined reference to `__imp___cxa_begin_catch'
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)

This is the complete error log with -v

Add -static-libstdc++.

$ x86_64-w64-mingw32-clang++ -o program.exe -pthread -static-libstdc++ -static program.cpp 
/tmp/program-b7fcbe.o:(.text+0xa2): undefined reference to `__imp___dynamic_cast'
/tmp/program-b7fcbe.o:(.text+0xb7): undefined reference to `__imp___cxa_bad_cast'
/tmp/program-b7fcbe.o:(.text+0xf1): undefined reference to `__imp___cxa_begin_catch'
/tmp/program-b7fcbe.o:(.text+0x146): undefined reference to `__imp___cxa_end_catch'
/tmp/program-b7fcbe.o:(.text+0x161): undefined reference to `__imp___cxa_end_catch'
/tmp/program-b7fcbe.o:(.text[__clang_call_terminate]+0x7): undefined reference to `__imp___cxa_begin_catch'
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)

I've tried all sorts of combinations of -static and -static-libstdc++, but this is always the result

Maybe there is some #define missing.

Compare the output of x86_64-w64-mingw32-clang++ -dM -E - < /dev/null and x86_64-w64-mingw32-g++ -dM -E - < /dev/null

I compared them but I'm not seeing anything related to exceptions (here are the definitions: clang.txt and gcc.txt).

I've also tried to compile with x86_64-w64-mingw32-clang++ redefining all its constant to match the ones of x86_64-w64-mingw32-g++ with this script, but it gives the same errors.

Out of ideas. Please file another bug report at https://bugs.llvm.org/describecomponents.cgi?product=clang.

I know now what's going on. Clang uses DLL linkage while GCC doesn't.

$ x86_64-w64-mingw32-clang++ program.cpp -c && x86_64-w64-mingw32-nm program.o | grep cxa
                U __imp___cxa_bad_cast
                U __imp___cxa_begin_catch
                U __imp___cxa_end_catch

$ x86_64-w64-mingw32-g++ program.cpp -c && x86_64-w64-mingw32-nm program.o | grep cxa
                U __cxa_bad_cast
                U __cxa_begin_catch
                U __cxa_end_catch

Do you still want me to open another bug report or are you working on this? Also, if I have to open it, please give me the details to attach to the report.

I am not an LLVM developer - you must report it to them. My previous comment should be enough to describe the problem.

Issue reported on the clang bugzilla: https://bugs.llvm.org/show_bug.cgi?id=34122

https://stackoverflow.com/questions/42545078/clang-version-5-and-lnk4217-warning

Add -Xclang -flto-visibility-public-std to your compiler options.
Like so:
clang++ -Xclang -flto-visibility-public-std -o test.exe test.cpp
From JayPhi

It is clang bug with possible fix here https://github.com/mati865/MINGW-packages/blob/954fa97c5349029b75b63c58ae81c62a39fa9658/mingw-w64-clang/0106-MinGW-use-flto-visibility-public-std-CC1-arg-to-get-.patch

If there will be no regressions I'll try to push it for next feature LLVM/clang release.

Fixed in clang trunk and 6.0 (coming next year) llvm-mirror/clang@be95e32

clang 6.0 has been released, and the issue has been fixed