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

Why compiler isn't finding mysql includes?

每当我尝试编译包含 mysql 的文件时,我都会收到错误,'undefined reference' 对于我对 mysql 的所有调用。我在我的包含中有图书馆,我相信我以我应该的方式包含它。我不知道到底发生了什么。

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

#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 输出:

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 指令仅供编译器了解库的使用方式。您需要在编译中包含二进制文件。