Need help on go1.8 cgo compilation error

139 views
Skip to first unread message

clashion

unread,
Apr 3, 2017, 8:54:07 PM4/3/17
to golang-nuts
Hi, 

I tried to compile the code from https://github.com/ebfe/scard .

Error during compilation.


Environment: go1.8 , linux


Error Message:


cgo-gcc-prolog:198:2: warning: passing argument 3 of ‘SCardGetStatusChange’ from incompatible pointer type [enabled by default]


/usr/include/PCSC/winscard.h:64:16: note: expected ‘LPSCARD_READERSTATE’ but argument is of type ‘struct *’
PCSC_API LONG SCardGetStatusChange(SCARDCONTEXT hContext,



Need advice how to solve this. Thanks.

Ian Lance Taylor

unread,
Apr 3, 2017, 9:44:41 PM4/3/17
to clashion, golang-nuts
You are using cgo code. This error comes from the C compiler. You
need to use the correct type in your C code.

Ian

clas...@gmail.com

unread,
Apr 3, 2017, 9:59:12 PM4/3/17
to golang-nuts, clas...@gmail.com
I think the correct C type is used. 

Line 57:

func scardGetStatusChange(ctx uintptr, timeout uint32, states []scardReaderState) Error {
r := C.SCardGetStatusChange(C.SCARDCONTEXT(ctx), C.DWORD(timeout), (C.LPSCARD_READERSTATE)(unsafe.Pointer(&states[0])), C.DWORD(len(states)))
return Error(r)

clas...@gmail.com

unread,
Apr 3, 2017, 10:20:44 PM4/3/17
to golang-nuts, clas...@gmail.com
The reason I think correct C type is used is because in the "winscard.h" C header file, the expected type is LPSCARD_READERSTATE.
And , in the golang cgo code, i.e. scard_unix.go, the argument 3 is cast as (C.LPSCARD_READERSTATE).




Further details (not sure if is useful): 
I found that in another C header file (pcsclite.h),  LPSCARD_READERSTATE is defined as:
typedef struct
{
        const char *szReader;
        void *pvUserData;
        DWORD dwCurrentState;
        DWORD dwEventState;
        DWORD cbAtr;
        unsigned char rgbAtr[MAX_ATR_SIZE];
}
SCARD_READERSTATE, *LPSCARD_READERSTATE;


And, on Windows , I could compile the code but failed on linux. 

Ian Lance Taylor

unread,
Apr 3, 2017, 11:05:41 PM4/3/17
to clashion, golang-nuts
On Mon, Apr 3, 2017 at 7:20 PM, <clas...@gmail.com> wrote:
> The reason I think correct C type is used is because in the "winscard.h" C
> header file, the expected type is LPSCARD_READERSTATE.
> And , in the golang cgo code, i.e. scard_unix.go, the argument 3 is cast as
> (C.LPSCARD_READERSTATE).
>
>
>
>
> Further details (not sure if is useful):
> I found that in another C header file (pcsclite.h), LPSCARD_READERSTATE is
> defined as:
> typedef struct
> {
> const char *szReader;
> void *pvUserData;
> DWORD dwCurrentState;
> DWORD dwEventState;
> DWORD cbAtr;
> unsigned char rgbAtr[MAX_ATR_SIZE];
> }
> SCARD_READERSTATE, *LPSCARD_READERSTATE;
>
>
> And, on Windows , I could compile the code but failed on linux.

The Windows code is different anyhow. The file that fails is not
compiled on Windows.

Anyhow, I do see this now. Thanks for the report. Filed
https://golang.org/issue/19832.

It's just a warning, so the immediate workaround is to simply ignore
the problem.

Another workaround would be to modify the Go code to not call
`C.SCardGetStatusChange` directly, but to call a little function,
defined in the C comment, that takes a void* argument and calls the
real C function.

Ian

Nicholas

unread,
Apr 4, 2017, 2:14:06 AM4/4/17
to Ian Lance Taylor, golang-nuts
Got it. Thanks. 
Reply all
Reply to author
Forward
0 new messages