native extension Dart native extension Dart dart dart

native extension Dart


After playing around some with your code and getting the postgresql-dev-9.1 package installed this is where I am. Currently it still does not run, however it is due to a linking error not due to the import itself.

Note a change to your C++ file: I renamed your initialization function from: psql_dart_Init to just psql_Init

// libpsql.cc#include <string.h>#include <stdio.h>#include <libpq-fe.h>#include "dart_api.h"Dart_NativeFunction ResolveName(Dart_Handle name, int argc);DART_EXPORT Dart_Handle psql_Init(Dart_Handle parent_library) {  if (Dart_IsError(parent_library)) return parent_library;  Dart_Handle result_code =      Dart_SetNativeResolver(parent_library, ResolveName);  if (Dart_IsError(result_code)) return result_code;  return Dart_Null();}Dart_Handle HandleError(Dart_Handle handle) { if (Dart_IsError(handle)) Dart_PropagateError(handle); return handle;}void Connect(Dart_NativeArguments args) {    Dart_EnterScope();    PGconn *conn;    const char *conninfo = "user=postgres;password=postgres;host=localhost;port=5432;dbname=reviewdb";    conn = PQconnectdb(conninfo);    /* Check to see that the backend connection was successfully made */    if (PQstatus(conn) != CONNECTION_OK)    {        fprintf(stderr, "Connection to database failed: %s",                PQerrorMessage(conn));        PQfinish(conn);    }  Dart_Handle result = HandleError(Dart_NewInteger((int64_t) conn));  Dart_SetReturnValue(args, result);  Dart_ExitScope();}Dart_NativeFunction ResolveName(Dart_Handle name, int argc) {  assert(Dart_IsString8(name));  const char* cname;  Dart_Handle check_error = Dart_StringToCString(name, &cname);  if (Dart_IsError(check_error)) Dart_PropagateError(check_error);  Dart_NativeFunction result = NULL;  if (strcmp("Connect", cname) == 0) result = Connect;  Dart_ExitScope();  return result;}

And the following is my 1st dart file:

// psql.dart#library("psql");#import("dart-ext:psql");class Database {  var mDb;  var mUser;  var mDbname;  var mPasswd;  var mHost;  var mPort;  var mTable;  //String toString() => "<PostgreSQL: $user@$_host:$_port/$_table>";  Database(host,user,passwd,dbname) : this.mUser = user, this.mHost = host, this.mPasswd = passwd, this.mDbname = dbname  {    mDb = _connect(host,user,passwd,dbname);  }}_connect(host,user,passwd,dbname) native 'Connect';

And then the actual VERY minimal application (command line instead of dartium-based) to test it.

// test.dart#import('psql.dart');main() {  var database = new Database('localhost', 'mbutler', 'test', 'test');  if(database != null) {    print('worked?');  }}

I used the following command to compile and link in one go and I it does work correctly. I segfault because I don't have a valid database to connect to but the following does load the native library properly:

g++ -O2 -DDART_SHARED_LIB -I/home/<user>/dart/dart-sdk/include -rdynamic -fPIC -shared libpsql.cc -lpq -I/usr/include/postgresql -o libpsql.so

(Thanks to dart-sqlite build script was able to piece together the linking I required)