Can CGO be made to use Windows SDK instead of Windows gcc?

1,015 views
Skip to first unread message

Curtis Paul

unread,
Jan 20, 2014, 10:37:43 PM1/20/14
to golan...@googlegroups.com
...if so, can anyone offer up some examples of how one may go about it?

Curtis Paul

unread,
Jan 20, 2014, 10:42:10 PM1/20/14
to golan...@googlegroups.com
Microsoft Windows SDK v7.1 to be more specific...  cl.exe instead of gcc.exe

brainman

unread,
Jan 20, 2014, 10:49:34 PM1/20/14
to golan...@googlegroups.com
I don't know, if anyone tried that. What is the problem you are trying to solve?

Alex

Curtis Paul

unread,
Jan 20, 2014, 11:06:08 PM1/20/14
to golan...@googlegroups.com
I'm looking to include some Matlab libraries into a Go program.  The problem is, Matlab's windows libraries/headers do not support gcc.  
The Windows SDK compiler (cl.exe) does compile the Matlab windows libraries/headers however.

Golang CGO apparently wants to use gcc, and I can't find any information about how (if possible) to change the C compiler CGO uses.

Curtis Paul

unread,
Jan 20, 2014, 11:45:24 PM1/20/14
to golan...@googlegroups.com
I found this, not sure if it could be worked into a solution that works for cgo....so basically, the question could be, is it possible to convert gcc switches to ones that will work for cl?

brainman

unread,
Jan 21, 2014, 12:25:44 AM1/21/14
to golan...@googlegroups.com
On Tuesday, 21 January 2014 15:06:08 UTC+11, Curtis Paul wrote:
> ... The problem is, Matlab's windows libraries/headers do not support gcc. The Windows SDK compiler (cl.exe) does compile the Matlab windows libraries/headers however.

Does Matlab comes in a form of DLLs? If yes, you should be able to call them directly from Go. That is what we do for all Windows APIs. You just need to "translate" them to Go.  See %GOROOT%\src\pkg\syscall\zsyscall_windows_386.go for some examples.

> ... Golang CGO apparently wants to use gcc, and I can't find any information about how (if possible) to change the C compiler CGO uses.

cgo does use gcc on windows. You can look at the source in %GOROOT%\src\cmd\cgo to see what it does, perhaps you could make it work with MS compiler. You will, probably, need to change the linker (%GOROOT%\src\cmd\ld\ldpe.c) as well - Go linker need to be able to understand object files generated by MS compiler. I think it will get complicated quickly.

> ...so basically, the question could be, is it possible to convert gcc switches to ones that will work for cl?

I doubt it. You need to see what cgo does to answer that question.

Alex

Curtis Paul

unread,
Jan 21, 2014, 10:43:50 PM1/21/14
to golan...@googlegroups.com
Right on Alex, the DLL concept was a good direction.

I was able to load the DLL's I needed to get the Matlab MCR going, but Matlab has commands like SetData and mwArray in their example matrixdriver.cpp code that I'm not sure about how to use in Golang...

brainman

unread,
Jan 22, 2014, 12:39:40 AM1/22/14
to golan...@googlegroups.com
On Wednesday, 22 January 2014 14:43:50 UTC+11, Curtis Paul wrote:
> ... but Matlab has commands like SetData and mwArray in their example matrixdriver.cpp code that I'm not sure about how to use in Golang...
These are C++ interfaces. You won't be able to call these directly, without appropriate compiler / linker. I was hoping there is simple C functions that can be accessed inside DLL.

Googling (I know nothing about matlab), I find this http://www.codeproject.com/Articles/5739/MATLAB-Shared-Library Perhaps, if you can build mylib.dll and mylib.h as described, you can look inside mylib.h and create similar Go interface functions, then you should be able to call them into mylib.dll.

Be warned, Go dll call functions do not pass data via float CPU registers.

I can also see people use OLE to access matlab. Perhaps you can try that. https://github.com/mattn/go-ole/ is the only lib I know of - I am not sure how good / bad it is.

Alex

Curtis Paul

unread,
Jan 22, 2014, 1:17:25 AM1/22/14
to golan...@googlegroups.com
I was able to find SetData and mxArray_* functions that appears to do those jobs.

Was able to .Call them...values get passed to them via pointers.  

During Go run, now getting:

panic: invalid memory address or nil pointer dereference
fatal error: panic during malloc

Curtis Paul

unread,
Jan 22, 2014, 1:25:07 AM1/22/14
to golan...@googlegroups.com
Do you mean to say I can't pass float pointer values to DLL .Call ?


On Tuesday, January 21, 2014 10:39:40 PM UTC-7, brainman wrote:

brainman

unread,
Jan 22, 2014, 2:58:45 AM1/22/14
to golan...@googlegroups.com
On Wednesday, 22 January 2014 17:17:25 UTC+11, Curtis Paul wrote:
> I was able to find SetData and mxArray_* functions that appears to do those jobs.
>
> Was able to .Call them...values get passed to them via pointers.
>
> During Go run, now getting:
>
> panic: invalid memory address or nil pointer dereference
> fatal error: panic during malloc

If you have .h file that describes these functions, show it and show your Go program. I will look if you made a mistake. Everything needs to match perfectly. You should start small and check your every step. Use gdb to see where your call is going, and see it returns correctly and with correct parameters.


On Wednesday, 22 January 2014 17:25:07 UTC+11, Curtis Paul wrote:
> Do you mean to say I can't pass float pointer values to DLL .Call ?

No, you can pass pointers. But some DLLs use FPU registers to pass parameters. Go cannot pass and receive parameters in FPU registers.

Alex
Reply all
Reply to author
Forward
0 new messages