windows下使用confluent-kafka-go包报错undefined: kafka.Producer
报错undefined: kafka.Producer
打开ide终端
执行go env -w CGO_ENABLED=1
继续报错报错
cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in %PATH%
安装gcc
下载
*gcc官网下载:
下载README.rst.old,找到红框后面的下载地址
下载完成后,解压
添加环境变量
右键此电脑—>属性—>高级系统设置—>环境变量
找到下面的系统变量,新增环境变量,值就是上面解压后的bin文件的路径,例如D:\mingw64\bin
验证
cmd打开命令提示符,输入gcc -v
然后继续报错
D:\software\go\pkg\tool\windows_amd64\link.exe: running gcc failed: exit status 1
C:/Users/Administrator/go/pkg/mod/github.com/confluentinc/confluent-kafka-go@v1.9.2/kafka/librdkafka_vendor/librdkafka_windows.a(rdkafka_error.c.obj):(.text+0x5ca): undefined reference to `__imp__wassert'
C:/Users/Administrator/go/pkg/mod/github.com/confluentinc/confluent-kafka-go@v1.9.2/kafka/librdkafka_vendor/librdkafka_windows.a(rdkafka_error.c.obj):(.text+0x63d): undefined reference to `__imp__wassert'
C:/Users/Administrator/go/pkg/mod/github.com/confluentinc/confluent-kafka-go@v1.9.2/kafka/librdkafka_vendor/librdkafka_windows.a(rdkafka_error.c.obj):(.text+0x700): undefined reference to `__imp__wassert'
C:/Users/Administrator/go/pkg/mod/github.com/confluentinc/confluent-kafka-go@v1.9.2/kafka/librdkafka_vendor/librdkafka_windows.a(rdkafka_error.c.obj):(.text+0xf89): undefined reference to `__imp__wassert'
C:/Users/Administrator/go/pkg/mod/github.com/confluentinc/confluent-kafka-go@v1.9.2/kafka/librdkafka_vendor/librdkafka_windows.a(rdkafka_error.c.obj):(.text+0x10d5): undefined reference to `__imp__wassert'
C:/Users/Administrator/go/pkg/mod/github.com/confluentinc/confluent-kafka-go@v1.9.2/kafka/librdkafka_vendor/librdkafka_windows.a(rdkafka_error.c.obj):(.text+0x1158): more undefined references to `__imp__wassert' follow
collect2.exe: error: ld returned 1 exit status
解决办法:找到confluent-kafka-go包下的build_windows.go文件
**将 -lmsvcr110 增加到 **\#cgo LDFLAGS: ${SRCDIR}/librdkafka_vendor/librdkafka_windows.a -lws2_32 -lsecur32 -lcrypt32
后面
再次运行代码即可
如果编译成功后,如果提示找不到 msvcr120d.dll, 下载个放到 C:\Windows\System32 下即可