www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Interfacing SQLite with DLang is very easy - why?

reply Selim Ozel <sozel wpi.edu> writes:
Hello everyone,

I started to learn basics of SQLite last week for my small side 
project & I was quite impressed with its form factor. Long story 
short, I decided to call it directly from D Language after seeing 
"This one simple trick allows D programmer use llama.cpp, rust 
programmers hate him!" [1].

It simply worked with amalgamated sqlite files! What I did was to 
create a single line of code include file and import it to my D 
Language application, which is a port of the reference C 
Application in SQLite references [2]. I also compiled the static 
library using gcc & linked it together with the D Language 
binaries. Guess what? It worked.

I am still scratching my head about how I did this and if I made 
any mistakes because it feels quite un-natural to be able to link 
together an output compiled by gcc with dmd. If someone could 
please explain how & why this works, I'd greatly appreciate it!

I wrote a small blog post on how to replicate my work & source 
files are on my github if anyone's interested in this.

Best wishes,
Selim

[1] 
https://forum.dlang.org/post/qxctappnigkwvaqakeqf forum.dlang.org
[2] https://www.sqlite.org/quickstart.html
[3] https://substack.com/ safepine/p-148370802
[4] https://github.com/SelimOzel/dlang-sqlite-interface/
Sep 01 2024
next sibling parent Vladimir Marchevsky <vladimmi gmail.com> writes:
On Sunday, 1 September 2024 at 16:18:47 UTC, Selim Ozel wrote:
 I am still scratching my head about how I did this and if I 
 made any mistakes because it feels quite un-natural to be able 
 to link together an output compiled by gcc with dmd. If someone 
 could please explain how & why this works, I'd greatly 
 appreciate it!
https://dlang.org/spec/interfaceToC.html https://dlang.org/spec/importc.html Quite natural, actually.
Sep 01 2024
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Sunday, 1 September 2024 at 16:18:47 UTC, Selim Ozel wrote:

 I am still scratching my head about how I did this and if I 
 made any mistakes because it feels quite un-natural to be able 
 to link together an output compiled by gcc with dmd. If someone 
 could please explain how & why this works, I'd greatly 
 appreciate it!
D has been ABI compatible with C from the beginning. It uses the same object file formats and the same linkers that C compilers use. The earliest D users, at a time when there were no D libraries, could write their programs in D and incorporate the existing C ecosystem. A lot of the modules people shared back then were just bindings to C libraries.
Sep 01 2024
parent Selim Ozel <sozel wpi.edu> writes:
On Monday, 2 September 2024 at 02:04:59 UTC, Mike Parker wrote:
 On Sunday, 1 September 2024 at 16:18:47 UTC, Selim Ozel wrote:

 I am still scratching my head about how I did this and if I 
 made any mistakes because it feels quite un-natural to be able 
 to link together an output compiled by gcc with dmd. If 
 someone could please explain how & why this works, I'd greatly 
 appreciate it!
D has been ABI compatible with C from the beginning. It uses the same object file formats and the same linkers that C compilers use. The earliest D users, at a time when there were no D libraries, could write their programs in D and incorporate the existing C ecosystem. A lot of the modules people shared back then were just bindings to C libraries.
Wow, I started using D only five years ago & genuinely didn't know this. Amazing. Thank you.
Sep 01 2024