• 使用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

    说实话,这么的一个小问题困扰了我一天的时间!真是菜啊~~~~~~


     

  • 相关阅读:
    关于字节对齐以及内存占用
    关于HandlerThread的分析
    关于栈和队列的相关操作
    自定义控件(View的绘制流程源码解析)
    关于采用github.io搭建个人博客
    算法题解
    关于Android中ArrayMap/SparseArray比HashMap性能好的深入研究
    ADB server didn't ACK * failed to start daemon *
    Handler 、 Looper 、Message
    KMP字符串模式匹配详解(转)
  • 原文地址:https://www.cnblogs.com/qkhh/p/1280175.html
Copyright © 2020-2023  润新知