Skip to content

cmd/link: MSVC support (MSVC object + linking with MSVC) #20982

Open
@ghost

Description

I understand that the go linker cannot currently link msvc object files and also recognize that this issue is likely to be a low priority. However, it would be nice to support this because it would somewhat simplify windows workflow. This issue is mainly to understand how much effort this would be and/or what would be required.

Activity

changed the title [-]cgo/ support msvc object files using go linker[/-] [+]cmd/link: support msvc object files[/+] on Jul 11, 2017
bradfitz

bradfitz commented on Jul 11, 2017

@bradfitz
Contributor
alexbrainman

alexbrainman commented on Jul 12, 2017

@alexbrainman
Member

@xoviat what is the problem that you are having? I need to be able to reproduce it here. So, please, provide all steps I will need to follow to reproduce this.

Thank you

Alex

PS: I won't have computer until end of July. I will have a look at this then.

ghost

ghost commented on Jul 12, 2017

@ghost

what is the problem that you are having?

I haven't tried it yet, but I would like to specifically call c functions in msvc object files by linking them as .syso with the go linker. Everything that I have read indicates that this is not possible but I will create a procedure to reproduce the specific error that occurs.

alexbrainman

alexbrainman commented on Jul 12, 2017

@alexbrainman
Member

specifically call c functions in msvc object files by linking them as .syso with the go linker

Have you tried building these into a DLL, and use them from inside of DLL?

I will create a procedure to reproduce the specific error that occurs.

Please, do. Thank you.

Alex

ghost

ghost commented on Jul 12, 2017

@ghost

Have you tried building these into a DLL, and use them from inside of DLL?

That was actually my original plan. I am using swig so it is not so convenient to compile the generated c code separately and then rewrite the functions as DLL exports. It's not difficult, but it is annoying when the workflow with gcc is just go generate; go build.

ghost

ghost commented on Jul 13, 2017

@ghost

Alright, I have a procedure. Start with these files:

hello.go:

package main

/*
	extern void hello();
*/
import "C"

func main() {
	C.hello()
}

hello.c:

#include <stdio.h>

extern void hello()
{
    printf("Hello World from C");
}

then run these commands (with msvc on path):

cl /c hello.c
mv hello.obj hello.syso
mv hello.c hello.c.bak
go build

Result:
Warning: corrupt .drectve at end of def file

When running the produced file:

Exception 0xc0000005 0x8 0x13 0x13
PC=0x13
signal arrived during external code execution

main._Cfunc_hello()
        _//_obj/_cgo_gotypes.go:41 +
main.main()
        C://hello.go:9 +0x27

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
        C:/Program Files/Go/src/runtime/asm_amd64.s:2197 +0x1
rax     0x4a5960
rbx     0xc042045f78
rcx     0x4a9a20
rdi     0xc042045f78
rsi     0x4adc60
rbp     0xc042045f38
rsp     0x6dfd68
r8      0xc042016340
r9      0x0
r10     0xc04204faa0
r11     0x4783c2
r12     0x0
r13     0x6
r14     0x0
r15     0xf1
rip     0x13
rflags  0x10216
cs      0x33
fs      0x53
gs      0x2b
alexbrainman

alexbrainman commented on Jul 13, 2017

@alexbrainman
Member

I do not have cl command installed on my pc. How do I install msvc?

Thank you.

Alex

ghost

ghost commented on Jul 13, 2017

@ghost

You need the "build tools 2017" here. Microsoft actually now allows anyone to use visual studio for free as long as it's not for commercial development. If it's too much trouble, I can just give you the object file if you want.

ghost

ghost commented on Jul 13, 2017

@ghost

If only c++ was not a thing, I wouldn't have to worry about this. But it is, so the pain continues...

added
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Jul 13, 2017
added this to the Go1.10 milestone on Jul 13, 2017
alexbrainman

alexbrainman commented on Jul 13, 2017

@alexbrainman
Member

You need the "build tools 2017" here.

Got it. Thank you.

If it's too much trouble, I can just give you the object file if you want.

Yes, please, post hello.obj somewhere.

Thinking about this some more. You are actually using gcc to link object file compiled with msvc compiler. Can you try and do your exercise, but replacing "go build" step with gcc linking hello.obj to a C program? Perhaps it has been done before. I suspect, if we know how to do that, we might be able to do similar with Go.

Alex

ghost

ghost commented on Jul 13, 2017

@ghost

AFAIK lld https://github.com/llvm-mirror/lld supports msvc object files.

Object file is here: https://github.com/xoviat/msvcgo/blob/master/hello.syso

259 remaining items

hajimehoshi

hajimehoshi commented on Jun 14, 2025

@hajimehoshi
Member

I've reported a problem about using MSVC to link a C-archive file generated by Cgo/MinGW (#74189), not creating a C-archive file with MSVC. Is this problem tracked in this issue?

changed the title [-]cmd/link: support msvc object files[/-] [+]cmd/link: MSVC support (MSVC object + linking with MSVC)[/+] on Jun 14, 2025
cherrymui

cherrymui commented on Jun 14, 2025

@cherrymui
Member

Currently we don't have any MSVC support. Let's use this issue as a central place for tracking. (I can't edit the original issue content by @ghost, so I just updated the title.)

hajimehoshi

hajimehoshi commented on Jun 15, 2025

@hajimehoshi
Member

For #74189, would it make sense to replace fprintf in runtime/cgo with some compatible thing, like an original implemetation with WriteConsoleW or OutputDebugStringW for Windows, so that we can avoid this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Buildersx/build issues (builders, bots, dashboards)FeatureRequestIssues asking for a new feature that does not need a proposal.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.OS-Windowscompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @bradfitz@deadprogram@mattn@hajimehoshi@rsc

        Issue actions

          cmd/link: MSVC support (MSVC object + linking with MSVC) · Issue #20982 · golang/go