Thread overview
How To Compile D2SQLite3 on OSX?
Sep 08, 2015
Mike McKee
Sep 08, 2015
Jacob Carlborg
Sep 08, 2015
Mike McKee
September 08, 2015
LOL, I feel like I need to do one of these "Explain like I'm 5" kind of posts you see on Reddit. I'm stuck. I'm on OSX (Yosemite). I seem to have properly installed homebrew, and then dub, and then sqlite3, and I have the dmd D compiler installed and working. Now I'm trying to get D2SQLite3 installed on OSX so that I can interact with a SQLite database. I downloaded and expanded the zip from Github:

https://github.com/biozic/d2sqlite3

...and then cd'd to this directory. I then edited the dub.json file like so:

{
    "name": "d2sqlite3",
    "description": "A thin wrapper around SQLite3",
    "homepage": "https://github.com/biozic/d2sqlite3",
    "authors": ["Nicolas Sicard", "Other contributors: see Github repo"],
    "copyright": "Copyright 2011-15 Nicolas Sicard",
    "license": "BSL-1.0",
    "targetType": "library",
    "libs": ["sqlite3"],
    "systemDependencies": "SQLite version >= 3.8.7",
    "lflags": ["-L/usr/local/Cellar/sqlite/3.8.11.1/lib/libsqlite3.dylib"]
}


...although I'm not quite clear if I did the lflags line properly. I then ran "dub build" and it seems to have created the file: libd2sqlite3.a

Now I don't know what to do with that file. I also created a test.d script to test the SQLite out, but it's complaining of missing symbols, even though I'm importing the right .d files:

import d2sqlite3;
import std.array;
import std.conv;
import std.exception;
import std.file;
import std.path;
import std.stdio;
import std.string;
import std.c.stdlib : exit;
import core.stdc.time : time_t;
import etc.c.zlib;

void main(){

	auto db = Database("test.sqlite");
	auto results = db.execute("SELECT * FROM test");
	while (!results.empty) {
		auto row = results.front;
		writeln(row.peek!string(0));
		results.popFront();
	}

}

When I compile this, it complains of missing the d2SQLite symbols:

$ dmd test.d
Undefined symbols for architecture x86_64:
  "_D9d2sqlite311ResultRange11__fieldDtorMFZv", referenced from:
      __Dmain in test.o
  "_D9d2sqlite311ResultRange5emptyMFNdZb", referenced from:
      __Dmain in test.o
  "_D9d2sqlite311ResultRange5frontMFNdZS9d2sqlite33Row", referenced from:
      __Dmain in test.o
  "_D9d2sqlite311ResultRange8_Payload11__fieldDtorMFZv", referenced from:
      _D42TypeInfo_S9d2sqlite311ResultRange8_Payload6__initZ in test.o
  "_D9d2sqlite311ResultRange8_Payload11__xopEqualsFKxS9d2sqlite311ResultRange8_PayloadKxS9d2sqlite311ResultRange8_PayloadZb", referenced from:
      _D42TypeInfo_S9d2sqlite311ResultRange8_Payload6__initZ in test.o
  "_D9d2sqlite311ResultRange8popFrontMFZv", referenced from:
      __Dmain in test.o
  "_D9d2sqlite312__ModuleInfoZ", referenced from:
      _D4test12__ModuleInfoZ in test.o
  "_D9d2sqlite33Row13internalIndexMFiZi", referenced from:
      _D9d2sqlite33Row13__T4peekTAyaZ4peekMFiZAya in test.o
  "_D9d2sqlite38Database11__fieldDtorMFZv", referenced from:
      __Dmain in test.o
  "_D9d2sqlite38Database6__ctorMFNcAyaiZS9d2sqlite38Database", referenced from:
      __Dmain in test.o
  "_D9d2sqlite38Database7executeMFAyaZS9d2sqlite311ResultRange", referenced from:
      __Dmain in test.o
  "_D9d2sqlite38Database8_Payload6__dtorMFZv", referenced from:
      _D38TypeInfo_S9d2sqlite38Database8_Payload6__initZ in test.o
  "_D9d2sqlite39Statement8_Payload10__aggrDtorMFZv", referenced from:
      _D39TypeInfo_S9d2sqlite39Statement8_Payload6__initZ in test.o
  "_D9d2sqlite39Statement8_Payload11__xopEqualsFKxS9d2sqlite39Statement8_PayloadKxS9d2sqlite39Statement8_PayloadZb", referenced from:
      _D39TypeInfo_S9d2sqlite39Statement8_Payload6__initZ in test.o
  "_sqlite3_column_text", referenced from:
      _D9d2sqlite33Row13__T4peekTAyaZ4peekMFiZAya in test.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
--- errorlevel 1

How do I get this going?

September 08, 2015
On 2015-09-08 07:50, Mike McKee wrote:

> $ dmd test.d
> Undefined symbols for architecture x86_64:
>
> How do I get this going?

You need to link with the library, i.e.

$ dmd test.d libd2sqlite3.a

You probably need to link the native SQLite3 library as well:

$ dmd test.d libd2sqlite3.a -L/usr/local/Cellar/sqlite/3.8.11.1/lib/libsqlite3.dylib

But the correct way to do this is create a dub.json file for your own project which adds "d2sqlite3" as a dependency:

1. Run "dub init foo"
2. Navigate to the newly created directory "foo"
3. Add your source file(s) to "source"
4. Add "d2sqlite3" as a dependency in "dub.json" in the "dependencies" object/dictionary
5. Run "dub" to build and run the project. It will automatically fetch and build the dependencies

You might need to add that linker [2] flag to your dub.json file if the library is not in the standard library path (/usr/lib or /usr/local/lib).

For more information about the dub.json format see [1].

[1] http://code.dlang.org/package-format?lang=json
[2] "lflags": ["-L/usr/local/Cellar/sqlite/3.8.11.1/lib/libsqlite3.dylib"]

-- 
/Jacob Carlborg
September 08, 2015
On Tuesday, 8 September 2015 at 07:02:43 UTC, Jacob Carlborg wrote:
> On 2015-09-08 07:50, Mike McKee wrote:
>
>> [...]
>
> You need to link with the library, i.e.
>
> $ dmd test.d libd2sqlite3.a
>
> [...]

Jacob, you fixed me! :)

Thanks also for explaining the dub thing -- I wasn't quite getting it.