主题 : 使用Sqlite出现undefined reference to `sqlite3_open'错误的解决 复制链接 | 浏览器收藏 | 打印
好好学习ARM 找个好工作
级别: 新手上路
UID: 20271
精华: 0
发帖: 19
金钱: 95 两
威望: 19 点
贡献值: 0 点
综合积分: 38 分
注册时间: 2010-04-29
最后登录: 2018-02-08
楼主  发表于: 2012-09-14 13:42

 使用Sqlite出现undefined reference to `sqlite3_open'错误的解决

假设你已经正确编译和安装了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