How to include libsqlite3 in my Haxe/hxcpp build?

386 Views Asked by At

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?

2

There are 2 best solutions below

2
Jonas Malaco On BEST ANSWER

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

<compilerflag value="-lsqlite3"/>

with

<flag value="-lsqlite3"/>

or

<lib base="sqlite3"/>
2
Jeff Ward On

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.
}