Is it possible to link a DLL using CGO?

1,339 views
Skip to first unread message

Dorival Pedroso

unread,
Jan 15, 2015, 12:35:01 AM1/15/15
to golan...@googlegroups.com
Hi,
I'm wondering if we can link a go code via cgo with a dll created with mingw-gcc?
Cheers
D

minux

unread,
Jan 15, 2015, 12:41:11 AM1/15/15
to Dorival Pedroso, golang-nuts
On Thu, Jan 15, 2015 at 12:35 AM, Dorival Pedroso <dorival...@gmail.com> wrote:
I'm wondering if we can link a go code via cgo with a dll created with mingw-gcc?
It's possible. Add the path to the DLL to the cgo LDFLAGS, and use cgo to
call exported function from DLL as usual.

To call integer functions from DLL, you can also use the syscall package (search for DLL
in godoc), but there are some constrains in calling functions using the syscall package
(you can't pass floating point arguments, and the stack size is limited to 64K if it's a pure
Go program), so the recommended way to use a windows DLL (no matter how it's created,
MSVC or mingw) is to use cgo.

Dorival Pedroso

unread,
Jan 15, 2015, 12:48:36 AM1/15/15
to golan...@googlegroups.com, dorival...@gmail.com
by usual do you mean something like this?
#cgo windows LDFLAGS: -lblas -LC:\Gosl\lib\lib64\lapack_blas_windows

i'm getting errors:
$ go build
c:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lblas
collect2.exe: error: ld returned 1 exit status

by the way, it's win64
many thanks
d

Dorival Pedroso

unread,
Jan 15, 2015, 12:53:41 AM1/15/15
to golan...@googlegroups.com, dorival...@gmail.com
ok, i've found one mistake: has to use forward slashes "/" 

the thing compiles but go test fails because it cannot find the dll
i suppose i have to copy this dll to the systems folder ...

thanks again
d

minux

unread,
Jan 15, 2015, 12:54:19 AM1/15/15
to Dorival Pedroso, golang-nuts
On Thu, Jan 15, 2015 at 12:48 AM, Dorival Pedroso <dorival...@gmail.com> wrote:
by usual do you mean something like this?
#cgo windows LDFLAGS: -lblas -LC:\Gosl\lib\lib64\lapack_blas_windows

i'm getting errors:
$ go build
c:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lblas
collect2.exe: error: ld returned 1 exit status
can you link normal c programs to libblas.dll with -lblas -LC:\Gosl\lib\lib64\lapack_blas_windows?

minux

unread,
Jan 15, 2015, 12:58:07 AM1/15/15
to Dorival Pedroso, golang-nuts
On Thu, Jan 15, 2015 at 12:53 AM, Dorival Pedroso <dorival...@gmail.com> wrote:
ok, i've found one mistake: has to use forward slashes "/" 

the thing compiles but go test fails because it cannot find the dll
i suppose i have to copy this dll to the systems folder ...
just put the directory containing the DLL into %PATH%, no need to copy
the DLL into system folders. 

Dorival Pedroso

unread,
Jan 15, 2015, 1:09:26 AM1/15/15
to golan...@googlegroups.com, dorival...@gmail.com
it worked!
i've put all DLLs in C:\Gosl\lib
and added this directory to system PATH
many thanks
d

Dorival Pedroso

unread,
Jan 15, 2015, 1:31:27 AM1/15/15
to golan...@googlegroups.com, dorival...@gmail.com
another problem: after successfully compiling and linking, i get massive errors such as "invalid runtime symbol table"

e.g.

500 SuiteSparse_start   0x563570 SuiteSparse_finish     0x563580 SuiteSparse_malloc     0x5636c0
aa0 amd_l1      0x563dd0 clear_flag.part.0      0x563e00 amd_l2 0x5655e0 amd_l_aat      0x5658b0
l_analyze       0x567390 umf_l_apply_order      0x5673e0 row_assemble   0x567600 col_assemble   0
l_extend_front  0x56b370 umf_l_free     0x56b390 umf_l_fsize    0x56b470 umfdl_garbage_collection
0x56e9b0 umfdl_local_search     0x5705c0 umfdl_lsolve   0x5707b0 umfdl_ltsolve  0x570a20 umf_l_ma
l_lhsolve       0x571570 umfdl_uhsolve  0x571980 umfdl_triplet_map_nox  0x571db0 umfdl_triplet_ma
0x574be0 umfdl_report_vector    0x574d90 umfdl_row_search       0x5758b0 umfdl_scale    0x575950
l_solve 0x579070 umfdl_start_front      0x579480 umfdl_store_lu 0x57a3e0 umfdl_symbolic_usage   0
780 umfpack_dl_col_to_triplet   0x6f6ea0 umfpack_dl_defaults    0x6f6ea8 umfpack_dl_free_numeric

minux

unread,
Jan 15, 2015, 2:10:38 AM1/15/15
to Dorival Pedroso, golang-nuts
On Thu, Jan 15, 2015 at 1:31 AM, Dorival Pedroso <dorival...@gmail.com> wrote:
another problem: after successfully compiling and linking, i get massive errors such as "invalid runtime symbol table"

e.g.

500 SuiteSparse_start   0x563570 SuiteSparse_finish     0x563580 SuiteSparse_malloc     0x5636c0
aa0 amd_l1      0x563dd0 clear_flag.part.0      0x563e00 amd_l2 0x5655e0 amd_l_aat      0x5658b0
l_analyze       0x567390 umf_l_apply_order      0x5673e0 row_assemble   0x567600 col_assemble   0
l_extend_front  0x56b370 umf_l_free     0x56b390 umf_l_fsize    0x56b470 umfdl_garbage_collection
0x56e9b0 umfdl_local_search     0x5705c0 umfdl_lsolve   0x5707b0 umfdl_ltsolve  0x570a20 umf_l_ma
l_lhsolve       0x571570 umfdl_uhsolve  0x571980 umfdl_triplet_map_nox  0x571db0 umfdl_triplet_ma
0x574be0 umfdl_report_vector    0x574d90 umfdl_row_search       0x5758b0 umfdl_scale    0x575950
l_solve 0x579070 umfdl_start_front      0x579480 umfdl_store_lu 0x57a3e0 umfdl_symbolic_usage   0
780 umfpack_dl_col_to_triplet   0x6f6ea0 umfpack_dl_defaults    0x6f6ea8 umfpack_dl_free_numeric
The "invalid runtime symbol table" message originates from the Go runtime.

I have never seen this message. If you haven't modified the Go toolchain or
runtime, It might indicate a linker/runtime problem. Could you please try getting
a minimal example that still exhibit this behavior and file an issue at

BTW, if you're not using the latest release, please try Go 1.4 first, because Go
doesn't fix bugs in releases other than the latest.

Thanks.

Dorival Pedroso

unread,
Feb 2, 2015, 11:29:19 PM2/2/15
to golan...@googlegroups.com, dorival...@gmail.com
thank you.

i'm using go1.4.1 now

the problem persists. i'm getting this:


Exception 0xc0000005 0x0 0xd84901f6 0x6c236e50
PC=0x6c236e50
signal arrived during cgo execution

code.google.com/p/gosl/la._Cfunc_umfpack_dl_numeric(0xc08209d110, 0xc0820122a0, 0xc082012310, 0x8593f0, 0xc082036410, 0xc082036460, 0x0, 0x0)
        C:/Users/Dorival/Documents/MyGO/src/code.google.com/p/gosl/la/linsol_umfpack.go:190 +0xc8f
code.google.com/p/gosl/la.run_linsol_testR(0xc08205c000, 0xc0820120e0, 0x3d06849b86a12b9b, 0x3d3c25c268497682, 0xc08209d0e0, 0x5, 0x5, 0xc082021ec0, 0x5, 0x5
)
        C:/Users/Dorival/Documents/MyGO/src/code.google.com/p/gosl/la/t_linsol_test.go:31 +0x202
        C:/Users/Dorival/Documents/MyGO/src/code.google.com/p/gosl/la/t_linsol_test.go:135 +0x5e1
testing.tRunner(0xc08205c000, 0x6dc980)
        c:/go/src/testing/testing.go:447 +0xc6
created by testing.RunTests
        c:/go/src/testing/testing.go:555 +0xa92

goroutine 1 [chan receive]:
testing.RunTests(0x641808, 0x6dc860, 0x25, 0x25, 0x4f95cda70b34601)
        c:/go/src/testing/testing.go:556 +0xadd
testing.(*M).Run(0xc08200a2d0, 0x6e9720)
        c:/go/src/testing/testing.go:485 +0x73
main.main()

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
        c:/go/src/runtime/asm_amd64.s:2232 +0x1
rax     0x2ce4ae0
rbx     0x24f330
rcx     0x6c24bbb0
rdx     0x24edfc
rdi     0x2ce2be0
rsi     0x859a70
rbp     0x20
rsp     0x24ec38
r8      0x24ee00
r9      0x24ee10
r10     0x859e60
r11     0x0
r12     0x8599b0
r13     0x4
r14     0x0
r15     0x8599f0
rip     0x6c236e50
rflags  0x10202
cs      0x33
fs      0x53
gs      0x2b
exit status 2
FAIL    code.google.com/p/gosl/la       1.146s

Dave Cheney

unread,
Feb 3, 2015, 12:07:18 AM2/3/15
to golan...@googlegroups.com, dorival...@gmail.com
At a guess your C code segfaulted.
Reply all
Reply to author
Forward
0 new messages