对于如下使用SQLite 的C/C++程序,
#include <stdio.h>
#include <sqlite3.h>
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("test.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}else{
fprintf(stderr, "Opened database successfully\n");
}
sqlite3_close(db);
}
如果直接编译
gcc test_sqlite.cpp -o test_sqlite
会发生如下错误
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
起原因是没有将SQLite的lib链接进来。解决方法如下:
如果你是用的是Xcode一类的IDE,如图所示添加对libsqlite3的引用。
如果你是在terminal使用gcc编译,需要指定sqlite编译选项:
gcc test_sqlite.cpp -l sqlite3
Makefile的方法还在学习中,后续补充。