ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.78k stars 2.54k forks source link

MSVC target on Windows does not work with any C++ code #18685

Open vdemichev opened 9 months ago

vdemichev commented 9 months ago

Zig Version

0.11.0, x64 Windows; same problem with the latest master

Steps to Reproduce and Observed Behavior

Create any cpp file, try to compile with -target native-native-msvc - does not work. Target gnu works fine. Compiling as C code works fine.

Output:

PS C:\Temp\Test> gc test.cpp
#include <iostream>

int main() {
        std::cout << "Hello World!\n";
        return 0;
}
PS C:\Temp\Test> zig version
0.11.0
PS C:\Temp\Test> zig c++ C:/temp/test/test.cpp -target native-native-msvc
Compile C Objects [2/1] zig: zig: zig: warning: warning: warning: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]

zig: zig: zig: warning: warning: warning: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]

zig: warning: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]
zigzig: : warning: warning: zigargument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]: zig: zig: warning: zig:
warning: zigargument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]warning: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]zig: warning: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]

: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]zigwarning: zig: zig: zig: :

zig: warning: warning: warning: zig: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]warning: zigargument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]zigargument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]warning: zig: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]warning:
:
: warning:
argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]warning:

zig: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]warning: warning: zig:

argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]zig:
argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]
argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]zig: warning:
zig: zig: warning: warning: warning: zig: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]

warning: warning: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]warning:
zig: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]

argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]
argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]
warning:

argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]
In file included from C:\Zig\lib\libcxxabi\src/cxa_aux_runtime.cpp:14:
In file included from C:\Zig\lib\libcxx\include/typeinfo:73:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_typeinfo.h:137:13: error: target of using declaration conflicts with declaration already in scope
    using ::type_info;
            ^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_typeinfo.h:73:7: note: target of using declaration
class type_info
      ^
C:\Zig\lib\libcxxabi\include/cxxabi.h:30:27: note: conflicting declaration
class _LIBCXXABI_TYPE_VIS type_info; // forward declaration
                          ^
1 error generated.
C:\Zig\lib\libcxxabi\src/stdlib_typeinfo.cpp:16:12: error: cannot define or redeclare '~type_info' here because namespace 'std' does not enclose namespace 'type_info'
type_info::~type_info()
~~~~~~~~~~~^
C:\Zig\lib\libcxxabi\src/stdlib_typeinfo.cpp:22:11: error: redefinition of 'bad_cast'
bad_cast::bad_cast() noexcept
          ^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_typeinfo.h:151:5: note: previous definition is here
    bad_cast() noexcept
    ^
C:\Zig\lib\libcxxabi\src/stdlib_exception.cpp:17:12C:\Zig\lib\libcxxabi\src/stdlib_typeinfo.cpp:26:11: : warning: error: function previously declared with an implicit exception specification redeclared with an explicit exception specification [-Wimplicit-exception-spec-mismatch]redefinition of
'~exception'bad_cast::~bad_cast() noexcept

exception::~exception() noexcept
          ^
           ^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_typeinfo.h:146:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_exception.h7: C:\Zig\lib\libcxxabi\src/stdlib_new_delete.cpp:34:36: :88:13: error: note: previous declaration is hereno member named 'get_new_handler' in namespace 'std'

class bad_cast
        std::new_handler nh = std::get_new_handler();note:

      ^
previous definition is hereC:\Zig\lib\libcxxabi\src/stdlib_typeinfo.cpp:26:11:
                              ~~~~~^
    virtual ~exception() noexcept
PS C:\Temp\Test> error:             ^
definition of implicitly declared destructorC:\Zig\lib\libcxxabi\src/stdlib_new_delete.cppC:\Zig\lib\libcxxabi\src/stdlib_exception.cpp:21:24: :154:36:
warning: bad_cast::~bad_cast() noexcept
error: In file included from C:\Zig\lib\libcxxabi\src/cxa_handlers.cpp:19:
In file included from C:\Zig\lib\libcxxabi\src/private_typeinfo.h          ^
exception:14:
no specification in declaration does not match previous declaration [-Wmicrosoft-exception-spec]In file included from C:\Zig\lib\libcxx\include/typeinfo:73:
 member named 'get_new_handler' in namespace 'std'C:\Zig\lib\libcxxabi\src/stdlib_typeinfo.cpp:31:11:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_typeinfo.h
        std::new_handler nh = std::get_new_handler();:error: const char* exception::what() const noexcept
137:13
out-of-line definition of 'what' does not match any declaration in                               ~~~~~^
:                        ^
'std::bad_cast'error:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_exception.hbad_cast::what() const noexcept
:target293:36:           ^~~~ error
sofnote:  generated.
 using declaration conflictspreviousC:\Zig\lib\libcxxabi\src/stdlib_typeinfo.cpp with declaration already:  in scopedeclaration is here
38
    using ::type_info;:13:    _NODISCARD virtual char const* what() const

                                   ^
            ^
error: C:\Zig\lib\libcxxabi\src/stdlib_exception.cpp:21:24: redefinition of 'bad_typeid'C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_typeinfo.h:73:7:
error: bad_typeid::bad_typeid() noexcept
note:             ^
targetredefinition  ofof 'what'C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_typeinfo.h using declaration
:174:5: const char* exception::what() const noexcept

class type_info
                       ^
note:       ^
previous definition is hereC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_exception.h:93:36:
C:\Zig\lib\libcxxabi\include/cxxabi.h    bad_typeid() noexcept
:30:27:     ^
note: note: previous definition is hereconflicting declaration
C:\Zig\lib\libcxxabi\src/stdlib_typeinfo.cpp:42:13:
    _NODISCARD virtual char const* what() constclass _LIBCXXABI_TYPE_VIS type_info; // forward declaration

warning:                           ^
                                   ^
function previously declared with anC:\Zig\lib\libcxxabi\src/stdlib_exception.cppIn file included from C:\Zig\lib\libcxxabi\src/cxa_handlers.cpp:19:
 implicit exception specification redeclared with an explicit exception specification [-Wimplicit-exception-spec-mismatch]C:\Zig\lib\libcxxabi\src/private_typeinfo.h:19:53: :28:16: error:
bad_typeid::~bad_typeid() noexcept
warning: base class             ^
function previously declared C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_typeinfo.h:with an implicit exception specification redeclared with an explicit exception specification [-Wimplicit-exception-spec-mismatch]has169 incomplete
:7bad_exception::~bad_exception() noexcepttype:

class _LIBCXXABI_TYPE_VIS __shim_type_info : public std::type_info {
               ^
note:                                              ~~~~~~~^~~~~~~~~~~~~~
previous declaration is hereC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_exception.h:103:7:
C:\Zig\lib\libcxxabi\include/cxxabi.hclass bad_typeid
:30:27:       ^
note: note: previous declaration is hereC:\Zig\lib\libcxxabi\src/stdlib_typeinfo.cpp:42:13:
forward declaration of 'std::type_info'class bad_exception
error:
      ^
class _LIBCXXABI_TYPE_VIS type_info; // forward declaration
definition of                           ^C:\Zig\lib\libcxxabi\src/stdlib_exception.cpp:28:16: implicitly declared destructor
error:
bad_typeid::~bad_typeid() noexcept
definition of implicitly declared destructor            ^

bad_exception::~bad_exception() noexcept
C:\Zig\lib\libcxxabi\src/stdlib_typeinfo.cpp:47               ^
:13: C:\Zig\lib\libcxxabi\src/stdlib_exception.cpp:32:error: 28: out-of-line definition of 'what' does not match any declaration in 'std::bad_typeid'error:
bad_typeid::what() const noexcept
out-of-line definition of 'what' does not match any declaration in 'std::bad_exception'            ^~~~

const char* bad_exception::what() const noexcept
2 warnings and 7 errors generated.
                           ^~~~
C:\Zig\lib\libcxxabi\src/stdlib_exception.cpp:40:12: error: redefinition of 'bad_alloc'
bad_alloc::bad_alloc() noexcept
           ^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_exception.h:119:5: note: previous definition is here
    bad_alloc() noexcept
    ^
C:\Zig\lib\libcxxabi\src/stdlib_exception.cpp:44:12: warning: function previously declared with an implicit exception specification redeclared with an explicit exception specification [-Wimplicit-exception-spec-mismatch]
bad_alloc::~bad_alloc() noexcept
           ^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_exception.h:114:7: note: previous declaration is here
class bad_alloc
      ^
C:\Zig\lib\libcxxabi\src/stdlib_exception.cpp:44:12: error: definition of implicitly declared destructor
bad_alloc::~bad_alloc() noexcept
           ^
C:\Zig\lib\libcxxabi\src/stdlib_exception.cpp:49:12: error: out-of-line definition of 'what' does not match any declaration in 'std::bad_alloc'
bad_alloc::what() const noexcept
           ^~~~
In file included from C:\Zig\lib\libcxxabi\src/cxa_exception.cpp:20:
In file included from C:\Zig\lib\libcxxabi\src/stdlib_exception.cppC:\Zig\lib\libcxx\src/include/atomic_support.h:13:
In file included from :C:\Zig\lib\libcxx\include/memory:898:
56:23: In file included from C:\Zig\lib\libcxx\include/__memory/shared_ptr.h:40:
In file included from error: C:\Zig\lib\libcxx\include/typeinfo:In file included from C:\Zig\lib\libcxxabi\src/cxa_guard.cpp:15:
In file included from redefinition of 'bad_array_new_length'73:

C:\Zig\lib\libcxxabi\src/cxa_guard_impl.h:48:
In file included from bad_array_new_length::bad_array_new_length() noexcept
C:\Zig\lib\libcxx\src/include/atomic_support.hC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_typeinfo.h                      ^
:13:
:137:13: In file included from C:\Zig\lib\libcxx\include/memory:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_exception.h:error: 898:
In file included from C:\Zig\lib\libcxx\include/__memory/shared_ptr.h:40:
In file included from C:\Zig\lib\libcxx\include/typeinfo:73:
target139 of using declaration conflicts with declaration already in scope:5: C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_typeinfo.h
:137:13: note:     using ::type_info;
previouserror:  definition is heretarget
 of using declaration conflicts with declaration    bad_array_new_length() noexcept
             ^
already in scope    ^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_typeinfo.h
:73:7:     using ::type_info;C:\Zig\lib\libcxxabi\src/stdlib_exception.cpp:56:23: note:
target             ^
of using declarationerror: C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_typeinfo.h
:73:7: class type_info
constructor for 'std::bad_array_new_length' must explicitly initialize the base class 'bad_alloc' which does not have a default constructornote:
      ^
bad_array_new_length::bad_array_new_length() noexcepttarget of using declarationC:\Zig\lib\libcxxabi\include/cxxabi.h
:30:27:                       ^

class type_info
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_exception.h:114:7: note:       ^
note: conflicting declaration'std::bad_alloc' declared here
C:\Zig\lib\libcxxabi\include/cxxabi.h
class _LIBCXXABI_TYPE_VIS type_info; // forward declaration
class bad_alloc
:30:27:       ^
                          ^
note: conflicting declarationC:\Zig\lib\libcxxabi\src/stdlib_exception.cpp:60:23:
class _LIBCXXABI_TYPE_VIS type_info; // forward declaration
warning:                           ^
function previously declared with an implicit exception specification redeclared with an explicit exception specification [-Wimplicit-exception-spec-mismatch]
bad_array_new_length::~bad_array_new_length() noexcept
                      ^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include/vcruntime_exception.h:134:7: note: previous declaration is here
class bad_array_new_length
      ^
C:\Zig\lib\libcxxabi\src/stdlib_exception.cpp:60:23: error: definition of implicitly declared destructor
bad_array_new_length::~bad_array_new_length() noexcept
                      ^
C:\Zig\lib\libcxxabi\src/stdlib_exception.cpp:65:23: error: out-of-line definition of 'what' does not match any declaration in 'std::bad_array_new_length'
bad_array_new_length::what() const noexcept
                      ^~~~
4 warnings and 11 errors generated.
In file included from C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:15:
C:\Zig\lib\libcxx\src/include/refstring.h:83:1: error: use of undeclared identifier '__libcpp_refstring'
__libcpp_refstring::__libcpp_refstring(const char* msg) {
^
C:\Zig\lib\libcxx\src/include/refstring.h:95:1: error: use of undeclared identifier '__libcpp_refstring'
__libcpp_refstring::__libcpp_refstring(const __libcpp_refstring &s) noexcept
^
C:\Zig\lib\libcxx\src/include/refstring.h:95:46: error: unknown type name '__libcpp_refstring'
__libcpp_refstring::__libcpp_refstring(const __libcpp_refstring &s) noexcept
                                             ^
C:\Zig\lib\libcxx\src/include/refstring.h:103:1: error: unknown type name '__libcpp_refstring'
__libcpp_refstring& __libcpp_refstring::operator=(__libcpp_refstring const& s) noexcept {
^
C:\Zig\lib\libcxx\src/include/refstring.h:103:21: error: use of undeclared identifier '__libcpp_refstring'
__libcpp_refstring& __libcpp_refstring::operator=(__libcpp_refstring const& s) noexcept {
                    ^
C:\Zig\lib\libcxx\src/include/refstring.h:103:51: error: unknown type name '__libcpp_refstring'
__libcpp_refstring& __libcpp_refstring::operator=(__libcpp_refstring const& s) noexcept {
                                                  ^
C:\Zig\lib\libcxx\src/include/refstring.h:120:1: error: use of undeclared identifier '__libcpp_refstring'
__libcpp_refstring::~__libcpp_refstring() {
^
C:\Zig\lib\libcxx\src/include/refstring.h:130:6: error: use of undeclared identifier '__libcpp_refstring'
bool __libcpp_refstring::__uses_refcount() const {
     ^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:17:27: error: no member named '__libcpp_refstring' in namespace 'std'
static_assert(sizeof(std::__libcpp_refstring) == sizeof(const char *), "");
                     ~~~~~^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:22:14: warning: function previously declared with an implicit exception specification redeclared with an explicit exception specification [-Wimplicit-exception-spec-mismatch]
logic_error::~logic_error() noexcept {}
             ^
C:\Zig\lib\libcxx\include/stdexcept:77:29: note: previous declaration is here
class _LIBCPP_EXCEPTION_ABI logic_error
                            ^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:22:14: error: definition of implicitly declared destructor
logic_error::~logic_error() noexcept {}
             ^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:25:14: error: out-of-line definition of 'what' does not match any declaration in 'std::logic_error'
logic_error::what() const noexcept
             ^~~~
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:27:12: error: use of undeclared identifier '__imp_'
    return __imp_.c_str();
           ^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:30:16: warning: function previously declared with an implicit exception specification redeclared with an explicit exception specification [-Wimplicit-exception-spec-mismatch]
runtime_error::~runtime_error() noexcept {}
               ^
C:\Zig\lib\libcxx\include/stdexcept:100:29: note: previous declaration is here
class _LIBCPP_EXCEPTION_ABI runtime_error
                            ^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:30:16: error: definition of implicitly declared destructor
runtime_error::~runtime_error() noexcept {}
1 error generated.
               ^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:33:16: error: out-of-line definition of 'what' does not match any declaration in 'std::runtime_error'
runtime_error::what() const noexcept
               ^~~~
1 error generated.
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:352 error:12: s generated.
error: use of undeclared identifier '__imp_'
    return __imp_.c_str();
           ^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:38:15: warning: function previously declared with an implicit exception specification redeclared with an explicit exception specification [-Wimplicit-exception-spec-mismatch]
domain_error::~domain_error() noexcept {}
              ^
C:\Zig\lib\libcxx\include/stdexcept:123:29: note: previous declaration is here
class _LIBCPP_EXCEPTION_ABI domain_error
                            ^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:38:15: error: definition of implicitly declared destructor
domain_error::~domain_error() noexcept {}
              ^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:39:19: warning: function previously declared with an implicit exception specification redeclared with an explicit exception specification [-Wimplicit-exception-spec-mismatch]
invalid_argument::~invalid_argument() noexcept {}
                  ^
C:\Zig\lib\libcxx\include/stdexcept:136:29: note: previous declaration is here
class _LIBCPP_EXCEPTION_ABI invalid_argument
                            ^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:39:19: error: definition of implicitly declared destructor
invalid_argument::~invalid_argument() noexcept {}
                  ^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:40:15: warning: function previously declared with an implicit exception specification redeclared with an explicit exception specification [-Wimplicit-exception-spec-mismatch]
length_error::~length_error() noexcept {}
              ^
C:\Zig\lib\libcxx\include/stdexcept:149:29: note: previous declaration is here
class _LIBCPP_EXCEPTION_ABI length_error
                            ^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:40:15: error: definition of implicitly declared destructor
length_error::~length_error() noexcept {}
              ^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:41:15: warning: function previously declared with an implicit exception specification redeclared with an explicit exception specification [-Wimplicit-exception-spec-mismatch]
out_of_range::~out_of_range() noexcept {}
              ^
C:\Zig\lib\libcxx\include/stdexcept:161:29: note: previous declaration is here
class _LIBCPP_EXCEPTION_ABI out_of_range
                            ^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:41:15: error: definition of implicitly declared destructor
out_of_range::~out_of_range() noexcept {}
              ^
C:\Zig\lib\libcxxabi\src/stdlib_stdexcept.cpp:43:14: warning: function previously declared with an implicit exception specification redeclared with an explicit exception specification [-Wimplicit-exception-spec-mismatch]
range_error::~range_error() noexcept {}
             ^
C:\Zig\lib\libcxx\include/stdexcept:174:29: note: previous declaration is here
class _LIBCPP_EXCEPTION_ABI range_error
                            ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
7 warnings and 20 errors generated.

Expected Behavior

I guess it should work? My goal is try to compile a project that relies on third-party code that is MSVC-specific.

kassane commented 6 months ago

Please, try replacing to zig cc over zig c++ or zig build-exe -lc no -lc++

Zig toolchain can't build libcxx for MSVC yet.

Reference