Linking cgo with Visual Studio x64 release libraries on Windows

3,039 views
Skip to first unread message

adi.4...@gmail.com

unread,
Apr 20, 2015, 4:21:18 PM4/20/15
to golan...@googlegroups.com
I'm trying to link a Go program with a C function from a dll compiled in Visual Studio (x64) on Windows.

The C++ code is this:


extern "C" {
__declspec(dllexport) int add(int a, int b) {
return a+b;
}
};


The Go code is:


package main

/*
__declspec(dllimport) int add(int,int);
#cgo LDFLAGS: cproj.lib
*/
import "C"

import "fmt"

func main() {
        a := C.int(3)
        b := C.int(4)
        c := C.add(a,b)
        d := uint32(c);
        fmt.Printf("a+b = %v\n", d)
}


I put the .lib and .dll files generated by Visual Studio, next to the .go file.

But when I build using the command

go build cgo_example.go

I get the following output:

# command-line-arguments
cproj.lib: error adding symbols: File format not recognized
collect2.exe: error: ld returned 1 exit status

I must be doing something wrong.
Any ideas what it is?

andrewc...@gmail.com

unread,
Apr 20, 2015, 9:33:59 PM4/20/15
to golan...@googlegroups.com
I could be wrong, but I thought cgo on windows required a mingw based c compiler.

chai2010

unread,
Apr 20, 2015, 10:42:03 PM4/20/15
to adi.4...@gmail.com, golang-nuts
You need generate x.a for cgo link.

1. create cproj.def file:

```
LIBRARY cproj.dll

EXPORTS
add
```

2. generate libcproj.a

```
dlltool -dllname cproj.dll --def cproj.def --output-lib libcproj.a
```

3. cgo link libcproj.a

```
#cgo LDFLAGS: -L. -lcproj
```

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Doug Henderson

unread,
Apr 20, 2015, 11:45:37 PM4/20/15
to andrewc...@gmail.com, golang-nuts
HI,
I believe there are (at least) a couple of steps needed to allow you to use 3rd party supplied or self created windows libraries.

The loader does not like the format of a <x>.lib file.

"gendef" compiled for your version of mingw64 creates  <x>.def file from a <x>.lib

"dlltool" for your version of mingw64 creates a lib<x>.a file from a <x>.dll file and the <x>.def file

The loader likes the format of the <x>.a file. This means, I think, that the <x>.dll has been ripped apart to create the <x>.a file.

I found the documentation for cgo to be a bit like an onion. You have to keep peeling back layers until you get to the detailed answers.

Best of luck, and please report back on how you did it, or I may be asking the same question later!

Doug


On 20 April 2015 at 19:33, <andrewc...@gmail.com> wrote:
I could be wrong, but I thought cgo on windows required a mingw based c compiler.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Doug Henderson, Calgary, Alberta, Canada

Adi Levin

unread,
Apr 21, 2015, 1:07:13 AM4/21/15
to golan...@googlegroups.com, adi.4...@gmail.com
It worked like a charm! I just followed chai2010's instructions, and there was no problem. Thank you!
Reply all
Reply to author
Forward
0 new messages