简体   繁体   中英

makefile linking fortran library to a C program

I'm trying to build a makefile in order to compile a C program and then link it to a fortran library called MUMPS (that also needs blas and pthread libraries). So my idea was to compile by the gcc and then link by the gfortran. here's what I got.

ROOT = $(addprefix $(PWD), /)
BUILDS_DIR = $(addprefix $(ROOT), builds/)
SRCS_DIR   = $(addprefix $(ROOT), src/)
INCS_DIR   = $(addprefix $(ROOT), src/)
OBJS_DIR   = $(addprefix $(SRCS_DIR), objects/)
LIBS_DIR   =

MUMPS_DIR      = $(MUMPS_ROOT)
MUMPS_INCS_DIR = $(addprefix $(MUMPS_DIR), /include)
MUMPS_LIB_DIR  = $(addprefix $(MUMPS_DIR), /lib)
MUMPS_MPI_DIR  = $(addprefix $(MUMPS_ROOT), /libseq)

CC       = gcc -c
CFLAGS   = -O3 -DTRILIBRARY
INCLUDES = -I$(INCS_DIR) -I$(MUMPS_INCS_DIR)

FL       = gfortran -o
LFLAGS   = -L$(LIBS_DIR) -L$(MUMPS_LIB_DIR) -L$(MUMPS_MPI_DIR)
CLIBS    = -lm
FLIBS    = -lblas
LDLIBS   = -lpthread

MUMPS_LIBS = -ldmumps -ldmumps_seq -lmumps_common -lmumps_common_seq -lpord -lpord_seq -lmpiseq

LIBS = $(CLIBS) $(FLIBS) $(LDFLIBS) $(MUMPS_LIBS)

while the rules are

default: $(TARGET)

$(TARGET): $(OBJS)
    @echo -e "\n\n\t\t*** Compile successfully! ***\n" ;
    $(FL) $(BUILDS_DIR)$@ $(LFLAGS) $(LIBS) \
        $(OBJS)
    @echo -e "\n\n\t\t*** Linking complete! ***\n"

$(OBJS): $(OBJS_DIR)%.o : $(SRCS_DIR)%.c
    $(CC) $(CFLAGS) $(INCLUDES) \
        $<\
        -o $@

Unfortunately, this does not work and it gives me as an error

/usr/bin/ld: cannot find -ldmumps

/usr/bin/ld: cannot find -ldmumps_seq

/usr/bin/ld: cannot find -lmumps_common

/usr/bin/ld: cannot find -lmumps_common_seq

/usr/bin/ld: cannot find -lpord

/usr/bin/ld: cannot find -lpord_seq

where am I wrong?

Update: I actually found the bug of my makefile. In the linking step, I was calling the libraries before the objects.o and apparently this is not acceptable. In fact, it was trying to link libraries to some object files that I haven't called yet. if I put $(LIBS) after $(OBJS) everything works out. Hope this could be useful to someone else.

Linker errors of the form

/usr/bin/ld: cannot find -ldmumps

indicate that the library you asked to be linked is not present in the library search path.

Ensure that the MUMPS development libraries installed. Sometimes development components are packaged and/or installed separately from runtime components. The development libraries will have names of the form libdmumps.so or libdmumps.a , with nothing extra appended to the end.

If the dev libraries are installed and have names corresponding to your link options, then it must be the case that they are located somewhere that is not on the linker's default search path. That wouldn't be too unusual. For example, the MUMPS libraries may be installed in /usr/lib/mumps, /usr/lib64/mumps, or /opt/mumps/lib, none of which will be searched by default. If the libraries exist, but their directory is not on the default search path, then you must add a -L/path/to/library/dir link option before all the -ldmumps etc..

In any case, if your main program is written in C, then you should use a C linker driver (gcc for you) to link it. Your C code will likely have to refer to Fortran functions via mangled names, and it will have to use argument conventions appropriate to calling the desired functions. You cannot get around that by choice of linker, because the structure of all your function calls is set up by the compiler, not the linker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM