每当我尝试使用mysql编译一个文件包含我得到错误时,对于我所做的所有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输出:
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 /'
中答案 0 :(得分:0)
尝试clang test.c -o driver -lmysqlclient
可能吗?
#include指令仅供编译器知道如何使用库。您需要在编译中包含二进制文件。