1

I have a very simple test put together to include SQLite3 in my Haxe build (I know it has SQLite built in, but this issue doesn't just apply here). It looks like so:

@:include("sqlite3.h")
@:buildXml('<files id="haxe" append="true"><compilerflag value="-lsqlite3"/></files>')
extern class SQLite3 {
    @:native("sqlite3_open") public static function sqlite3_open(path: String, outReference:Reference<DBPointer>):Int;
}

@:include("sqlite3.h")
@:native("sqlite3")
extern class DBPointer {

}

This doesn't throw any Haxe errors, but when I try to compile, I get the following error in C++ compilation:

Undefined symbols for architecture x86_64:
"_sqlite3_open", referenced from:
    Main_obj::main() in aea44ed0_Main.o
ld: symbol(s) not found for architecture x86_64

I had figured that adding the buildXml instructions you can see there would be enough to dynamically reference the macOS SQLite library, but it seems that is not the case.

How can I go about including SQLite here?

Alastair
  • 5,894
  • 7
  • 34
  • 61

2 Answers2

2

According to the hxcpp build XML documentation, I believe you should replace

<compilerflag value="-lsqlite3"/>

with

<flag value="-lsqlite3"/>

or

<lib base="sqlite3"/>
Jonas Malaco
  • 1,557
  • 9
  • 18
  • Been trying to find exactly this kind of documentation forever! Thank you very much. – Alastair Nov 02 '17 at 15:22
  • 1
    Happy to help, @Alastair! I can also recommend that you check Hugh Sanderson's talks on past WWXs ([2015](https://www.youtube.com/watch?v=hltXpZ3Upxg), [2016](https://www.youtube.com/watch?v=1yuMSS5nR4g)) or [this year's Haxe Summit](https://www.youtube.com/watch?v=mTsr38FaMW0). This year's workshop on writing C++ externs was also [recorded](https://www.youtube.com/watch?v=p4kw2QSaSi8). – Jonas Malaco Nov 02 '17 at 15:30
0

I don't know a lot about using CPP external libraries (so this doesn't precisely answer your question), but I do know that an SQLLite implementation is built into Haxe (for the cpp, hl, java, lua, macro, neko, php, and python platforms.) Here're some related documentation:

Here's a snippet (from this full example gist.)

var conn = sys.db.Sqlite.open("test.db");

var rs = conn.request('
  CREATE TABLE IF NOT EXISTS artists_backup
  (
    artistid INTEGER PRIMARY KEY AUTOINCREMENT,
    name NVARCHAR
  );
');

var rs = conn.request('INSERT INTO artists_backup (name) VALUES ("John");');

Note that a ResultSet is an Iterator<Dynamic>, but you can put in a type hint to keep your DB code nice and type-safe:

typedef RecordType = { name:String, id:Int };

for (record in (rs:Iterator<RecordType>)) {
  // While record is still a Dynamic object, the TypeDef alias tells
  // the compiler that .name and .id are the only valid fields.
}
Jeff Ward
  • 16,563
  • 6
  • 48
  • 57
  • I know this is old, but: `sys.db.Sqlite` isn't very good --- no parameterised queries and no prepared statements, which is error-prone and kills performance. I'm having to roll my own for that reason. – David Given Sep 01 '20 at 21:01
  • Here it is: https://github.com/davidgiven/stellation/blob/stellation7/src/runtime/cpp/Sqlite.hx – David Given Sep 02 '20 at 23:39