使用 C 程序连接到 mysql 数据库

Connecting to mysql database with c program

我想在 CodeBlocks

中使用 C 连接 mysql 数据库
  1. 我下载了 MySQL Connector C 6.1

  2. 我在下面将其添加到我的链接器设置中

  3. 我在搜索目录中将其添加到编译器

  4. 我将 libmysql.dll 复制到我的项目目录并 /windows/system

  5. 我在我的 hello world 示例中添加了 #include "mysql.h" 并尝试编译它。

     #include <stdio.h>
     #include <stdlib.h>
     #include "mysql.h"    
     int main()
     {
     printf("Hello world!\n");
     return 0;
     }
    

当我尝试编译它时出现以下错误:

cannot find -l-lmysqlpp

cannot find -l-lmysqlclient

如有任何帮助,我将不胜感激。

编辑:

  1. 让我上传我的 test_build_log.html 文件
-------------- Build: Debug in test (compiler: GNU GCC Compiler)---------------
gcc.exe -o bin\Debug\test.exe obj\Debug\main.o "C:\Program Files\MySQL\MySQL Connector C 6.1\lib\libmysql.lib" "C:\Program Files\MySQL\MySQL Connector C 6.1\lib\vs12\mysqlclient.lib" -l-lmysqlpp -l-lmysqlclient
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -l-lmysqlpp
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -l-lmysqlclient
collect2.exe: error: ld returned 1 exit status
cannot find -l-lmysqlpp
cannot find -l-lmysqlclient

这些应该以如下形式呈现给链接器:

-lmysqlpp
-lmysqlclient

重复的 -l 开关表示您的链接器设置有问题。确保 link libraryOther linker options 框中都没有条目(包括空格或其他隐藏字符)。您可能需要清除并重新输入每个框中的所有内容。

再想一想,查看实际使用的编译命令行:
Code::Blocks 可以输出构建日志。 Settings->Compiler and debugger->Global compiler settings->{slide tabs to the right}->Build options tab->Save build log to HTML.打开此功能,然后在下次尝试后查看日志。那里可能有指向问题的地方。