CodeBlocks链接器与Windows 8.1上的MySQL的问题

我试图在Windows 8.1的CodeBlocks 13.12 IDE中使用C连接到MySQL服务器。 以下是我的构build选项。 在这里输入图像说明

在这里输入图像说明

我的代码如下。

#ifdef WIN32 #include <windows.h> #include <winsock.h> #pragma warning (disable: 4514 4786) #pragma warning( push, 3 ) #endif #include <stdio.h> #include <mysql.h> #ifndef WIN32 #include <unistd.h> #endif int main(int argc, char **argv) { MYSQL mysql; if(mysql_init(&mysql)==NULL){ printf("\nFailed to initate MySQL connection"); exit(1); } /*now you can call any MySQL API function you like*/ if(!mysql_real_connect(&mysql,"localhost","user","password","molecules",0,NULL,0)){ printf( "Failed to connect to MySQL: Error: %s\n", mysql_error(&mysql)); exit(1); } printf("Logged on to database sucessfully"); mysql_close(&mysql); } 

我收到以下错误。

在这里输入图像说明

任何人都可以请帮我吗?

由于某些原因Codeblocks不喜欢MySQL。 我假设你有MySQL C连接器? 如果没有,你可以在这里得到它

在MySQL C Connector目录中,复制名为Include的文件夹的全部内容,然后进入MinGW目录,并将内容粘贴到MinGW的Include文件夹中。

下载这个包含libmysql.a和libmysql.dll的Zip文件夹。 将libmysql.a解压并复制到MingGW的lib文件夹中。 将libmysql.dll复制到Windows System32和SysWOW64文件夹中。

最后在代码块中,在编译器设置/链接器设置/其他链接器选项中,输入“-lmysql”不带引号。

未在Windows 8.1上测试,在Windows 7上工作。

你有很多不必要的代码来测试连接。

 #include <stdio.h> #include <winsock2.h> #include <mysql.h> int main(int argc, char **argv) { MYSQL *conn = mysql_init(NULL); if (!(mysql_real_connect(conn, "localhost", "username", "password", "database", 0, NULL, 0))) { printf(stderr, "%s\n", mysql_error(conn)); mysql_close(conn); exit(1); } printf ("Connection Succesful"); mysql_close(conn); return 0; }