如何在我的 Haxe/hxcpp 构建中包含 libsqlite3?
How to include libsqlite3 in my Haxe/hxcpp build?
我有一个非常简单的测试,将 SQLite3 包含在我的 Haxe 构建中(我知道它内置了 SQLite,但这个问题不仅仅适用于此)。看起来是这样的:
@: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 {
}
这不会引发任何 Haxe 错误,但是当我尝试编译时,在 C++ 编译中出现以下错误:
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
我认为添加 buildXml
指令就足以动态引用 macOS SQLite 库,但似乎并非如此。
我怎样才能在此处包含 SQLite?
我对使用 CPP 外部库知之甚少(所以这并不能准确回答你的问题),但我知道 Haxe 中内置了一个 SQLLite 实现(对于 cpp、hl、java、lua、macro、neko、php 和 python 平台。)这里有一些相关文档:
- https://api.haxe.org/sys/db/Sqlite.html
- https://api.haxe.org/sys/db/Connection.html
- https://api.haxe.org/sys/db/ResultSet.html
这是一个片段(摘自 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");');
请注意 ResultSet
是 Iterator<Dynamic>
,但您可以输入类型提示以保持数据库代码良好且类型安全:
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.
}
根据hxcpp build XML documentation,我相信你应该更换
<compilerflag value="-lsqlite3"/>
与
<flag value="-lsqlite3"/>
或
<lib base="sqlite3"/>
我有一个非常简单的测试,将 SQLite3 包含在我的 Haxe 构建中(我知道它内置了 SQLite,但这个问题不仅仅适用于此)。看起来是这样的:
@: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 {
}
这不会引发任何 Haxe 错误,但是当我尝试编译时,在 C++ 编译中出现以下错误:
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
我认为添加 buildXml
指令就足以动态引用 macOS SQLite 库,但似乎并非如此。
我怎样才能在此处包含 SQLite?
我对使用 CPP 外部库知之甚少(所以这并不能准确回答你的问题),但我知道 Haxe 中内置了一个 SQLLite 实现(对于 cpp、hl、java、lua、macro、neko、php 和 python 平台。)这里有一些相关文档:
- https://api.haxe.org/sys/db/Sqlite.html
- https://api.haxe.org/sys/db/Connection.html
- https://api.haxe.org/sys/db/ResultSet.html
这是一个片段(摘自 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");');
请注意 ResultSet
是 Iterator<Dynamic>
,但您可以输入类型提示以保持数据库代码良好且类型安全:
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.
}
根据hxcpp build XML documentation,我相信你应该更换
<compilerflag value="-lsqlite3"/>
与
<flag value="-lsqlite3"/>
或
<lib base="sqlite3"/>