在C++中编译SQlite3

Mos*_*Boy 16 c++ sqlite compilation g++

我用这种方式编译代码:

g++ main.cpp -I sqlite3
Run Code Online (Sandbox Code Playgroud)

其中sqlite3是我从sqlite-amalgamation-3071100.zip收到的源文件的文件夹,-I是包含源的标志.

该存档包含:shell.c,sqlite3.c,sqlite3.h,sqlite3ext.h.

这是我收到的:

undefined reference to `sqlite3_open'
Run Code Online (Sandbox Code Playgroud)

该程序只包含#include和函数调用sqlite3_open(...);


如果我制作"sudo apt-get install libsqlite3-dev"并使用命令编译程序,我可以编译好

g++ main.cpp -lsqlite3

但我想解决这个问题,因为我不想在另一台计算机上安装一些库,我无法访问它!

小智 11

  • Step1:通过gcc 将sqlite3.c编译为sqlite3.o
  • 第二步:用g ++ 编译你的c ++代码和sqlite3.o

我的sqlite shell和c ++ api测试的makefile:

  1 CXX = g++
  2 cc = gcc
  3 
  4 LIB = -lpthread -ldl
  5 BIN = sqlite apiTest
  6 
  7 all : $(BIN)
  8 sqlite : sqlite3.c shell.c
  9     $(cc) -o $@ $^ $(LIB) 
 10 apiTest : apiTest.cpp sqlite3.o
 11     $(CXX) -o $@ $^ $(LIB) 
 12 sqlite3.o : sqlite3.c
 13     $(cc) -o $@ -c $^
 14 
 15 clean :
 16     rm -f $(BIN)
 17 
 18 .PHONY: all, clean
Run Code Online (Sandbox Code Playgroud)


Asi*_*rez 5

从http://www.sqlite.org/download.html下载 sqlite 合并版本。

  1. 包括对 sqlite 的任何引用作为 extern "C",因为 sqlite 是用 C 编写的。

  2. 使用“gcc -c sqlite3.c”创建 sqlite 库。

  3. 使用“g++ main.c sqlite3.o”将您的程序链接到新创建的库