Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 966
Moscow Boy Avatar asked May 13 '12 20:05

Moscow Boy


People also ask

How compile SQLite source code?

First download the preprocessed source code from http://www.sqlite.org/ sqlite_source. zip. This archive contains a set of C source and header files that are ready to be passed to a C compiler. To build the library, just compile all of the files.

What is sqlite3 H?

h: The header files that accompanies sqlite3. c and defines the C-language interfaces to SQLite.

What is SQLite amalgamation?

The amalgamation contains everything an application needs to embed SQLite. Combining all the code for SQLite into one big file makes SQLite easier to deploy — there is just one file to keep track of.


4 Answers

  • 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
like image 109
yc_yuy Avatar answered Oct 02 '22 07:10

yc_yuy


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"

like image 21
Asier Gutierrez Avatar answered Oct 02 '22 07:10

Asier Gutierrez


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.

like image 31
Ewout Boks Avatar answered Oct 01 '22 07:10

Ewout Boks


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

like image 21
Bytes Bytes Avatar answered Oct 03 '22 07:10

Bytes Bytes