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: aborts on attempt to link to static library on windows #44308

Closed
b0bh00d opened this issue Feb 16, 2021 · 3 comments
Closed

cmd/cgo: aborts on attempt to link to static library on windows #44308

b0bh00d opened this issue Feb 16, 2021 · 3 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

@b0bh00d
Copy link

b0bh00d commented Feb 16, 2021

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

$ go version
go version go1.15.8 windows/amd64

Does this issue reproduce with the latest release?

First time I've used Go, so 1.15.8 is the only version I've used.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Administrator\AppData\Local\go-build
set GOENV=C:\Users\Administrator\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Administrator\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Administrator\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=D:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -fmessage-length=0 -fdebug-prefix-map=N:\Temp\go-build229303370=/tmp/go-build -gno-record-gcc-switches

What did you do?

Using Microsoft's compiler (Visual Studio 2019 Professional) from the command line, I created a simple static-link library with a single function in two files, test.h and test.c,which contain the following lines:

test.h
int test(const char* value);

test.c

#include <stdio.h>

int test(const char *value)
{
    printf("value: %s\n", value);
    return 9;
}

I used the following command line calls to build the test.lib file:

cl /c test.c
lib test.obj

I then copied the test.h and test.lib files into the same folder as my Go code. My Go code, in a file called "ls.go", contains the following interface lines at the top:

// #cgo CFLAGS: -I${SRCDIR}
// #cgo LDFLAGS: -L${SRCDIR} -ltest
// #include <test.h>
import "C"

My Go code has no reference to the test() function contained in the static link library.

What did you expect to see?

I expected to see nothing, which is what I have typically seen with a successful compile.

What did you see instead?

When I executed the line "go build -x ls.go" I am getting the following output:

WORK=N:\Temp\go-build032909834
mkdir -p $WORK\b001\
cd M:\Projects\ls
TERM='dumb' CGO_LDFLAGS='"-g" "-O2" "-LM:/Projects/ls" "-ltest"' "D:\\Go\\pkg\\tool\\windows_amd64\\cgo.exe" -objdir "$WORK\\b001\\" -importpath command-line-arguments -- -I "$WORK\\b001\\" -g -O2 -IM:/Projects/ls "M:\\Projects\\ls\\ls.go"
# command-line-arguments

abnormal program termination

If it's of any assistance, following this, I checked the error code:

$ echo %errorlevel%
2
@seankhliao seankhliao changed the title Windows Go aborts on attempt to link to static library cmd/cgo: aborts on attempt to link to static library on windows Feb 16, 2021
@seankhliao seankhliao added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows labels Feb 16, 2021
@AlexRouSg
Copy link
Contributor

AlexRouSg commented Feb 16, 2021

Go uses and supports only mingw-w64 on windows, so you can only link things that you can link with mingw and static libs created with msvc isn't one of them. You can however dynamic link to msvc dlls.

You can follow #20982 for msvc support

@b0bh00d
Copy link
Author

b0bh00d commented Feb 17, 2021

Hi, Alex. Thanks for the response.

I've added an export to the test.cpp module for the DLL:

__declspec(dllexport) int test(const char *value)

I can create a *.dll of my test code with the following commands:

cl /c test.c
link /DLL /OUT.test.dll test.obj /MACHINE:X64 /INCREMENTAL:NO

which leaves me with test.h and test.dll.

Once I have these files in hand, is there anything different I need to do in order to tell (c)go to link with the the DLL instead?

@AlexRouSg
Copy link
Contributor

Once I have these files in hand, is there anything different I need to do in order to tell (c)go to link with the the DLL instead?

It's pretty much the same thing you'd have to do with mingw so you can lookup what to on on mingw. Easiest would be to not use the same file name for the static lib and dll if you need to have both in the same place.

Please see the link below for better places to ask for help on the topic as the issue tracker is mainly for bugs or proposals.
https://github.com/golang/go/wiki/Questions

@b0bh00d b0bh00d closed this as completed Feb 17, 2021
@golang golang locked and limited conversation to collaborators Feb 17, 2022
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

4 participants