mysql - Windows 8.1 上 MySQL 的 CodeBlocks 链接器问题

标签 mysql c windows codeblocks linker-errors

我正在尝试在 Windows 8.1 的 CodeBlocks 13.12 IDE 中使用 C 连接到 MySQL 服务器。以下是我的构建选项。 enter image description here

enter image description here

我的代码如下。

#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);
  }

我收到以下错误。

enter image description here

有人能帮帮我吗?

最佳答案

出于某种原因,Codeblocks 不喜欢 MySQL。我假设你有 MySQL C 连接器?如果没有你可以得到它here

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

下载this包含 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;
}

关于mysql - Windows 8.1 上 MySQL 的 CodeBlocks 链接器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32444019/

相关文章:

windows - 如何使用 PowerShell 2.0 递归删除整个目录?

c# - 如何打印电脑唤醒源?

mysql - 如何在mysql中按小时汇总

c - 是否有在 Windows 上的 C 中使用 winhttp 的完整示例?

c - 简单的C语言代码问题

c - 是否可以确定 pthread_t 结构是否未使用?

c++ - 如何使 Microsoft VCC 在出现第一个构建错误时崩溃?

php - HTML 单选按钮未显示在 $_POST 中

mysql - 不能链接两个表?

php - 自动将 Excel 表格中的产品图像和描述包含在 MySQL 数据库中