1

This Stackexchange may not be the best place for this question but I will start here.

I am trying to create a spatially-enabled, sqlite3 database. The following code is a short example of what I am trying to do.

#include <sqlite3.h>
#include <iostream>

//extern InitSpatialMetaData; // Always is an incomplete type no matter which spatialite headers I include.

int main()
{
  sqlite3* db;
  char* errMsg;
  int rc {0};

  rc = sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, nullptr);
  rc = sqlite3_exec(db, "SELECT InitSpatialMetaData();", nullptr, 0, &errMsg);
  if (rc != SQLITE_OK)
  {
    std::cout << "CreateSpatialMetaData error: " << errMsg << std::endl;
    sqlite3_free(errMsg);
  }
  sqlite3_close(db);
  return 0;
}

I have tried compiling with the following:

g++ example.cpp --std=c++11 -lspatialite -lsqlite3

g++ example.cpp --std=c++11 `pkg-config --libs spatialite` -lsplite3

g++ example.cpp --std=c++11 -Wl,--whole-archive -lspatialite -lsqlite3 -Wl,--no-whole-archive

Everything will compile and link. But at runtime the following error appears:

CreateSpatialMetaData error: no such function: InitSpatialMetaData

How can I include the InitSpatialMetaData symbol? Where is this even defined?

I tried going through the spatialite_gui source to see how this is implemented (which is not much different than mine). For Alessandro's work everything is in three files so tracking dependences is a bit difficult for me as there so much stuff in-scope.

I am building on Debian stable with supported packages:

  • g++ 4.9.2
  • libspatialite-dev 4.1.1
  • libsqlite-dev 3.8.7.1

I am also unable to start the sqlite3 plugin at runtime, this results in an unauthorised error message.

1 Answer 1

1

Found the solution after I explored the documentation a bit more.

https://www.gaia-gis.it/gaia-sins/spatialite-cookbook/html/c.html

Unfortunately, the cookbook part was more about how to use spatial queries instead of how to create software using the spatialite library, with an incomplete description on how to create the metatables.

The missing piece is the call to the spatialite_init_ex routine. This links spatialite sql function calls.

This compiles and works:

#include <sqlite3.h>
#include <iostream>

//extern InitSpatialMetaData; // Always is an incomplete type no matter which spatialite headers I include.

int main()
{
  sqlite3* db;
  char* errMsg;
  int rc {0};
  void* cache; // SOLUTION

  rc = sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, nullptr);
  /* SOLUTION */
  cache = spatialite_alloc_connection();
  spatialite_init_ex(db, cache, 0);
  /* ### */
  rc = sqlite3_exec(db, "SELECT InitSpatialMetaData();", nullptr, 0, &errMsg);
  if (rc != SQLITE_OK)
  {
    std::cout << "CreateSpatialMetaData error: " << errMsg << std::endl;
    sqlite3_free(errMsg);
  }
  sqlite3_close(db);
  return 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.