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

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

Open
SpexGuy opened this issue May 10, 2020 · 7 comments
Open

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

SpexGuy opened this issue May 10, 2020 · 7 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-windows proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@SpexGuy
Copy link
Contributor

SpexGuy commented May 10, 2020

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
Copy link
Contributor

alexnask commented May 10, 2020

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

@Vexu Vexu added the zig cc Zig as a drop-in C compiler feature label May 11, 2020
@Vexu Vexu added this to the 0.7.0 milestone May 11, 2020
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior os-windows labels Oct 17, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 17, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.8.1 Jun 4, 2021
@kubkon
Copy link
Member

kubkon commented Jul 27, 2021

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
Copy link
Contributor Author

SpexGuy commented Jul 27, 2021

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

@andrewrk andrewrk modified the milestones: 0.8.1, 1.1.0 Jul 29, 2021
@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. and removed bug Observed behavior contradicts documented or intended behavior zig cc Zig as a drop-in C compiler feature labels Jul 29, 2021
@andrewrk andrewrk changed the title zig c++ fails on an empty file on windows support -lc++ on windows for the msvc ABI Jul 29, 2021
@andrewrk andrewrk added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. enhancement Solving this issue will likely involve adding new logic or components to the codebase. and removed enhancement Solving this issue will likely involve adding new logic or components to the codebase. labels Jul 29, 2021
@kubkon
Copy link
Member

kubkon commented Jul 29, 2021

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
Copy link

jfm535 commented Aug 15, 2021

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
Copy link

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
Copy link
Contributor Author

SpexGuy commented Aug 18, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-windows proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

7 participants