0
每當我嘗試編譯一個文件與MySQL包括我得到的錯誤,'未定義的參考'的所有調用我的mysql。我的包裝中有圖書館,我相信我會按照我應有的方式包括它。我不知道到底發生了什麼。爲什麼編譯器不能找到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 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 /'中
真棒,那工作..它究竟做了什麼?你知道爲什麼沒有這個說法,它不工作? – nacy 2015-04-03 23:47:41
我按照你的要求編輯我的答案^^編譯器需要知道「如何」(使用函數;這是#include),鏈接器需要知道「where」(真正的函數;即-l) 。 – mathieu 2015-04-03 23:49:35
正確。謝謝。 – nacy 2015-04-03 23:53:53