I'm attempting to use SQLite in a c++ program. My knowledge of C/C++ is limited as I've mostly used Java to this point. I had some classes in college but its been a while and we never covered anything like this. SQLite is written in C. When compiling the program how would you do this? (I have MinGW installed on my windows platform so gcc and g++ are what i use to compile.)
-
3How can one program can include another program? – Nawaz Mar 26 '11 at 16:05
-
1Since we're being technical I'm surprised you didn't ask how you compile a program. I've made a modification to the title to reflect more accuracy in my question. – Kenneth Mar 26 '11 at 16:13
4 Answers
You protect the C headers in your C++ code by
extern "C" {
// your includes here
}
and that should be all---g++
should happily link code from both gcc
and g++
. The extern "C" ...
trick is also used in C++ system headers and many libraries, just look at the headers that came with your g++ installation or some suitable Open Source projects. Here is a Boost example:
edd@max:~$ grep 'extern "C"' /usr/include/boost/date_time/*
/usr/include/boost/date_time/filetime_functions.hpp: extern "C" {
/usr/include/boost/date_time/filetime_functions.hpp: } // extern "C"
edd@max:~$
Edit: Thanks to delnan for an attentive comment---this is from the sqlite3.h header itself:
/*
** Make sure we can call this stuff from C++.
*/
#ifdef __cplusplus
extern "C" {
#endif
so this is of course already taken care of.

- 360,940
- 56
- 644
- 725
-
Note: I wouldn't be surprised if the SQLite headers already had this (along with the `#ifdef __cplusplus` trick to only do this if it's included by a C++ program). – Mar 26 '11 at 16:19
-
Good point I forgot to make: even the **C** system as well as many library headers have that trick in them. – Dirk Eddelbuettel Mar 26 '11 at 16:21
C++ achieves compatibility with C through the use of extern "C"
declarations. There are some good explanations of what extern "C"
means and why it is needed at this SO question: Why do we need extern “C”{ #include } in C++?. Virtually all C-based libraries, including sqlite, provide for automatic C++ compatibility by including extern "C"
in their header files.
Therefore, SQLite will work without any special handling on your part (other than including the header and the library as you normally would for a C or a C++ library)...
/* my_sqlite_program.cpp */
#include <sqlite3.h>
int main()
{
...call sqlite functions...
}
compile with
g++ -Wall -Werror my_sqlite_program.cpp -lsqlite3 -o my_sqlite_program

- 1
- 1

- 51,587
- 17
- 154
- 173
-
does -lsqlite3 refer to the header file that's included or some other file? – Kenneth Mar 26 '11 at 16:58
-
1It is a different file. To use a library from C or C++, you include the header file `sqlite3.h`, which describes the functions, and a library file, which provides the implementations of the functions. Providing `-lsqlite3` on the g++ command-line causes your program to be linked against the library implementation file for sqlite3. The actual library file is some version of `/usr/lib/libsqlite3`, but gcc uses `-lsqlite3` as a shorthand way of specifying that. – Brent Bradburn Mar 26 '11 at 17:08
For SQLite in specific, there's nothing really complicated about it.
If you're using a dynamic or static library, you just include their headers and link against the proper lib files.
If you're including SQLite fully inside your app, you'll need to include all the source files in your project and build them as well, and include the headers as needed (using it as a static/dynamic library might be nicer though).
If you need to use C code in files compiled as C++, Dirk's answer is correct, but that's not needed for SQLite.

- 47,010
- 7
- 103
- 140
-
When I compile my cpp file using g++ i the only errors I get are "undefined reference to ..." errors where the ... part is an sqlite function. I've included the sqlite3.h file... not sure what I'm doing wrong... – Kenneth Mar 26 '11 at 16:25
-
you said to include the headers and link against the proper lib (for static or dynamic library) can you explain further? – Kenneth Mar 26 '11 at 16:26
-
I'm not terribly familiar with g++, but do you have everything included properly, names are correct, necessary files provided to the linker? As for my comment, you need to provide the proper libs to the linker depending on how you want SQLite to be set up (if you've built it as a shared module/dll or if you want it to be included). – ssube Mar 26 '11 at 16:26
How can you call C programs from C++ source code?
By using a facility called as linkage specification provided by the compilers. The specification tells the compiler how to link the source code.
Linkage specification is of the format
extern "Language_Type"
{
}
In your case you can wrap your SQLlite C functions like
extern "C"
{
//SQLite function declarations
}
This should enable you to get it working but Since you are trying to call SQlite c functions from C++, SQLite already provides some wrappers for achieving what you are trying to achieve. Check more details on SQLite website. Also, some open source projects also provide what you want. Check CppSQLite
Hope this helps!

- 202,538
- 53
- 430
- 533