microsoft / cppwinrt

C++/WinRT
MIT License
1.64k stars 238 forks source link

Bug: including winrt/base.h causes linker errors when compiling with /std:c++20 and linking to a library compiled with /std:c++17 #1281

Closed equeim closed 1 year ago

equeim commented 1 year ago

Version

v2.0.220110.5

Summary

My application is compiled with /std:c++20. It links to a library that is compiled with /std:c++17. Both the library and the application include <winrt/base.h> which includes either <experimental/coroutine> or <coroutine> depending on /std switch. Neither library nor application actually use coroutines but it still causes linker errors. Is there any way I can prevent winrt from including coroutine header so that it doesn't cause this conflict? (BTW it's kind of a dick move from a library to use experimental stuff which is not ABI stable, and do so by default).

Reproducible example

#include <winrt/base.h>

Expected behavior

No linker errors.

Actual behavior

error LNK2038: mismatch detected for '_COROUTINE_ABI': value '1' doesn't match value '2'

Additional comments

No response

sylveon commented 1 year ago

Pass /await:strict to the compiler to enable the C++20 coroutines ABI in C++17

equeim commented 1 year ago

That would mean using overlay triplets with vcpkg and I would rather avoid it unless it is absolutely necessary. I guess I can "fix" this with _ALLOW_COROUTINE_ABI_MISMATCH although it does sound dangerous.

sylveon commented 1 year ago

You can also do the reverse and pass /await to use the C++17 coroutines ABI in C++20 I think.

kennykerr commented 1 year ago

C++/WinRT adopted coroutines very early on, but as @sylveon points out you can compile it today with C++20 and avoid the older headers.