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

cmd/go: c-shared header does not compile on Visual Studio #36233

Closed
xp19870106 opened this issue Dec 20, 2019 · 7 comments
Closed

cmd/go: c-shared header does not compile on Visual Studio #36233

xp19870106 opened this issue Dec 20, 2019 · 7 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows

Comments

@xp19870106
Copy link

What version of Go are you using (go version)?

go1.13.5 windows/386

Does this issue reproduce with the latest release?

Yes

What did you do?

go build -buildmode=c-shared -o client.dll test.go

this command generated a client.h header file, it has some code like this:
typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt32 GoInt;
typedef GoUint32 GoUint;
typedef SIZE_TYPE GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;

What did you expect to see?

Hope it can be compiled in VC

What did you see instead?

But this file can not compile in Visual Studio, the error is :
Error 28 error C2061: syntax error : identifier 'GoUintptr'
Error 30 error C2061: syntax error : identifier 'GoComplex64'
Error 32 error C2061: syntax error : identifier 'GoComplex128'

@xp19870106
Copy link
Author

And I'm curious that there is no lib file, how is the msvc link the code?

@ALTree ALTree added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows labels Dec 23, 2019
@ALTree ALTree changed the title Go build dll can not work in VC cmd/go: c-shared header does not compile on Visual Studio Dec 23, 2019
@Tkd-Alex
Copy link

I have the same problem, did you find for a solution?

@gopherbot
Copy link

Change https://go.dev/cl/379474 mentions this issue: cmd/cgo: use size_t instead of __SIZE_TYPE__

gopherbot pushed a commit that referenced this issue Mar 31, 2022
__SIZE_TYPE__ is a GCC type which has been superseded
by size_t -define in stddef.h- since ISO C99.

cmd/cgo already uses size_t in many places, but still generates several
files using __SIZE_TYPES__, most notably the _cgo_export.h.

This change replaces all __SIZE_TYPES__ occurrences with size_t.

Updates #36233

Change-Id: Id8a99b5d7763caab9333eab9b585e78249a37415
Reviewed-on: https://go-review.googlesource.com/c/go/+/379474
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
@gopherbot
Copy link

Change https://go.dev/cl/397134 mentions this issue: cmd/go: cgo export header to be compatible with MSVC complex types

@qmuntal
Copy link
Contributor

qmuntal commented Mar 31, 2022

And I'm curious that there is no lib file, how is the msvc link the code?

MSVC can link Go DLL's compiled by MinGW using different tricks.

The one I use the most is this one:

# 1. Build MinGW DLL.
go build -buildmode=c-shared -o foo.dll . # Generate MinGW DLL
# 2. Generate a def file out of the DLL.
gendef foo.dll
# 3. Generate the MSVC import library.
dlltool --input-def foo.def --output-lib foo.lib 
# 4. Compile the C file using MSVC with the import library previously generated.
# There is no need for foo.dll to be present when compiling C,
# but it must be available when running the resulting binary.
cl.exe main.c foo.lib 

@jsoneaday
Copy link

And I'm curious that there is no lib file, how is the msvc link the code?

MSVC can link Go DLL's compiled by MinGW using different tricks.

The one I use the most is this one:

# 1. Build MinGW DLL.
go build -buildmode=c-shared -o foo.dll . # Generate MinGW DLL
# 2. Generate a def file out of the DLL.
gendef foo.dll
# 3. Generate the MSVC import library.
dlltool --input-def foo.def --output-lib foo.lib 
# 4. Compile the C file using MSVC with the import library previously generated.
# There is no need for foo.dll to be present when compiling C,
# but it must be available when running the resulting binary.
cl.exe main.c foo.lib 

The last line can you explain? I have no c file I'm only using go. How can I do this?

@qmuntal
Copy link
Contributor

qmuntal commented Mar 22, 2023

The last line can you explain? I have no c file I'm only using go. How can I do this?

If you are only using Go why do you need Visual Studio? cl.exe is the Microsoft C/C++ compiler shipped with Visual Studio, and main.c is just used here as an example, the actual command will probably be more complex if the C project contains more files.

@golang golang locked and limited conversation to collaborators Mar 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Projects
None yet
Development

No branches or pull requests

6 participants