sqlite is a sweet little answer to database storage for stand-alone applications. CppSQLite is the best C++ wrapper I found (NOTE: there are many). I didn’t find any quick instructions to get the C code statically linked into a Visual Studio 2005 C++ app, so here they are. At the end, I had all the code wrapped nicely into my application with a size increase of 132kb – and no dependencies! Whoop.
You can just grab the files here if you don’t care about the gory details. Read the included Readme.txt file for quick setup instructions. Everything you need is included, including v3.3.13 of the sqlite source and v3.1 of the CppSQLite files.
Now for the blow-by-blow setup of how to do the static library build yourself. It’s not brain surgery, but it’s good to know – for example, if you want to use a newer version of the source code (in case I get lazy and don’t update this).
- create a new Visual Studio 2005 Win32 console application
- during creation, under advanced options…
– change build target type to “static library”
– uncheck “precompiled header” - add NO_TCL to preprocessor definitions
– Go here: Project->Properties->Configuration Properties->C/C++->Preprocessor->Preprocessor Definitions
– Add NO_TCL to the existing lists of all configurations, to bypass inclusion of TCL support - change the runtime library (to match whatever project that will be linking to the sqlite library you build)
– Found here: Project->Properties->Configuration Properties->C/C++->Code Generation->Runtime Library - download the preprocessed source code and add it to the project
- (optional) disable the more neurotic VS8 warnings (even though you can safely ignore them)…
– add _CRT_SECURE_NO_WARNINGS to preprocessor definitions:
– – Go here: Project->Properties->Configuration Properties->C/C++->Preprocessor->Preprocessor Definitions
– – Add _CRT_SECURE_NO_WARNINGS to the existing lists of all configurations - compile!
- include the resulting static library, along with the CppSQLite wrapper class, in your shiny new project
- read about CppSQLite and write some code!
– disable the following warnings (Project->Properties->C/C++->Advanced->Disable Specific Warnings):
– – 4267;4244;4018;4311;4996;4312;4311
Post a comment if you have any troubles. Have fun!
Hi there
I find your tutorial very helpful.But I got hundreds of warnings when I build the project.
“change the runtime library (to match whatever project you will be linking into)”
What do you mean by “change the runtime library”. This is the only step that I am confused about, maybe that is the reason why I got so many warnings.
Thanks
Thanks Liu.
Yes, selecting the right runtime library can be a tricky part. Linking with different runtime libraries in the static lib and in your project can cause lots of linker errors. But since it doesn’t sound like you’re getting link *errors*, it sounds like you’ve done this part correctly.
But compiler warnings are a different subject. Visual Studio 2005 (VS8) does a thorough job of type checking, and also marks many standard C string functions as “unsafe”. This causes code that compiles without warnings in older compilers to generate a ton of warnings in VS8. Specifically, the sqlite code will generate lots of warnings – but the resulting library is fine.
I will try to update the project to reduce the number of warnings. More information is available here.
OK, I disabled the “secure CRT” warnings, along with these:
warning C4244: ‘function’ : conversion from ‘i64’ to ‘int’, possible loss of data
warning C4018: ‘>’ : signed/unsigned mismatch
warning C4311: ‘type cast’ : pointer truncation from ‘HANDLE’ to ‘int’
warning C4996: ‘isatty'(etc.): The POSIX name for this item is deprecated…
warning C4312: ‘type cast’ : conversion from ‘int’ to ‘char *’ of greater size
warning C4311: ‘type cast’ : pointer truncation from ‘char *’ to ‘int’
SQLite_Static_Library – 0 error(s), 0 warning(s) :>
Note that I don’t think the warnings are incorrect. But to make the C code perfectly compliant would require quite a few changes to the sqlite source. Instead of going that route right now, I will rely on the sqlite project’s QA to ensure that none of the warnings are a concern.
can you explaining a little more about the “add NO_TCL to preprocessor definitions” where exactly do I add that?
thanks for your help
Sure piggyg1. I updated the instructions to include the path to preprocessor definitions. Let me know if that doesn’t answer your question.
For linking with different runtime libraries, I tried /MT and /MD options. All of them can build the codes without errors.
So, what option should I use usually?
Thank you.
Hello T.
You should use the option that matches your project. For mine, I build two libraries, one with /MT, for linking to my release configuration of my project, and one with /MD, for linking with my debug configuration.
Said another way, I have a release configuration of the sqlite library and a release configuration of my project that both use /MT, and the release configuration of my project includes the release build of the sqlite library. And I have a debug configuration of both as well.
Make sense?
Hello
Im trying to find the new source files of sqlite 3.3.17 so to make a lib for wince and windows.But in website of sqlite has only the sqlite.c and header.Are these files enough to make a lib and how? is any site available to download the full source code?
Thanks
Hello Manos!
First, browse here.
Then, you can click on the link that says “This ZIP archive contains pure C code for the SQLite library…” As you can see, it’s 576K, and contains all the sqlite source code.
Good luck!
Thank you m!!!
No problem Manos, good luck!
does it work with MFC?
Yes, if you select the MFC libraries when creating both the sqlite library and your project.
i’m a bit confused. do i need to create new project after compiling the library? OR just add wrapper files? I do not see Linker option. thanks,
Yes, that’s what I did. I created two projects, one for sqlite compiled as a library, and another for my application that uses the library.
I suppose you could just embed the sqlite code right into your project, but that’s not the approach explained here.
Hope that helps!
thanks for your prompt reply. i dno’t know what i’m doing wrong.
unexpected end of file while looking for precompiled header. Did you forget to add ‘#include “stdafx.h”‘ to your source?
can you kindly give me a solution file with two projects in it? first is lib project and another with MFC. I’m really stucked! Many Thanks,
aunghmaw@hotmail.com
Hi,
I was trying to build the amalgamation version as a DLL as part of a data access layer DLL. So we don’t intend to export sqlite calls.
I am getting several errors like
error C2440: ‘=’ : cannot convert from ‘void *’ to ‘char *’ sqlite3.cpp 7106
error C2440: ‘initializing’ : cannot convert from ‘SrcList::SrcList_item *’ to ‘vxprintf::SrcList_item *’ sqlite3.cpp 8909
Hey Sachin.
Not sure why you’re getting those errors – can you build the static library, following the above instructions, with the version of the code you have?
m
Aung, that’s a good idea, I’ll try to post that when I get a minute…
UPDATE: Make sure you uncheck “precompiled header” when creating your library project, perhaps that’s why you are getting the “unexpected end of file” error.
m
Hi. I tryed to compile the version 3.4.2. to a static library. And i have some errors there:
1: fatal error C1083: Cannot open include file: ‘unicode/utypes.h’: No such file or directory
2: fatal error C1083: Cannot open include file: ‘tcl.h’: No such file or directory
Also when i tryed to cmpile static library from amalgamation version i have error:
1: error C2133: ‘sqlite3IsIdChar’ : unknown size
I followed your steps and ended up with a huge lib (1445kb) is that normal? what was the size you got? thanks
Hello Miguel, I assume you built a debug version of the library? I would assume the debug version would be around that size.
The release build I did was 132kb.
As recommended above, I would build both, and link the appropriate one to each of your project builds. Make sense?
@ZNick
for version 3.4.2: just remove the files that give you those errors (icu.c and tclsqlite.c) from your visual studio project and the errors are gone.
I just tried compiling the latest release with these instructions and got this warning (which will actually be an error once it’s called):
warning C4013: ‘TryEnterCriticalSection’ undefined; assuming extern returning int
The solution is to add these lines at line 72 in os.h just before the #include :
#define _WIN32_WINNT 0x0400
#define WIN32_LEAN_AND_MEAN
Win64 supported?
Thanks for the tip Dave!
Haluk, I haven’t tried a 64-bit build yet.
The value specified for _WIN32_WINNT doesn’t work with XP, use the following instead:
#define _WIN32_WINNT 0x0502
I’m getting lib files of 2.4M (debug) and 3.0M (release). I set all the environment stuff, even tried to compile the solution you provide, with the same results. Any ideas?
Hey Wouter!
It’s strange that you’re getting a larger lib size for release than debug. I would double-check your project settings. Did you turn off “use precompiled header” in your static library project?
I’m looking for a VS2005-compatible build process for creating loadable modules. I compile the “half” example found in the sqlite3 documentation, and get “The specified procedure could not be found.”
Hi,
We also tried to build the library with all optimizations but we are still getting the size to be around 2.40 MB can you share your project/solution with which you generated the 132 kb library?
Hello Nandakumar.
Take a look at the project that’s included in the zip file:
source + project zip file
sqlite_static_with_wrapper.zip\sqlite\SQLite_Static_Library\SQLite_Static_Library.sln
Compare it to yours and let me know if that answers your question. Good luck!
Hi m,
this is the same project I downloaded from the link above. When I compile this in release mode, the size of the library is around 2.40 mb. Can you share the 132 kb library, may be I can try to use it directly.
Nandakumar, I’d have to rebuild it. My size increase was based on the change in my final executable after I linked and used the static library. Did you get that far? Can you compare the before/after sizes?
Thanks for this nice article?
What should I do to compile a static lib to work on VC6? Can we use the same one which is obtained using VC2005? How do we do with VC6?
Regards
Eugene
Hello Eugene!
I would set up a VC6 static library project. The process should be very similar to the VS2005 steps listed in the post. You won’t need to worry about the (optional) section. Let me know if that works for you!
Has anyone figured out why the library size is so large?
1.9mb for release, and 1.8 for debug.
Nate,
Just link to the library and your app size should not increase by very much.
HTH,
m
Hi,
I’m trying to build your project as it is written but when I click Build all I get 49 errors, all like that:
1>..\..\sqlite-source\alter.c : fatal error C1902: Program database manager mismatch; please check your installation
Any idea what I’m doing wrong?
Regards,
Wiktor
Hey Wiktor!
Sorry to hear about your troubles. Here is an article with a hotfix from Microsoft, sounds like it’s worth a try. Another solution is provided by Microsoft here.
Good luck!
Thanks m! Now it compiles and everything seems to work fine:) the only thing I would like to add is that when using your lib file in Win forms project there should be /clr option chosen.
so cool,i use the way to compile 3.6.16 sucess.
thank you!
will this work for Visual Studio 2008 too?
I cannot make it work, there is this window coming up saying “pleas specify the name of the executable file to be used for the debug session”
FYI, i just compiled 3.7.4 with 11 warnings