[GRASS-dev] unknown reference to G__gisinit

Hi, I’m starting to develop some code using Grass following the programmer’s manual:
http://grass.osgeo.org/programming6/gislib.html

but I’m unable to link an example with just the library initialization, receiving this error:

gcc -o main.o -c -I/usr/lib/grass64/include main.cxx
gcc -o test main.o -L/usr/lib/grass64/lib -lgrass_gis
main.o: In function main': main.cxx:(.text+0x3b): undefined reference to G__gisinit(char const*, char const*)’

while compiling this code:
#include <grass/gis.h>

int main (int argc, char **argv)
{
G_gisinit(argv[0]);
}

I’m linking against the correct library and path, because taking a look at the symbols of libgrass_gis.so it seems to exist:

nm -D /usr/lib/grass64/lib/libgrass_gis.so | grep gisinit
000000000001fdae T G__check_gisinit
000000000001fc91 T G__gisinit
000000000001fd57 T G__no_gisinit

so I’m lost here…any ideas? I use Ubuntu oneiric (grass 6.4.1), but I also linked against a manually compiled version of grass 6.4.2 with the same results.

Thanks,

Xavi

On Fri, May 4, 2012 at 1:37 PM, Xavier Rubio <xrubio@gmail.com> wrote:

Hi, I'm starting to develop some code using Grass following the programmer's
manual:
http://grass.osgeo.org/programming6/gislib.html

but I'm unable to link an example with just the library initialization,
receiving this error:

gcc -o main.o -c -I/usr/lib/grass64/include main.cxx
gcc -o test main.o -L/usr/lib/grass64/lib -lgrass_gis
main.o: In function `main':
main.cxx:(.text+0x3b): undefined reference to `G__gisinit(char const*, char
const*)'

It is highly recommended to use a GRASS style Makefile. This will likely
solve the problem. For a simple one, see
http://trac.osgeo.org/grass/browser/grass/branches/releasebranch_6_4/general/g.ask/Makefile

for raster or vector processing, respectively
http://trac.osgeo.org/grass/browser/grass/branches/releasebranch_6_4/doc/raster/r.example/
http://trac.osgeo.org/grass/browser/grass/branches/releasebranch_6_4/doc/vector/v.example/

Markus

Hi, thanks for your answers. Markus, I compared the compilation of “r.example” with my own system and realized that the only difference was the compiler (gcc or g++). Compiling my code with gcc worked, so the problem is the fact that I was developing C++ code to call a C function (the binary symbols are different, so it explains the errors).
Fix this problem was rather easy; I specified the compiler that the gis.h include is C code:

extern “C”
{
#include <grass/gis.h>
}

and it worked, just in case other people finds the same problem.

Thanks!

Xavi

2012/5/5 Markus Neteler <neteler@osgeo.org>

On Fri, May 4, 2012 at 1:37 PM, Xavier Rubio <xrubio@gmail.com> wrote:

Hi, I’m starting to develop some code using Grass following the programmer’s
manual:
http://grass.osgeo.org/programming6/gislib.html

but I’m unable to link an example with just the library initialization,
receiving this error:

gcc -o main.o -c -I/usr/lib/grass64/include main.cxx
gcc -o test main.o -L/usr/lib/grass64/lib -lgrass_gis
main.o: In function main': main.cxx:(.text+0x3b): undefined reference to G__gisinit(char const*, char
const*)’

It is highly recommended to use a GRASS style Makefile. This will likely
solve the problem. For a simple one, see
http://trac.osgeo.org/grass/browser/grass/branches/releasebranch_6_4/general/g.ask/Makefile

for raster or vector processing, respectively
http://trac.osgeo.org/grass/browser/grass/branches/releasebranch_6_4/doc/raster/r.example/
http://trac.osgeo.org/grass/browser/grass/branches/releasebranch_6_4/doc/vector/v.example/

Markus

On Sat, May 5, 2012 at 1:19 PM, Xavier Rubio <xrubio@gmail.com> wrote:

Hi, thanks for your answers. Markus, I compared the compilation of
"r.example" with my own system and realized that the only difference was the
compiler (gcc or g++). Compiling my code with gcc worked, so the problem is
the fact that I was developing C++ code to call a C function (the binary
symbols are different, so it explains the errors).
Fix this problem was rather easy; I specified the compiler that the gis.h
include is C code:

extern "C"
{
#include <grass/gis.h>
}

and it worked, just in case other people finds the same problem.

Glad you found it. I have added a note to
http://grass.osgeo.org/wiki/GRASS_and_C++

(please expand that Wiki page)

Markus

Perfect, I will expand the wiki page while I develop my code if I find issues and potential problems.

Xavi

2012/5/5 Markus Neteler <neteler@osgeo.org>

On Sat, May 5, 2012 at 1:19 PM, Xavier Rubio <xrubio@gmail.com> wrote:

Hi, thanks for your answers. Markus, I compared the compilation of
“r.example” with my own system and realized that the only difference was the
compiler (gcc or g++). Compiling my code with gcc worked, so the problem is
the fact that I was developing C++ code to call a C function (the binary
symbols are different, so it explains the errors).
Fix this problem was rather easy; I specified the compiler that the gis.h
include is C code:

extern “C”
{
#include <grass/gis.h>
}

and it worked, just in case other people finds the same problem.

Glad you found it. I have added a note to
http://grass.osgeo.org/wiki/GRASS_and_C++

(please expand that Wiki page)

Markus

Markus Neteler wrote:

> extern "C"
> {
> #include <grass/gis.h>
> }
>
> and it worked, just in case other people finds the same problem.

Glad you found it. I have added a note to
http://grass.osgeo.org/wiki/GRASS_and_C++

(please expand that Wiki page)

If it's considered desirable to be able to use GRASS from C++, the
headers' contents should be bracketed with:

  #ifdef __cplusplus
  extern "C" {
  #endif

  ...

  #ifdef __cplusplus
  }
  #endif

Without the 'extern "C" ...' qualification, C++ assumes that functions
have C++ linkage, meaning that the parameter types are embedded in the
symbol name (C++ allows function overloading, where multiple functions
can have the same name provided that they have different parameter
types).

--
Glynn Clements <glynn@gclements.plus.com>