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

試著忘記壹切 提交于 2019-12-10 13:38:56

问题


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

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


回答1:


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.




回答2:


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.




回答3:


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

See phobos/etc/c/sqlite3.d



来源:https://stackoverflow.com/questions/6664109/what-is-the-easiest-way-to-use-sqlite-in-d-program-on-ubuntu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!