Mixing Fortran 90 with C, when C requires a special type of makefile?

396 Views Asked by At

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.

1

There are 1 best solutions below

1
On

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.