Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SQLite in C++ program using Code::Blocks?

I'm a complete beginner with Code::Blocks and SQLite, and have some basic knowledge with C++. I'm currently using Ubuntu 11.04.

I have downloaded SQLite Amalgamation here. When I extracted the zip file, there are four files inside: shell.c, sqlite3.c, sqlite3.h, and sqlite3ext.h. If I simply add those files to a (for example) a console project, it gives out an error: the .c's of the downloaded sqlite each have their own main function. Removing those from the project, the errors are gone and I can call #include "sqlite3.h". I am trying to follow this, and tried the first two lines of code from here and it gives out an error: undefined reference to sqlite3_open.

I think adding those .h's directly to a console project isn't the right way to use it, though I'm not sure.

How exactly should I use those? What should I do to use those for my C++ program?

Any help is greatly appreciated. :)

EDIT: I also tried to create a .a file of those sqlite files by following this. When I try it, it gives out an error: cannot find -lsqlite.

like image 706
Gerald Torres Avatar asked Jul 04 '26 23:07

Gerald Torres


1 Answers

I got it! Though there was something that I did that caused problems.. I forget to remove the .a file that I added at Project > Build Options > Linker Settings earlier, which caused problems..

Here are the steps I made to add SQLite: (for those that might have the same problems)

  1. Copy the files extracted from the SQLite Amalgamation to the directory of the project.

  2. Add the sqlite files (Project > Add Files) EXCEPT the shell.c (it is the one that causes the multiple function error)

  3. Compile it (Yes, a simple Ctrl+f9).

  4. here are errors: undefined reference to pthread_mutexattr..... These are fixed by going to Project > Build Options > Highlight 'the Project Name' above Debug and Release at the top left corner > Linker settings, and adding "-lpthread" (without quotes) to Other linker options:.

  5. Some more errors are found: undefined reference to dlopen, dlerror..... Add '"-ldl"' just below the '"-lpthread"' added earlier.

DONE :)

like image 184
Gerald Torres Avatar answered Jul 06 '26 19:07

Gerald Torres



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!