Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot compile with `-static` when using exceptions #32

Closed
dariost opened this issue Aug 6, 2017 · 13 comments

Comments

Projects
None yet
4 participants
@dariost
Copy link

commented Aug 6, 2017

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

@tpoechtrager

This comment has been minimized.

Copy link
Owner

commented Aug 6, 2017

Add -static-libstdc++.

@dariost

This comment has been minimized.

Copy link
Author

commented Aug 6, 2017

$ 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

@tpoechtrager

This comment has been minimized.

Copy link
Owner

commented Aug 6, 2017

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

@dariost

This comment has been minimized.

Copy link
Author

commented Aug 6, 2017

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.

@tpoechtrager

This comment has been minimized.

Copy link
Owner

commented Aug 7, 2017

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

@tpoechtrager

This comment has been minimized.

Copy link
Owner

commented Aug 7, 2017

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
@dariost

This comment has been minimized.

Copy link
Author

commented Aug 7, 2017

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.

@tpoechtrager

This comment has been minimized.

Copy link
Owner

commented Aug 8, 2017

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

@dariost

This comment has been minimized.

Copy link
Author

commented Aug 8, 2017

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

@fire

This comment has been minimized.

Copy link

commented Sep 16, 2017

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

@mati865

This comment has been minimized.

Copy link
Contributor

commented Sep 18, 2017

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.

@mati865

This comment has been minimized.

Copy link
Contributor

commented Nov 11, 2017

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

@dariost

This comment has been minimized.

Copy link
Author

commented May 3, 2018

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

@dariost dariost closed this May 3, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.