0

I'm just getting started with libspatialindex. I ran through the instructions to install and it installed ok on ubuntu with the following commands (except I couldn't find autogen.sh which it mentions).

./configure
make
sudo make install

then I made a very simple program to test it as follows (based on this)

#include <spatialindex/capi/sidx_api.h>
using namespace SpatialIndex;
int main(int argc, char* argv[])
{
    char* pszVersion = SIDX_Version();
}

and compile as follows:

g++ -lspatialindex spatial.cpp -o spatial

and it gives me the error:

/tmp/ccACK3p4.o: In function `main':
spatial.cpp:(.text+0xa): undefined reference to `SIDX_Version'
collect2: ld returned 1 exit status

I've tried many different things such as using cmake instead and installing it a different folder but no luck. Any ideas?

EDIT:

Added the namespace SpatialIndex above and I also tried the following, but still not luck:

g++ -c -o spatial.o spatial.cpp
g++ -o spatial spatial.o -lspatialindex -lm
Richard
  • 56,349
  • 34
  • 180
  • 251
Patrick64
  • 430
  • 6
  • 12
  • Did you include the spatialindex namespace? – itachi Apr 22 '15 at 19:01
  • 1
    The error says that the linker dont find the corresponding code. Please try changing the order : g++ spatial.cpp -lspatialindex -o spatial I'm not very confident with single line compiling&link. I always use two pass: first compile and then link objects with required libraries. – Mel Viso Martinez Apr 22 '15 at 19:17
  • Thanks for the suggestion @itachi, I tried adding the namespace SpatialIndex (I edited the question above to show this too) but still no luck. How can I tell what namespace SIDX_Version is in? It's not clear from the docs http://libspatialindex.github.io/doxygen/sidx__api_8h.html – Patrick64 Apr 22 '15 at 19:59
  • @MelVisoMartinez, I tried it the other way around and separating the compiling&link (as shown in the edit to question I did) but no luck. – Patrick64 Apr 22 '15 at 20:01

2 Answers2

1

The problem is about linking phase. The library is correctly found because the compiler dont throws alert(s) of that kind of situation (I just made a test now). do a

> nm libspatialindex |grep SIDX_Version

for checking the symbol existence in the library you are using. May be libspatialindex_c instead?

  • 1
    Thanks, I tried `gcc -g spatial.c -o spatial -lspatialindex` but it gave me the error `In file included from /usr/local/include/spatialindex/SpatialIndex.h:30:0, from /usr/local/include/spatialindex/capi/sidx_config.h:56, from /usr/local/include/spatialindex/capi/sidx_api.h:34, from spatial.c:1: /usr/local/include/spatialindex/tools/Tools.h:59:20: fatal error: iostream: No such file or directory` – Patrick64 Apr 22 '15 at 20:42
  • I am really confused... The example you referred is typical c and is what "capi" suggest, but these includes are ¡using classes!. I'm sorry I will delete my reponse later. Plese do a nm over the library for checking if the missing symbol is here and let me know if it's inside or not. – Mel Viso Martinez Apr 22 '15 at 20:51
  • @Patrick64 Do you have some like libspatialindex_c? I have seen an example that seems to link against libspatialindex_c instead of libspatialindex. – Mel Viso Martinez Apr 22 '15 at 20:59
  • Yes there is a libspatialindex_c.a file in /usr/local/lib folder, `gcc -g spatial.c -lspatialindex_c -o spatial` does the same as before. – Patrick64 Apr 22 '15 at 21:23
  • Ok, but I was wrong before and you need to use your g++ compilation (with cpp source file) but linking against this other lib. :) – Mel Viso Martinez Apr 22 '15 at 21:25
  • 1
    If this fails due to missing symbols, include both libraries with -lspatialindex_c and -lspatialindex (in this order). I must left for rest. I hope this helps. – Mel Viso Martinez Apr 22 '15 at 21:33
  • You've cracked it, thanks so much. I did `g++ spatial.cpp -lspatialindex_c -lspatialindex -L/usr/local/lib -o spatial` and it works! I also needed to run `sudo ldconfig` before running the program. – Patrick64 Apr 22 '15 at 21:55
  • @Patrick64 - That you needed to run sudo ldconfig sounds like a bug in the install. I don't know what libspatialindex is, but this happens a lot with "free" software. – David Hammen Apr 22 '15 at 22:17
0

By default, the libspatialindex install target is /usr/local. You didn't override that in your invocation of ./configure, so that's where make install will install things.

The compiler knows to look for include files in /usr/local/include, so compilation isn't a problem. It however does not know to look for libraries in usr/local/lib. A simple fix is to add that to your link step:

g++ -o spatial spatial.o -lspatialindex -lm -L/usr/local/lib
David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • Same problem I'm afraid, although that folder does contain `libspatialindex.a` and similar files so maybe it helps to get closer to the solution. – Patrick64 Apr 22 '15 at 20:43