How to compile SQLite (System.Data.SQLite) on Linux / Mono using XBuild.

Background:

(If you want to read it)

Ok. So I do a lot of development with .Net for computer forensics and I have come to really dislike Windows as a development platform (it could be the worst OS of all time). I have become extremely comfortable and prefer a well personalized VIM setup for doing my development. I guess I’m currently left with that I’m a “Command Line Pwns” kinda guy. I forced myself to learn it, and now… well I think it pays dividends.

As a result I consistently try to work less and less in Windows and more and more in Linux but one of the biggest stumbling blocks for me is that our application is .Net and we haven’t spent much time (only me) compiling and running in Linux. I have gotten it to work a few different times, but for some reason or another I would walk away from making it work. SQLite has been one of the biggest pain in the rumps ever. Every new version (because I start this process over and redownload the latest) requires me to compile this thing, or try to work it into the solution, and its always, always been a serious pain. The code works great, zero documentation. (Thanks). I finally found out how to compile and get running the latest SQLite .NET (it has changed recently) and it drove me to create this blog to get it out there, so somebody else could not waste 2 or 3 days trying to do it. Nowhere on their site today has the information to build and deploy a Mono / Linux setup, except a slightly cryptic response in a trouble ticket. (How nice). Also I digged around through some of the text files and eventually found out why things weren’t working.

Effectively now you’ll see that they’re pushing to not have a consolidated dll that has the native C++ sqlite code attached to it. As of Sept 26, 2013, if you go to the main System.Data.SQLite website http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki you will see the explanation of how to setup your solution / output for this to work. This deals with x86 and x64 deployed solutions. There is this website that some build instructions on it http://system.data.sqlite.org/index.html/doc/trunk/www/build.wiki . Nearly none of this website makes much sense to me because it wasn’t written with Mono or Linux in mind. Terrific. Note all of the MSBuild comments. There is no real indications about anything else going on with all the projects in the source (there is alot).

How System.Data.SQLite (SQLite) is Setup

Ok so this is what you need to know regarding a Windows and Linux System.Data.SQLite deployment.

Windows:

You need System.Data.SQLite.dll and you need two directories x86 and x64. These have SQLite.Interop.dll which is pre-compiled for the two platforms. This provided to you on the website, and I won’t go any further than that.

Linux:

You will end up with ONLY System.Data.SQLite.dll and the native sqlite3 binary. (You can get sqlite3 however you please, but it will eventually end up in your output directory for your product).

What and How to Build:

Ok, so immediately do not attempt to build ANY of the projects in the downloaded source. There is only ONE project you need to worry about. In the folder System.Data.SQLite you will find the various versions of System.Data.SQLite.20XX.csproj I can’t comment about them, but i used 2010 in this case. This is the ONLY project you need to get to work. (This is the managed only dll).

You will not be building any other project. (I guess the Linq can be built, but I don’t use it. I tried just now and hit some compiling errors trying to find System.Data.Entity, entity framework, why its not in the gac i have no idea, don’t care).

It is important not to waste your time building anything else, because it WON”T BUILD. They’re using Visual Studio with C++ projects and all sorts of other junk. Don’t even try bringing the solution into monodevelop. You’ll want to shoot yourself twice. (Once for using monodevelop and 2nd because the solution is so totally messed up up when its imported).

Don’t just try and run xbuild on the project because you need to have some added parameters that change whats put into this dll. You’ll need the following args:

/p:UserInteropDll=false

/p:UseSqliteStandard=true

The first means to not use the SQLite.Interop.dll and the 2nd makes it want to use sqlite3 (inside your executables directory). So this is a basic xbuild statement:

xbuild /p:Configuration=Release /p:UseInteropDll=false /p:UseSqliteStandard=true /home/user/sqlite_src/System.Data.SQLite/System.Data.SQLite.2010.csproj

 

Deployment

Now that you have your binaries you just need to drop them in the directory of your application and that’s it. For deploying beyond your own system you’ll need to account for all these things, (moving sqlite3 over, using xbuild on the System.Data.SQLite source, then moving the results over). This could easily be built into a solution I think, but that’s not what this little tutorial is about, since I needed to do it by hand.

Hopefully this helps at least ONE of you out there because it took me way too long to get this to compile because they’ve made the solution overly complex (in my opinion) and mono is an after thought (as is everything else .NET it would appear).

12 thoughts on “How to compile SQLite (System.Data.SQLite) on Linux / Mono using XBuild.

    • Definitely.
      All you are really doing is compiling SQLite without the native code intact leaving you to provide the C++ binary. How exactly you’ll need to have your resulting directory I don’t know because I haven’t tried it, but it would seem that mono/sqlite has no problem finding a binary named “sqlite” inside the library’s directory.

      • I built System.Data.SQLite.dll for OSX according to your instructions. When I use this built assembly I find it will fail when call some native APIs like sqlite3_create_disposable_module. In managed code, they are marked by INTEROP_VIRTUAL_TABLE. While sqlite3.dylib on OSX does not implement these APIs.

        Should we also disable INTEROP_VIRTUAL_TABLE too?

  1. You deserve a medal for this!
    After about 2 hours of Internet search, this page helped me to contribute to a friend’s .NET project.

  2. This was a huge help for me. I had some C# code that used System.Data.SQLite on Windows. This helped me get it working on Linux in about 30 minutes. Thank you!

  3. Thanks for sharing this; regarding your choice of the 2010 source code … did you choose this version because it best matched your version of Linux Mono Compiler? … Or was it the most current source code available at the time to you? Best Regards;

Leave a comment