mysql - 为什么编译器找不到 mysql 包含?

标签 mysql c mariadb

每当我尝试使用 mysql include 编译文件时,我都会收到错误,我对 mysql 进行的所有调用都出现“ undefined reference ”。我在我的包含中包含了该库,并且我相信我正在按照我应该的方式包含它。我不知道到底发生了什么事。

这是我正在尝试的测试文件:

#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>


int main(void) {
    MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW row;
    char *server = "localhost";
    char *user = "????";
    char *pass = "????";
    char *db = "TEST_DB";

    conn = mysql_init(NULL);

    if(!mysql_real_connect(conn, server, user, pass, db, 0, NULL, 0))
        fprintf(stderr, "%s\n", mysql_error(conn));
    if(!mysql_query(conn, "show tables"))
        fprintf(stderr, "%s\n", mysql_error(conn));

    res = mysql_use_result(conn);
    printf("MySQL Tables in mysql DB: \n");

    while((row = mysql_fetch_row(res)) != NULL)
        printf("%s \n", row[0]);

    mysql_free_result(res);
    mysql_close(conn);
  return 0;
}

这是我正在尝试的简单编译器命令,以及操作的输出:

clang test.c -o driver

铿锵输出:

clang version 3.6.0 (tags/RELEASE_360/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.2
Found candidate GCC installation: /usr/lib64/gcc/x86_64-unknown-linux-gnu/4.9.2
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2
Candidate multilib: .;@m64
Selected multilib: .;@m64

"/usr/bin/ld" --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o driver /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../lib64/crt1.o /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../lib64/crti.o /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/crtbegin.o -L/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2 -L/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../lib64 -L/usr/bin/../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../.. -L/usr/bin/../lib -L/lib -L/usr/lib /tmp/ellipsis/test-47b79a.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/crtend.o /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../lib64/crtn.o

/tmp/ellipsis/test-47b79a.o: In function `main':
test.c:(.text+0x4f): undefined reference to `mysql_init'
test.c:(.text+0x8d): undefined reference to `mysql_real_connect'
test.c:(.text+0xb1): undefined reference to `mysql_error'
test.c:(.text+0xdf): undefined reference to `mysql_query'
test.c:(.text+0x102): undefined reference to `mysql_error'
test.c:(.text+0x126): undefined reference to `mysql_use_result'
test.c:(.text+0x147): undefined reference to `mysql_fetch_row'
test.c:(.text+0x180): undefined reference to `mysql_free_result'
test.c:(.text+0x189): undefined reference to `mysql_close'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

C mysql 库存在于“/usr/include/mysql/”中

最佳答案

尝试clang test.c -o driver -lmysqlclient也许? #include 指令仅供编译器了解如何使用该库。您需要将二进制文件包含在您的编译中。

关于mysql - 为什么编译器找不到 mysql 包含?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29441159/

相关文章:

mysql - 具有 MEMORY 存储引擎的 MySQL 数据库是否会持久保存到磁盘上?

c - 使用 GMP 类型 (mpf_t/mpz_t) 作为哈希表中的键

c - 我应该删除 `else` 中不必要的 `else if` 吗?

mysql - 几乎或半空的列是否与另一个表相同或更好?如何编写快速或节省空间的sql?

mysql - 使用 SQL 将 XML 导入 phpMyAdmin

php - 如何使用 Foreach 在 PHP 中显示 MYSQL 全文查询的输出

c++ - 如何识别C或C++

mysql - 远程到IP有端口的MariaDB服务器

javascript - 基于分层(父子)数据的数据驱动面包屑

php - 根据类别列出子类别 - 从 mysql 拉取