Windows 8.1上MySQL的代码块链接器问题

Windows 8.1上MySQL的代码块链接器问题,mysql,c,windows,codeblocks,linker-errors,Mysql,C,Windows,Codeblocks,Linker Errors,我正在尝试使用Windows8.1上代码块13.12IDE中的C连接到MySQL服务器。以下是我的构建选项。 我的代码如下 #ifdef WIN32 #include <windows.h> #include <winsock.h> #pragma warning (disable: 4514 4786) #pragma warning( push, 3 ) #endif #include <stdio.h> #include <m

我正在尝试使用Windows8.1上代码块13.12IDE中的C连接到MySQL服务器。以下是我的构建选项。

我的代码如下

#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);
  }
#ifdef WIN32
#包括
#包括
#杂注警告(禁用:4514 4786)
#pragma警告(推,3)
#恩迪夫
#包括
#包括
#ifndef WIN32
#包括
#恩迪夫
int main(int argc,字符**argv)
{
MYSQL;
if(mysql_init(&mysql)==NULL){
printf(“\n无法初始化MySQL连接”);
出口(1);
}
/*现在,您可以调用任何您喜欢的MySQL API函数*/
if(!mysql\u real\u connect(&mysql,“localhost”,“user”,“password”,“molecules”,0,NULL,0)){
printf(“连接到MySQL失败:错误:%s\n”,
mysql_错误(&mysql));
出口(1);
}
printf(“成功登录数据库”);
mysql\u 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;
}
#包括
#包括
#包括
int main(int argc,字符**argv)
{
MYSQL*conn=MYSQL_init(NULL);
if(!(mysql\u real\u connect(conn,“localhost”,“username”,“password”,“database”,0,NULL,0)))
{
printf(stderr,“%s\n”,mysql_错误(conn));
关闭(康涅狄格州);
出口(1);
}
printf(“连接成功”);
关闭(康涅狄格州);
返回0;
}

我得到了这个:c:\mingw\bin\..\lib\gcc\mingw32\6.3.0\..\crt2.o | | |未定义对
的引用c:\mingw\bin\..\lib\gcc\mingw32\6.3.0\..\crt2.o | |未定义对
的引用| setargv'| | | |错误:ld返回1退出状态| | | | | | | | | | | | | | | | | | |===|