c.h
typedef signed int VarInt32;
typedef struct {
VarInt32 VI32
} VAR_A, *VAR_B
int Add(VAR_B v_b) //the argument is a pointer
var mvar unsafe.Pointer
C.Add((*C.VAR_B)(&mvar))
fmt.Println(mvar)
รับผลลัพธ์ดังนี้:
warning: passing argument 1 of ‘add’ from incompatible pointer type [enabled by default]
note: expected ‘MVAR’ but argument is of type ‘struct <anonymous> *’
ดูเหมือนไม่ถูกต้อง
ฉันกำลังมองหาสิ่งนี้ใน golang: mvar.VI32
ขอบคุณ
ปวดหัวมาก ไม่เก่ง C/C++
นี่คือฉากของฉันเพื่อเรียก SDK (พร้อม .so libs)
c.h
typedef char* MPChar;
typedef MLong MRESULT;
typedef struct{
MPChar startTime;
MPChar endTime;
MPChar platform;
MPChar sdkType;
MPChar appId;
MPChar sdkKey;
MPChar sdkVersion;
MPChar fileVersion;
}ASF_ActiveFileInfo, *LPASF_ActiveFileInfo;
MRESULT ASFGetActiveFileInfo(
LPASF_ActiveFileInfo activeFileInfo
);
ฉันพยายามเรียกใช้ฟังก์ชัน ASFGetActiveFileInfo ใน golang เช่นนี้:
var c_afInfo C.ASF_ActiveFileInfo
C.ASFGetActiveFileInfo(&c_afInfo)
//get the same error
//C.ASFGetActiveFileInfo((C.LPASF_ActiveFileInfo)(unsafe.Pointer(&c_afInfo)))
รับข้อผิดพลาดนี้:
# command-line-arguments
cgo-gcc-prolog: In function ‘_cgo_8c613494cf5f_Cfunc_ASFGetActiveFileInfo’:
cgo-gcc-prolog:52:2: warning: passing argument 1 of ‘ASFGetActiveFileInfo’ from incompatible pointer type [enabled by default]
In file included from ./main.go:7:0:
./inc/arcsoft_face_sdk.h:104:9: note: expected ‘LPASF_ActiveFileInfo’ but argument is of type ‘struct <anonymous> *’
MRESULT ASFGetActiveFileInfo(
^
# command-line-arguments
./main.go:134:139: cannot use _cgo0 (type *_Ctype_LPASF_ActiveFileInfo) as type *_Ctype_struct___0 in argument to _Cfunc_ASFGetActiveFileInfo
พยายาม:
var c_afInfo C.LPASF_ActiveFileInfo
C.ASFGetActiveFileInfo((C.LPASF_ActiveFileInfo)&c_afInfo)
รับข้อผิดพลาดนี้
# command-line-arguments
cgo-gcc-prolog: In function ‘_cgo_061c0167bf6c_Cfunc_ASFGetActiveFileInfo’:
cgo-gcc-prolog:52:2: warning: passing argument 1 of ‘ASFGetActiveFileInfo’ from incompatible pointer type [enabled by default]
In file included from ./main.go:7:0:
./inc/arcsoft_face_sdk.h:104:9: note: expected ‘LPASF_ActiveFileInfo’ but argument is of type ‘struct <anonymous> *’
MRESULT ASFGetActiveFileInfo(
^
# command-line-arguments
./main.go:134:25: type _Ctype_LPASF_ActiveFileInfo is not an expression
มีวิธีแก้ไขปัญหานี้หรือไม่?
ขอบคุณสำหรับคำตอบของ @peterSO มันใช้งานได้ แต่ยังคงแสดงข้อผิดพลาด:
cgo-gcc-prolog: In function ‘_cgo_73061f0a5639_Cfunc_ASFGetActiveFileInfo’:
cgo-gcc-prolog:98:2: warning: passing argument 1 of ‘ASFGetActiveFileInfo’ from incompatible pointer type [enabled by default]
In file included from ./main.go:7:0:
./inc/arcsoft_face_sdk.h:104:9: note: expected ‘LPASF_ActiveFileInfo’ but argument is of type ‘struct <anonymous> *’
MRESULT ASFGetActiveFileInfo(
^
0 linux
ความจริงอยู่ใกล้มาก
struct
ที่ไม่ระบุชื่อใน C: แทนtypedef struct /*anonymous*/ { ... } typename_list
ให้ใช้struct struct_name { ... }
เสมอ (Typedefs ใน C ค่อนข้างจะใช้งานไม่ได้ และฉันขอแนะนำให้หลีกเลี่ยงมัน แต่ก็สามารถใช้ได้หากคุณชอบมันจริงๆ การมีอยู่หรือไม่มี typedef ที่นี่ไม่เกี่ยวข้องกันจริงๆ สิ่งสำคัญคือการใช้แท็ก struct) - person torek   schedule 16.12.2019