开发者

What is the easiest way to use Sqlite in D program on Ubuntu?

I suppose using phobos.etc.c.sqlite3 binding. Compiling sqlite3.c using a C compiler to make a .o file and then link it with my program.

Which C compiler should I use, and what compiler flags? Is it possible link the sqlite3.o with DMD in one step, without calling linker separately?

Or is there some other even easier way?

Answer: How to get Sqlite going with D on 64bit Ubuntu

  1. install sqlite dev sudo apt-get install libsqlite3-dev

  2. compile dmd test.d -L-ldl -L/usr/lib/x86_64-linux-gnu/libsqlite3.a

test.d开发者_如何学JAVA

import std.stdio, std.string, etc.c.sqlite3;

void main () {
    sqlite3* db;
    auto ret = sqlite3_open (toStringz("mydb.s3db"), &db);
    writeln (ret);
}

-ldl switch was needed because of sqlite3 linking problems


As long as you have the sqlite3 development package installed, you can just call dmd test.d -L-lsqlite3 -- no need for an absolute path.

A nice alternative is the lib pragma:

pragma(lib, "sqlite3");

import std.stdio, std.string, etc.c.sqlite3;

void main () {
    sqlite3* db;
    auto ret = sqlite3_open (toStringz("mydb.s3db"), &db);
    writeln (ret);
}

With that in place, you can just say dmd test.d.

I can't reproduce your issue with -ldl, but that could also be added as a pragma directive.


You can use the binding with an available sqlite library (of an appropriate version, certainly), without having to manually compile it to an object file. Just like what you would have done in C: you'd #include <headers> and add -llibrary to compiler flags. The same here — import, and a link directive.

EDIT:

On Ubuntu you can install precompiled sqlite using the following command:

sudo apt-get install libsqlite3-dev

Also, see http://prowiki.org/wiki4d/wiki.cgi?DatabaseBindings#SQLite for some other sqlite binding variants.


Latest phobos contains a quite up-to-date SQLite binding now.

See phobos/etc/c/sqlite3.d

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜