0

I have a FORTRAN code which needs a C routine to calculate a measure. The C routine includes .c and .h files and in the documentation it is written:

If you want to embed the hypervolume function into your own C/C++ code, the main function for computing the hypervolume (fpli_hv) is self-contained in the file hv.c. A simple way to add it to your own code is to include Makefile.lib into your Makefile and link against fpli_hv.a. The exported function is:"

double fpli_hv(double *front, int d, int n, double *ref); 

The makefile.lib is also as follows:

VARIANT    ?= 4

HV_SRCS    = hv.c
HV_HDRS    = hv.h
HV_OBJS    = $(HV_SRCS:.c=.o)
HV_LIB     = fpli_hv.a

$(HV_LIB): $(HV_OBJS)
    @$(RM) $@
    $(QUIET_AR)$(AR) rcs $@ $^

## Augment CFLAGS for hv.[co] objects
hv.o: CPPFLAGS += -D VARIANT=$(VARIANT)

## Dependencies:
$(HV_OBJS): $(HV_HDRS)  

How can I embed this C routine in FORTRAN makefiles? Could you please help me, perhaps by providing an illustrative example. I searched for this issue and found a number of examples, but they all showed simple examples which did not require manipulating makefiles.

Chris
  • 44,602
  • 16
  • 137
  • 156
Matt
  • 11
  • 2

1 Answers1

4

Including Makefile.lib into another Makefile should add one new target called fpli_hv.a (also will define the HV_LIB macro). You should add it as dependency to one of the targets in the makefile used to build the Fortran code. For example:

Original Makefile, used to build the Fortran code

...

myprog.exe: <list of object files>
    $(FC) -o $@ $^

...

Modification should be as simple as:

...

include Makefile.lib

...

myprog.exe: <list of object files> $(HV_LIB)
    $(FC) -o $@ $^ $(HV_LIB)

...

Or if you are not going to change and recompile the C code often then you could simply build the library, then add something like this to your Fortran Makefile:

...

LIBHV = /path/to/fpli_hv.a

...

myprog.exe: <list of object files>
    $(FC) -o $@ $^ $(LIBHV)

Probably the original hypervolume Makefile already includes Makefile.lib so you should be able to build the library by something like make fpli_hv.a in the directory where the hypervolume source code is.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
  • Thank you so much for your help. Now, I know how I can deal with makefile. Yes, you are right, the original makefile inlcudes makefile.lib, so everything woyld be fine. My other problem is how to call the C function (double fpli_hv(double *front, int d, int n, double *ref);) in fortran. Do I need to define INTERFACE? How would such an interface look like? Thanks, Matt – Matt May 31 '12 at 13:16