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

Cross-compiling Golang static lib along with Rust using MSVC runtime on Linux #138

Open
NuLL3rr0r opened this issue Aug 25, 2024 · 4 comments

Comments

@NuLL3rr0r
Copy link

NuLL3rr0r commented Aug 25, 2024

Thank you very much for the awesome project and your blog post about it.

I'm using rust-cross/cargo-xwin to build the Rust portion of the code which works fine. Then I try to build a Go library which my Rust code relies on using FFI, so ideally, I would like to build the Go code with the same toolchain.

Since Go by default relies on MinGW and not MSVC toolchain to produce the archive file, I decided to go using Zig CC. This is how it's invoked:

$ env CC="zig cc -target x86_64-windows-msvc -fno-builtin \
        -I/home/mamadou/.cache/cargo-xwin/xwin/crt/include \
        -I/home/mamadou/.cache/cargo-xwin/xwin/sdk/include \
        -I/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/cppwinrt \
        -I/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt \
        -I/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/um \
        -I/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/shared \
        -L/home/mamadou/.cache/cargo-xwin/xwin/crt/lib/x86_64 \
        -L/home/mamadou/.cache/cargo-xwin/xwin/sdk/lib/ucrt/x86_64 \
        -L/home/mamadou/.cache/cargo-xwin/xwin/sdk/lib/um/x86_64" \
    CXX="zig c++ -target x86_64-windows-msvc -fno-builtin \
        -I/home/mamadou/.cache/cargo-xwin/xwin/crt/include \
        -I/home/mamadou/.cache/cargo-xwin/xwin/sdk/include \
        -I/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/cppwinrt \
        -I/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt \
        -I/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/um \
        -I/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/shared \
        -L/home/mamadou/.cache/cargo-xwin/xwin/crt/lib/x86_64 \
        -L/home/mamadou/.cache/cargo-xwin/xwin/sdk/lib/ucrt/x86_64 \
        -L/home/mamadou/.cache/cargo-xwin/xwin/sdk/lib/um/x86_64" \
    GODEBUG=netdns=cgo+1 \
    CGO_ENABLED=1 \
    GO111MODULE=on \
    GOOS=windows \
    GOARCH=amd64 \
    go build -buildmode=c-archive -ldflags \
    -s -o /home/mamadou/dev/MamadouX/target/mx-go.lib

The errors I get:

# runtime/cgo
In file included from gcc_context.c:7:
In file included from ./libcgo.h:7:
In file included from /home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/stdio.h:12:
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt_wstdio.h:1486:1: error: attribute declaration must precede definition [-Werror,-Wignored-attributes]
 1486 | __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_2_ARGLIST_EX(
      | ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt.h:1788:13: note: expanded from macro '__DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_2_ARGLIST_EX'
 1788 |             _CRT_INSECURE_DEPRECATE(_FuncName##_s) _DeclSpec _ReturnType _CC _FuncName(_SalAttributeDst _DstType *_Dst, _TType1 _TArg1, _TType2 _TArg2, ...); \
      |             ^
/home/mamadou/.cache/cargo-xwin/xwin/crt/include/vcruntime.h:320:55: note: expanded from macro '_CRT_INSECURE_DEPRECATE'
  320 |         #define _CRT_INSECURE_DEPRECATE(_Replacement) _CRT_DEPRECATE_TEXT(    \
      |                                                       ^
/home/mamadou/.cache/cargo-xwin/xwin/crt/include/vcruntime.h:310:47: note: expanded from macro '_CRT_DEPRECATE_TEXT'
  310 | #define _CRT_DEPRECATE_TEXT(_Text) __declspec(deprecated(_Text))
      |                                               ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt_wstdio.h:1403:33: note: previous definition is here
 1403 | _CRT_STDIO_INLINE int __CRTDECL __swprintf_l(
      |                                 ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt_wstdio.h:1486:1: error: attribute declaration must precede definition [-Werror,-Wignored-attributes]
 1486 | __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_2_ARGLIST_EX(
      | ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt.h:1789:13: note: expanded from macro '__DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_2_ARGLIST_EX'
 1789 |             _CRT_INSECURE_DEPRECATE(_SecureVFuncName) _DeclSpec _ReturnType _CC _VFuncName(_SalAttributeDst _DstType *_Dst, _TType1 _TArg1, _TType2 _TArg2, va_list _Args);
      |             ^
/home/mamadou/.cache/cargo-xwin/xwin/crt/include/vcruntime.h:320:55: note: expanded from macro '_CRT_INSECURE_DEPRECATE'
  320 |         #define _CRT_INSECURE_DEPRECATE(_Replacement) _CRT_DEPRECATE_TEXT(    \
      |                                                       ^
/home/mamadou/.cache/cargo-xwin/xwin/crt/include/vcruntime.h:310:47: note: expanded from macro '_CRT_DEPRECATE_TEXT'
  310 | #define _CRT_DEPRECATE_TEXT(_Text) __declspec(deprecated(_Text))
      |                                               ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt_wstdio.h:1205:33: note: previous definition is here
 1205 | _CRT_STDIO_INLINE int __CRTDECL __vswprintf_l(
      |                                 ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt_wstdio.h:1495:1: error: attribute declaration must precede definition [-Werror,-Wignored-attributes]
 1495 | __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1_ARGLIST_EX(
      | ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt.h:1784:13: note: expanded from macro '__DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1_ARGLIST_EX'
 1784 |             _CRT_INSECURE_DEPRECATE(_SecureFuncName) _DeclSpec _ReturnType _CC _FuncName(_SalAttributeDst _DstType *_Dst, _TType1 _TArg1, ...); \
      |             ^
/home/mamadou/.cache/cargo-xwin/xwin/crt/include/vcruntime.h:320:55: note: expanded from macro '_CRT_INSECURE_DEPRECATE'
  320 |         #define _CRT_INSECURE_DEPRECATE(_Replacement) _CRT_DEPRECATE_TEXT(    \
      |                                                       ^
/home/mamadou/.cache/cargo-xwin/xwin/crt/include/vcruntime.h:310:47: note: expanded from macro '_CRT_DEPRECATE_TEXT'
  310 | #define _CRT_DEPRECATE_TEXT(_Text) __declspec(deprecated(_Text))
      |                                               ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt_wstdio.h:1444:33: note: previous definition is here
 1444 | _CRT_STDIO_INLINE int __CRTDECL _swprintf(
      |                                 ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt_wstdio.h:1495:1: error: attribute declaration must precede definition [-Werror,-Wignored-attributes]
 1495 | __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1_ARGLIST_EX(
      | ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt.h:1785:13: note: expanded from macro '__DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1_ARGLIST_EX'
 1785 |             _CRT_INSECURE_DEPRECATE(_SecureVFuncName) _DeclSpec _ReturnType _CC _VFuncName(_SalAttributeDst _DstType *_Dst, _TType1 _TArg1, va_list _Args);
      |             ^
/home/mamadou/.cache/cargo-xwin/xwin/crt/include/vcruntime.h:320:55: note: expanded from macro '_CRT_INSECURE_DEPRECATE'
  320 |         #define _CRT_INSECURE_DEPRECATE(_Replacement) _CRT_DEPRECATE_TEXT(    \
      |                                                       ^
/home/mamadou/.cache/cargo-xwin/xwin/crt/include/vcruntime.h:310:47: note: expanded from macro '_CRT_DEPRECATE_TEXT'
  310 | #define _CRT_DEPRECATE_TEXT(_Text) __declspec(deprecated(_Text))
      |                                               ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt_wstdio.h:1221:33: note: previous definition is here
 1221 | _CRT_STDIO_INLINE int __CRTDECL _vswprintf(
      |                                 ^
In file included from gcc_context.c:7:
In file included from ./libcgo.h:7:
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/stdio.h:1793:1: error: attribute declaration must precede definition [-Werror,-Wignored-attributes]
 1793 | __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1_ARGLIST(
      | ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt.h:703:5: note: expanded from macro '__DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1_ARGLIST'
  703 |     __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1_ARGLIST_EX(_ReturnType, _ReturnPolicy, _DeclSpec, _CC, _FuncName, _FuncName##_s, _VFuncName, _VFuncName##_s, _SalAttributeDst, _DstType, _Dst, _TType1, _TArg1)
      |     ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt.h:1784:13: note: expanded from macro '__DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1_ARGLIST_EX'
 1784 |             _CRT_INSECURE_DEPRECATE(_SecureFuncName) _DeclSpec _ReturnType _CC _FuncName(_SalAttributeDst _DstType *_Dst, _TType1 _TArg1, ...); \
      |             ^
/home/mamadou/.cache/cargo-xwin/xwin/crt/include/vcruntime.h:320:55: note: expanded from macro '_CRT_INSECURE_DEPRECATE'
  320 |         #define _CRT_INSECURE_DEPRECATE(_Replacement) _CRT_DEPRECATE_TEXT(    \
      |                                                       ^
/home/mamadou/.cache/cargo-xwin/xwin/crt/include/vcruntime.h:310:47: note: expanded from macro '_CRT_DEPRECATE_TEXT'
  310 | #define _CRT_DEPRECATE_TEXT(_Text) __declspec(deprecated(_Text))
      |                                               ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/stdio.h:1769:33: note: previous definition is here
 1769 | _CRT_STDIO_INLINE int __CRTDECL sprintf(
      |                                 ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/stdio.h:1971:1: error: attribute declaration must precede definition [-Werror,-Wignored-attributes]
 1971 | __DEFINE_CPP_OVERLOAD_STANDARD_NFUNC_0_2_ARGLIST_EX(
      | ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt.h:1830:13: note: expanded from macro '__DEFINE_CPP_OVERLOAD_STANDARD_NFUNC_0_2_ARGLIST_EX'
 1830 |             _CRT_INSECURE_DEPRECATE(_FuncName##_s) _DeclSpec _ReturnType _CC _FuncName(_SalAttributeDst _DstType *_Dst, _TType1 _TArg1, _TType2 _TArg2, ...); \
      |             ^
/home/mamadou/.cache/cargo-xwin/xwin/crt/include/vcruntime.h:320:55: note: expanded from macro '_CRT_INSECURE_DEPRECATE'
  320 |         #define _CRT_INSECURE_DEPRECATE(_Replacement) _CRT_DEPRECATE_TEXT(    \
      |                                                       ^
/home/mamadou/.cache/cargo-xwin/xwin/crt/include/vcruntime.h:310:47: note: expanded from macro '_CRT_DEPRECATE_TEXT'
  310 | #define _CRT_DEPRECATE_TEXT(_Text) __declspec(deprecated(_Text))
      |                                               ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/stdio.h:1952:33: note: previous definition is here
 1952 | _CRT_STDIO_INLINE int __CRTDECL _snprintf(
      |                                 ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/stdio.h:1971:1: error: attribute declaration must precede definition [-Werror,-Wignored-attributes]
 1971 | __DEFINE_CPP_OVERLOAD_STANDARD_NFUNC_0_2_ARGLIST_EX(
      | ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/corecrt.h:1831:13: note: expanded from macro '__DEFINE_CPP_OVERLOAD_STANDARD_NFUNC_0_2_ARGLIST_EX'
 1831 |             _CRT_INSECURE_DEPRECATE(_VFuncName##_s) _DeclSpec _ReturnType _CC _VFuncName(_SalAttributeDst _DstType *_Dst, _TType1 _TArg1, _TType2 _TArg2, va_list _Args);
      |             ^
/home/mamadou/.cache/cargo-xwin/xwin/crt/include/vcruntime.h:320:55: note: expanded from macro '_CRT_INSECURE_DEPRECATE'
  320 |         #define _CRT_INSECURE_DEPRECATE(_Replacement) _CRT_DEPRECATE_TEXT(    \
      |                                                       ^
/home/mamadou/.cache/cargo-xwin/xwin/crt/include/vcruntime.h:310:47: note: expanded from macro '_CRT_DEPRECATE_TEXT'
  310 | #define _CRT_DEPRECATE_TEXT(_Text) __declspec(deprecated(_Text))
      |                                               ^
/home/mamadou/.cache/cargo-xwin/xwin/sdk/include/10.0.22000/ucrt/stdio.h:1396:33: note: previous definition is here
 1396 | _CRT_STDIO_INLINE int __CRTDECL _vsnprintf(
      |                                 ^
7 errors generated.

Unfortunately, I was not able to go further, even reordering the included paths seems not to work.

@NuLL3rr0r NuLL3rr0r changed the title Cross-compiling Golang static lib with Rust using MSVC runtime on Linux Cross-compiling Golang static lib along with Rust using MSVC runtime on Linux Aug 25, 2024
@NuLL3rr0r
Copy link
Author

As a side note, it might not be even possible to use MSVC to build the Golang archive. Not really sure, but I've found this StackOverflow answer and a repo using MSVC with MinGW-generated Go archive to link C code. Not sure, how I can translate the magic code to Rust either.

@NuLL3rr0r
Copy link
Author

NuLL3rr0r commented Aug 25, 2024

And this is the simplest Go CGO code to reproduce the issue:

package main

import (
	"C"
)

//export Add
func Add(a C.int, b C.int) C.int {
	return C.int(internal_add(int(a), int(b)))
}

func internal_add(a int, b int) int {
	return a + b
}

@trcrsired
Copy link

https://github.com/trcrsired/windows-msvc-sysroot

what about trying this? I am maintaining the headers myself since these headers need more tweaks rather than "just downloading files from microsoft". This xwin is terrible project since rust folks have no idea how compilers work in general

@NuLL3rr0r
Copy link
Author

Thanks! I'll bookmark it to check out later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants