使用Sqlite出現undefined reference to `sqlite3_open’錯誤的解決

來源:互聯網
上載者:User

假設你已經正確編譯和安裝了Sqlite,寫個測試程式來測試:
#include <stdlib.h>
#include <stdio.h>
#include "sqlite3.h"

int main(void)
{
    sqlite3 *db=NULL;
    char *zErrMsg = 0;
    int rc;
    rc=sqlite3_open("test1.db",&db);
    if(rc)
    {
        fprintf(stderr,"Can't open database: %s\n",sqlite3_errmsg(db));
        sqlite3_close(db);
        exit(1);
    }
    else printf("open mydata successfully!\n");
    sqlite3_close(db);
    return 0;
}

用GCC來編譯的時候總是會出現錯誤,編譯的命令如下
gcc -static -o hello -lsqlite3 -L /usr/local/lib -I/usr/local/include hello.c
錯誤資訊如下
/tmp/ccKeKpX9.o(.text+0x37): In function `main':
: undefined reference to `sqlite3_open'
/tmp/ccKeKpX9.o(.text+0x51): In function `main':
: undefined reference to `sqlite3_errmsg'
/tmp/ccKeKpX9.o(.text+0x73): In function `main':
: undefined reference to `sqlite3_close'
/tmp/ccKeKpX9.o(.text+0x9b): In function `main':
: undefined reference to `sqlite3_close'
collect2: ld returned 1 exit status

那麼,恭喜你中招了。錯誤根本不在SQLITE也不在你的程式,而在GCC。Gcc的編譯參數是有順序的。正確的編譯命令是:
gcc -o hello -L /usr/local/lib -I/usr/local/include -static hello.c -lsqlite3

說實話,這麼的一個小問題困擾了我一天的時間!真是菜啊~~~~~~


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.