[GRASS5] modifying? gmake rules for c++ code

Glynn Clements glynn.clements at virgin.net
Mon Jul 14 19:46:53 EDT 2003


N.J. Hardebol wrote:

> However at the moment I'm trying to compile a geophysical routine that uses
> several g3d functions and some problems with compiling the c++ source within
> the grass tree arises.
> We have tried to setup a make-rule to compile cpp files within the grass
> source tree.
> I have found the rules to compile *.f and *.c files in the gmake.sh script
> located in grass5.0.2/src/CMD/generic/
> 
> Obviously  the section between lines 268-306  needs to be expanded with the
> rule to compile a *.cpp file.
> The rule is considered to be rougly the same as for *.c files, however with
> additional flag "-XC++".

You shouldn't need that flag; gcc can deduce the language from the
file's suffix, so simply giving the file a ".cc" suffix should cause
gcc to treat it as C++ source.

[Aside: gcc interprets any of the suffixes ".cc", ".cpp", ".cxx" or
".C" (upper-case "C") as signifying C++ code, but the (latest) GRASS
build system only understands ".cc".]

> However we don't fully comprehend the lines 282-284 that uses the sed and
> awk commands, which seem to filter out the *.f and *.c files based on their
> suffix.

That code finds all of the ".o" files which are listed in the
Gmakefile, strips off the ".o" suffix and passes the results (i.e. the
base file names) to the while loop. The while loop then generates one
build rule for each file (i.e. .f -> .o, .cc -> .o or .c -> .o),
depending upon whether a .f or .cc file was found.

[Aside: if neither a .f or .cc file is found, a .c -> .o rule is
added. This handles the situation where the source file is actually a
.l (lex) or .y (yacc) file; in that case, a .c file won't exist when
gmake.sh is being used to generate the make.rules file, but it will be
generated subsequently.]

> Could anybody please indicate how these lines have to be changed, so that
> also object files can be build from *.cpp files.
> Or has anybody modified the gmake5 gmake.sh rules to compile *.cpp source
> code.

In the CVS HEAD version, that while loop looks like this:

    while read file
    do
	if test -f $file.f
	then
	    echo '$(OBJARCH)/'${file}.o: ${file}.f
	    echo '	$(FC) $(FFLAGS) -c' ${file}.f -o '$@'
	elif test -f $file.cc
	then
	    echo '$(OBJARCH)/'${file}.o: ${file}.cc
	    echo '	$(CXX) $(CXXFLAGS) -c' ${file}.cc -o '$@'
	else
	    echo '$(OBJARCH)/'${file}.o: ${file}.c
	    echo '	$(CC) $(CFLAGS) -c' ${file}.c -o '$@'
	fi
    done

The rest of gmake.sh is unchanged from 5.0.2.

However, the above references CXX and CXXFLAGS instead of CC and
CFLAGS. In order to work, this requires some changes which have been
made to other parts of the build system. For your purposes, it should
suffice to change CXX and CXXFLAGS to CC and CFLAGS respectively in
the above code, eliminating the need for the other changes.

This is part of the reason why C++ code only works with gcc. The
configure script currently performs all library and header tests using
the C compiler, resulting in a set of switches which are suitable for
the C compiler. To correctly support the use of a C++ compiler, it
should really re-do all of the tests with a C++ compiler; it may even
be necessary to provide a second set of configure switches which would
apply to C++ compilation.

Right now, we just take the switches which were chosen for C, use them
for C++, and hope that it works.

The other thing which needs to be borne in mind when using C++ is that
the GRASS header files aren't C++-aware, so you have to include them
from C++ like this:

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

One other point: you can't use <imagery.h>, because that has:

	struct Cluster
	{
	    ...
	    int    *class        ; /* class of each point */

which is a syntax error in C++.

-- 
Glynn Clements <glynn.clements at virgin.net>




More information about the grass-dev mailing list