ziglang / zig

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

support -lc++ on windows for the msvc ABI #5312

Open SpexGuy opened 4 years ago

SpexGuy commented 4 years ago

On Windows, zig c++ empty_file.cpp fails with this error:

C:\Users\Martin\Projects\Zig-ImGui>zig c++ test.cpp
zig: warning: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]
zig: warning: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]
zig: warning: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]
zig: warning: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]
zig: warning: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]
In file included from C:\Program Files\Zig\Current\lib\zig\libcxxabi\src/cxa_aux_runtime.cpp:14:
In file included from C:\Program Files\Zig\Current\lib\zig\libcxx\include\typeinfo:72:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\vcruntime_typeinfo.h:115:10: error:
      target of using declaration conflicts with declaration already in scope
        using ::type_info;
                ^
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\vcruntime_typeinfo.h:65:7: note:
      target of using declaration
class type_info
      ^
C:\Program Files\Zig\Current\lib\zig\libcxxabi\include\cxxabi.h:29:27: note: conflicting declaration
class _LIBCXXABI_TYPE_VIS type_info; // forward declaration
                          ^
1 error generated.

The following command failed:
C:\Program Files\Zig\Current\zig.exe clang -c -nostdinc -nostdinc++ -fno-spell-checking -target x86_64-unknown-windows-msvc -isystem C:\Program Files\Zig\Current\lib\zig\include -isystem C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt -isystem C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include -isystem C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt\..\um -isystem C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt\..\shared -Xclang -target-cpu -Xclang skylake -Xclang -target-feature -Xclang -3dnow,-3dnowa,+64bit,+adx,+aes,+avx,+avx2,-avx512bf16,-avx512bitalg,-avx512bw,-avx512cd,-avx512dq,-avx512er,-avx512f,-avx512ifma,-avx512pf,-avx512vbmi,-avx512vbmi2,-avx512vl,-avx512vnni,-avx512vp2intersect,-avx512vpopcntdq,+bmi,+bmi2,-branchfusion,-cldemote,+clflushopt,-clwb,-clzero,+cmov,+cx16,+cx8,-enqcmd,+ermsb,+f16c,-false-deps-lzcnt-tzcnt,+false-deps-popcnt,-fast-11bytenop,-fast-15bytenop,-fast-bextr,+fast-gather,-fast-hops,-fast-lzcnt,+fast-scalar-fsqrt,-fast-scalar-shift-masks,+fast-shld-rotate,+fast-variable-shuffle,+fast-vector-fsqrt,-fast-vector-shift-masks,+fma,-fma4,+fsgsbase,+fxsr,-gfni,-idivl-to-divb,+idivq-to-divl,+invpcid,-lea-sp,-lea-uses-ag,-lwp,+lzcnt,+macrofusion,+merge-to-threeway-branch,+mmx,+movbe,-movdir64b,-movdiri,-mpx,-mwaitx,+nopl,-pad-short-functions,+pclmul,-pconfig,-pku,+popcnt,-prefer-128-bit,-prefer-256-bit,-prefer-mask-registers,-prefetchwt1,+prfchw,-ptwrite,-rdpid,+rdrnd,+rdseed,-retpoline,-retpoline-external-thunk,-retpoline-indirect-branches,-retpoline-indirect-calls,+rtm,+sahf,+sgx,-sha,-shstk,+slow-3ops-lea,-slow-incdec,-slow-lea,-slow-pmaddwd,-slow-pmulld,-slow-shld,-slow-two-mem-ops,-slow-unaligned-mem-16,-slow-unaligned-mem-32,-soft-float,+sse,-sse-unaligned-mem,+sse2,+sse3,+sse4.1,+sse4.2,-sse4a,+ssse3,-tbm,-use-aa,-use-glm-div-sqrt-costs,-vaes,-vpclmulqdq,+vzeroupper,-waitpkg,-wbnoinvd,+x87,-xop,+xsave,+xsavec,+xsaveopt,+xsaves -MD -MV -MF C:\Users\Martin\AppData\Local\zig\stage1\tmp\mNdq4h1lx5Zr-cxa_aux_runtime.obj.d -fno-omit-frame-pointer -D_DEBUG -Og -fstack-protector-strong --param ssp-buffer-size=4 -o C:\Users\Martin\AppData\Local\zig\stage1\tmp\mNdq4h1lx5Zr-cxa_aux_runtime.obj C:\Program Files\Zig\Current\lib\zig\libcxxabi\src/cxa_aux_runtime.cpp -DHAVE___CXA_THREAD_ATEXIT_IMPL -D_LIBCPP_DISABLE_EXTERN_TEMPLATE -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS -D_LIBCXXABI_BUILDING_LIBRARY -D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -I C:\Program Files\Zig\Current\lib\zig\libcxxabi\include -I C:\Program Files\Zig\Current\lib\zig\libcxx\include -O3 -DNDEBUG -nostdinc++ -fstrict-aliasing -funwind-tables -D_DEBUG -UNDEBUG -std=c++11

The same error is observed when building pub fn main() void {} with zig build-exe -lc++.

Adding --target=native-native-gnu seems to fix it.

alexnask commented 4 years ago

The issue appears to be that we try to compile libcxxabi when using the MSVC c++ stdlib, I assume this is just wrong.

kubkon commented 3 years ago

I don't think this is still an issue. @SpexGuy would you mind double checking that the error is now a linking error stating the lack of _WinMain export? I actually would have expected _main undefined rather than _WinMain meaning we probably set subsystem to Windows by default rather than console.

SpexGuy commented 3 years ago

This doesn't seem to be fixed, but some things have rearranged. Since we changed to the mingw abi by default, you need to use this command now to reproduce: zig c++ -target native-native-msvc empty_file.cpp

We compile libc files in parallel now, so the error message is considerably harder to read as errors from many files are interleaved. I've attached the result of running this command: master_stderr.txt

kubkon commented 3 years ago

OK, some success in at least narrowing this down. As far as I understood from LLVM's docs, they "guarantee" (or claim) that it is possible to build libcxx lib with ucrt and vcruntime support (equivalent to compiling with msvc rather than clang), however, there is not much on building libcxxabi as a replacement lib for msvc ABI. But, lemme talk some facts.

As far as I understood, ucrt is the closest equivalent to libc on Windows while vcruntime to libc++ with the former including the latter (yeah, they are very much intertwined). LLVM's libcxx takes special care to respect that and it builds fine with Zig as the error brought up in this issue is actually about libcxxabi. The error comes from the fact that clang complains about a mismatching forward declaration in cxxabi.h within the LLVM's libcxxabi which is

namespace std {
class  type_info;
}

while being declared into the std namespace in vcruntime_typeinfo.h (part of vcruntime lib) as

namespace std {
  using ::type_info;
}

where it is also defined in the global scope. FWIW this actually should build fine with GCC, however, clang complains with an error

      target of using declaration conflicts with declaration already in scope
        using ::type_info;

This can be fixed with the following diff to the cxxabi.h in libcxxabi shipped with Zig:

diff --git a/lib/libcxxabi/include/cxxabi.h b/lib/libcxxabi/include/cxxabi.h
index 43ce6f5f7..c51579d77 100644
--- a/lib/libcxxabi/include/cxxabi.h
+++ b/lib/libcxxabi/include/cxxabi.h
@@ -25,12 +25,14 @@

 #ifdef __cplusplus

-namespace std {
 #if defined(_WIN32)
 class _LIBCXXABI_TYPE_VIS type_info; // forward declaration
 #else
 class type_info; // forward declaration
 #endif
+
+namespace std {
+    using ::type_info;
 }

However, then we still hit a far more critical error

In file included from C:\Users\jk\dev\zig\build-release\lib\zig\libcxxabi\src/cxa_guard.cpp:15:
C:\Users\jk\dev\zig\build-release\lib\zig\libcxxabi\src/cxa_guard_impl.h:42:10: fatal error: 'unistd.h' file not found
#include <unistd.h>
         ^~~~~~~~~~

As you will know, unistd.h is a header available on Posix systems which MS lacks (I think). And how to handle this, I think we'll need to have some discussion about next steps.

jfm535 commented 3 years ago

I just want to say even if you get past the missing unistd header you will then need to empty alot of of the cpp files because of re-declaration errors.

MikoverseAllar commented 2 years ago

Dang, I am also stuck here. Seeing issues with exceptions and type_info as above when targeting msvc from ubuntu on 10.0-dev.3475, but compiles fine with gnu.

SpexGuy commented 2 years ago

Not sure if this is useful, but I've had success with passing cpp files to zig build-exe, and passing -lc instead of -lc++. At least some c++ std lib headers and objects are available with the msvc ABI and these settings.