Using SQLite to show undefined reference to ` SQLite3_ open’…

Assuming you have compiled and installed Sqlite correctly, write a test program to test.

#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;

}

When compiling with GCC there is always an error, the compile command is as follows

gcc -static -o hello -lsqlite3 -L /usr/local/lib -I/usr/local/include hello.c

The error message is as follows

/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

The error is not in SQLITE nor in your program at all, but in GCC. the compilation parameters of Gcc are in order. The correct compile command is

gcc -o hello -L /usr/local/lib -I/usr/local/include -static hello.c -lsqlite3

Similar Posts: