[GRASS-dev] Problem using C++ libraries with C code

Hi,

I’m working on a raster module written in C and I want to use C++ libraries to build a function but I don’t know anything about linkers. I tried to use ‘extern “C”’ to build the function in C++ code and declare it in a header file shared by C (the local_proto.h header) but I have no idea if this could work since I haven’t been able to compile. I also tried to modify my makefile in order to compile the C and the C++ files together but I have little experience on building custom makefiles…
I could rewrite the whole code in C++ but that’s too much work. Any hint or example code? By the way, the C++ library that I’m trying to use is GAlib, a genetic algorithms library.

This is how C++ function looks like:

extern “C” {
#include <grass/gis.h>
#include <grass/glocale.h>
#include <stdio.h>
#include “fann.h”
#include “local_proto.h”
}

#include <math.h>

#include <ga/GASimpleGA.h>
#include <ga/GABin2DecGenome.h>
#include <ga/std_stream.h>

#define cout STD_COUT

extern “C” void genetic(struct input * init, struct layer * vars, struct output * out, char * NNconfig, int totalayers, int rows, int cols, int * fixedcat)
{
/Nothing yet. I have to learn how to compile this/
}

This is my “local_proto.h” file:

#include <grass/gis.h>
#include <grass/glocale.h>

struct input
{
const char *name, *mapset;
int fd;
CELL *buf; /puntero del tipo double CELL/
};

struct layer
{
const char *name, *mapset;
int fd;
DCELL *buf; /puntero del tipo double CELL/
};

struct output
{
const char *name, *mapset;
int fd;
CELL *buf;
};

/* tfile.c */
void Create_training_file(struct input *, struct layer *, int, int, int, int);

/* simple.c */
void Simple_training(char *, char *, unsigned int, unsigned int, unsigned int, float, unsigned int *);

/interpol.c/
double interpolate(struct layer, int, int, int, int);

/run.c/
void run_NN(struct input *, struct layer *, struct output *, char *, int, int, int, int *);

/genetic_fann.cc/

#ifdef __cplusplus
extern “C”
#endif
void genetic(struct input *, struct layer *, struct output *, char *, int, int, int, int *);

And my makefile (pretty much standard):

MODULE_TOPDIR = …/…

PGM = r.ailucc

LIBES = $(STATSLIB) $(RASTERLIB) $(GISLIB)
DEPENDENCIES = $(STATSDEP) $(RASTERDEP) $(GISDEP)

include $(MODULE_TOPDIR)/include/Make/Module.make

LDFLAGS+= -lfann

default: cmd