简体   繁体   中英

Compiling of SQlite3 in C++

I compile code this way:

g++ main.cpp -I sqlite3

where sqlite3 is a folder with source files which I received from sqlite-amalgamation-3071100.zip, -I is flag for including sources.

This archive contains : shell.c, sqlite3.c, sqlite3.h, sqlite3ext.h.

This is what I receive:

undefined reference to `sqlite3_open'

The program just contain #include and call of function sqlite3_open(...);


I can compile all fine if I make "sudo apt-get install libsqlite3-dev" and compile program with command

g++ main.cpp -lsqlite3

But I want to solve that problem, because I do not want to have to install some libraries on another computer, I do not have access for that!

  • Step1: compile sqlite3.c to sqlite3.o by gcc
  • Step2: compile your c++ code together with sqlite3.o by g++

My makefile for sqlite shell and c++ api test:

  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

Download the sqlite amalgamation from http://www.sqlite.org/download.html .

  1. Include any reference to sqlite as extern "C", since sqlite is written in C.

  2. Create the sqlite library with "gcc -c sqlite3.c".

  3. Link your program to the newly created library with "g++ main.c sqlite3.o"

You will need to compile sqlite3 with gcc . I tried g++ and the result was hundreds of errors and warnings.

Perhaps sqlite3 shoule be written in such a way that it would compile with a C++ compiler. C++ compilers are much choosier and enforce types and such much better than a C compiler.

在具有MinGW32的Windows上,通过以下方式编译动态链接库:

gcc -shared sqlite3.c -o sqlite3.dll

The following worked for me on Ubuntu:

gcc -o test test.c sqlite3.c -lpthread -idl

  1. I declared #include "sqlite3.h" in source file (test.c) #include did NOT work.
  2. gcc -o test test.c sqlite3.c -lpthread -idl

The reference as stated below:

https://www.sqlite.org/draft/howtocompile.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM