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/cgo: __declspec(dllexport) generated in header file while building shared-c lib on windows #56994

Open
Marco-LIU opened this issue Nov 30, 2022 · 3 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Milestone

Comments

@Marco-LIU
Copy link

I use go1.18 building dll on windows.
The header file cgo generated will have __declspec(dllexport) in function declarations.
So before I publish lib to my colleague, I must change __declspec(dllexport) to __declspec(dllimport) handly.

Is there a method to generate __declspec(dllimport) directly? Or is it an issue?

in go, build command: go build -buildmode=c-shared -o libadd.go.dll .
//export Add

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

in generated c header: libadd.go.h
extern __declspec(dllexport) int Add(int a, int b);

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Nov 30, 2022
@bcmills bcmills added OS-Windows NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Nov 30, 2022
@bcmills bcmills added this to the Backlog milestone Nov 30, 2022
@bcmills
Copy link
Contributor

bcmills commented Nov 30, 2022

The __declspec(dllexport) was added in CL 262797 for #30674 (attn @qmuntal @ianlancetaylor @alexbrainman).

It does seem like we may need to generate separate header declarations for compiling the cgo package itself vs. writing out the header file for C callers.

(CC @golang/windows)

@qmuntal
Copy link
Contributor

qmuntal commented Nov 30, 2022

@Marco-LIU which failure are you seeing? While it is true that headers should mark symbols defined by a DLL with __declspec(dllimport), I have used the cgo export header, which uses __declspec(dllexport), without problems in multiple projects, either with mingw and msvc.

Anyway, it is probably a good idea to generate a separate declaration for the cgo export header, as @bcmills mentioned.

@Marco-LIU
Copy link
Author

Marco-LIU commented Dec 1, 2022

@Marco-LIU which failure are you seeing? While it is true that headers should mark symbols defined by a DLL with __declspec(dllimport), I have used the cgo export header, which uses __declspec(dllexport), without problems in multiple projects, either with mingw and msvc.

Anyway, it is probably a good idea to generate a separate declaration for the cgo export header, as @bcmills mentioned.

@qmuntal thanks for your reply.
In most case, it is ok, compiler and linker have no error commited. It just makes a confusion for reading. For the go lib developer, there is a different on function declaration with expectation. This could lead other errors....

If I define a helper function in CGO comments, this function should call a exported interface, I must make a forward declaration for this exported interface. Then the compiler(clang) will commit a ERROR. To fix this, __declspec(dllexport) should be added in forward declaration. It also makes a reading confusion, and makes the code uncompatible for Linux or Mac.

EXAMPLE:
in go, build command: go build -buildmode=c-shared -o libadd.go.dll .
// extern "C" {
// extern int Add(int a, int b); // ERROR(redeclaration of 'Add' cannot add 'dllexport' attribute)
// static int TripleAdd(int a, int b, int c) {
// return Add(Add(a, b), c);
// }
import "C"

//export Add
func Add(a, b int) int {
return a+b
}

in generated c header: libadd.go.h
extern __declspec(dllexport) int Add(int a, int b);

@mknyszek mknyszek changed the title cmd/cgo: __declspec(dllexport) genrated in header file while building shared-c lib on windows cmd/cgo: __declspec(dllexport) generated in header file while building shared-c lib on windows Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Projects
Development

No branches or pull requests

4 participants