CGO undefined reference to `mysql_init@4'

发布于 2021-11-25 18:20:34

https://www.widuu.com/show/golang/382.html

按照你说的,windows上实现的时候,遇到了问题:

报错信息:

D:\go_project\cgo_mysql>go run main.go
# command-line-arguments
C:\Users\ADMINI~1\AppData\Local\Temp\go-build089382058\b001\_x002.o: In function `get_mysql_query':
D:/go_project/cgo_mysql/main.go:24: undefined reference to `mysql_init@4'
D:/go_project/cgo_mysql/main.go:25: undefined reference to `mysql_options@12'
D:/go_project/cgo_mysql/main.go:27: undefined reference to `mysql_real_connect@32'
D:/go_project/cgo_mysql/main.go:29: undefined reference to `mysql_query@8'
D:/go_project/cgo_mysql/main.go:34: undefined reference to `mysql_store_result@4'
D:/go_project/cgo_mysql/main.go:39: undefined reference to `mysql_num_fields@4'
D:/go_project/cgo_mysql/main.go:40: undefined reference to `mysql_fetch_row@4'
D:/go_project/cgo_mysql/main.go:44: undefined reference to `mysql_fetch_field@4'
D:/go_project/cgo_mysql/main.go:50: undefined reference to `mysql_free_result@4'
D:/go_project/cgo_mysql/main.go:51: undefined reference to `mysql_close@4'
D:/go_project/cgo_mysql/main.go:30: undefined reference to `mysql_error@4'
D:/go_project/cgo_mysql/main.go:35: undefined reference to `mysql_error@4'
C:\Users\ADMINI~1\AppData\Local\Temp\go-build089382058\b001\_x002.o: In function `cgo_5dac3ed41015_Cfunc_mysql_close':
/tmp/go-build/cgo-gcc-prolog:61: undefined reference to `mysql_close@4'
C:\Users\ADMINI~1\AppData\Local\Temp\go-build089382058\b001\_x002.o: In function `cgo_5dac3ed41015_Cfunc_mysql_fetch_field':
/tmp/go-build/cgo-gcc-prolog:76: undefined reference to `mysql_fetch_field@4'
C:\Users\ADMINI~1\AppData\Local\Temp\go-build089382058\b001\_x002.o: In function `cgo_5dac3ed41015_Cfunc_mysql_fetch_lengths':
/tmp/go-build/cgo-gcc-prolog:94: undefined reference to `mysql_fetch_lengths@4'
C:\Users\ADMINI~1\AppData\Local\Temp\go-build089382058\b001\_x002.o: In function `cgo_5dac3ed41015_Cfunc_mysql_fetch_row':
/tmp/go-build/cgo-gcc-prolog:112: undefined reference to `mysql_fetch_row@4'
C:\Users\ADMINI~1\AppData\Local\Temp\go-build089382058\b001\_x002.o: In function `cgo_5dac3ed41015_Cfunc_mysql_free_result':
/tmp/go-build/cgo-gcc-prolog:127: undefined reference to `mysql_free_result@4'
C:\Users\ADMINI~1\AppData\Local\Temp\go-build089382058\b001\_x002.o: In function `cgo_5dac3ed41015_Cfunc_mysql_init':
/tmp/go-build/cgo-gcc-prolog:142: undefined reference to `mysql_init@4'
C:\Users\ADMINI~1\AppData\Local\Temp\go-build089382058\b001\_x002.o: In function `cgo_5dac3ed41015_Cfunc_mysql_num_fields':
/tmp/go-build/cgo-gcc-prolog:160: undefined reference to `mysql_num_fields@4'
C:\Users\ADMINI~1\AppData\Local\Temp\go-build089382058\b001\_x002.o: In function `cgo_5dac3ed41015_Cfunc_mysql_query':
/tmp/go-build/cgo-gcc-prolog:179: undefined reference to `mysql_query@8'
C:\Users\ADMINI~1\AppData\Local\Temp\go-build089382058\b001\_x002.o: In function `cgo_5dac3ed41015_Cfunc_mysql_real_connect':
/tmp/go-build/cgo-gcc-prolog:204: undefined reference to `mysql_real_connect@32'
C:\Users\ADMINI~1\AppData\Local\Temp\go-build089382058\b001\_x002.o: In function `cgo_5dac3ed41015_Cfunc_mysql_store_result':
/tmp/go-build/cgo-gcc-prolog:234: undefined reference to `mysql_store_result@4'
C:\Users\ADMINI~1\AppData\Local\Temp\go-build089382058\b001\_x002.o: In function `get_mysql_query':
D:/go_project/cgo_mysql/main.go:52: undefined reference to `mysql_server_end@0'
C:\Users\ADMINI~1\AppData\Local\Temp\go-build089382058\b001\_x002.o: In function `cgo_5dac3ed41015_Cfunc_mysql_server_end':
/tmp/go-build/cgo-gcc-prolog:219: undefined reference to `mysql_server_end@0'
collect2.exe: error: ld returned 1 exit status

Windows-main.go:

package main/*// -Wl,--allow-multiple-definition for multiple definition#cgo CFLAGS: -I${SRCDIR}#cgo LDFLAGS: -Wl,--allow-multiple-definition -L${SRCDIR} -lmysqlclient#include <winsock.h> // for mysql-socket#include <stdio.h> // for c.puts#include "mysql.h"MYSQL *mysql,*res;MYSQL_RES *results;MYSQL_FIELD *field;struct Person {   int id;   char *name;   int chinese;};void get_mysql_query(){   MYSQL_ROW row;   mysql = mysql_init(NULL);   mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");   res = mysql_real_connect(mysql, "127.0.0.1","root","root", "dongbao", 0,NULL,0);   if ( mysql_query(res, "SELECT * FROM users")){      fprintf(stderr,"Query failed (%s)/n",mysql_error(res));      exit(1);   }   if (!(results=mysql_store_result(res))) {      fprintf(stderr,"Couldn't get result from %s/n", mysql_error(res));      exit(1);   }   int number = mysql_num_fields(results);   while((row = mysql_fetch_row(results))){      int i;      for (i = 0; i < number; i++)      {         if((field = mysql_fetch_field(results) )!=NULL){            printf("field_name is : %s , field_value is : %s \n", field->name,row[i]);         }      }   }   mysql_free_result(results);   mysql_close(mysql);   mysql_server_end();   return;}*/import "C"import (   "fmt"   "unsafe")const (   maxSize = 1 << 20)func Pause() {   var str string   fmt.Println("")   fmt.Print("请按任意键继续...")   fmt.Scanln(&str)   fmt.Print("程序退出...")}func main() {   fmt.Println("main start")   C.puts(C.CString(" C MYSQL 函数查询…… "))   C.get_mysql_query()   C.puts(C.CString(" C MYSQL 使用库函数查询…… "))   // 使用C的函数库 初始化 MYSQL *   mysql := C.mysql_init(nil)   // 使用库连接 MYSQL *   C.mysql_real_connect(mysql, C.CString("127.0.0.1"), C.CString("root"), C.CString("root"), C.CString("dongbao"), 0, nil, 0)   // 查询函数 int   C.mysql_query(mysql, C.CString("SELECT * FROM users"))   // 查询结果  MYSQL_RES *   results := C.mysql_store_result(mysql)   // 查询的字段数目  unsigned int   number := C.mysql_num_fields(results)   // 查询结果 char **MYSQL_ROW   row := C.mysql_fetch_row(results)   // 查询的字段结果  MYSQL_FIELD *   field := C.mysql_fetch_field(results)   // 每个字段的内容的长度 unsigned long   sql_lengths := C.mysql_fetch_lengths(results)   lengths := (*[maxSize]uint64)(unsafe.Pointer(sql_lengths))   cfields := (*[maxSize]C.MYSQL_FIELD)(unsafe.Pointer(field))   rowPtr := (*[maxSize]*[maxSize]byte)(unsafe.Pointer(row))   for i := 0; i < int(number); i++ {   length := cfields[i].name_length   fname := (*[maxSize]byte)(unsafe.Pointer(cfields[i].name))[:length]   fmt.Print("field_name is :", string(fname))   fmt.Println(" ,field_value is : ", string(rowPtr[i][:lengths[i]]))   }   // 释放结果   C.mysql_free_result(results)   // 关闭mysql   C.mysql_close(mysql)   C.mysql_server_end()   Pause()}

其他:

已有.h和.a文件在项目根目录下...

查看更多

关注者
0
被浏览
9.1k
1 个回答
widuu
widuu 2021-12-21
站在肩膀上的码农

添加需要的库文件%MySQL安装路径%/lib/下的库文件libmysql.lib和mysqlclient.lib

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览