简体   繁体   English

makefile将fortran库链接到C程序

[英]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). 我正在尝试构建一个makefile来编译C程序,然后将其链接到名为MUMPS的fortran库(它也需要blas和pthread库)。 So my idea was to compile by the gcc and then link by the gfortran. 所以我的想法是通过gcc进行编译,然后通过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:找不到-ldmumps

/usr/bin/ld: cannot find -ldmumps_seq / usr / bin / ld:找不到-ldmumps_seq

/usr/bin/ld: cannot find -lmumps_common / usr / bin / ld:找不到-lmumps_common

/usr/bin/ld: cannot find -lmumps_common_seq / usr / bin / ld:找不到-lmumps_common_seq

/usr/bin/ld: cannot find -lpord / usr / bin / ld:找不到-lpord

/usr/bin/ld: cannot find -lpord_seq / usr / bin / ld:找不到-lpord_seq

where am I wrong? 我哪里错了?

Update: I actually found the bug of my makefile. 更新:我实际上发现了我的makefile的错误。 In the linking step, I was calling the libraries before the objects.o and apparently this is not acceptable. 在链接步骤中,我在objects.o之前调用了库,显然这是不可接受的。 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. 如果我将$(LIBS)放在$(OBJS)之后,则一切正常。 Hope this could be useful to someone else. 希望这对其他人有用。

Linker errors of the form 形式的链接器错误

/usr/bin/ld: cannot find -ldmumps / usr / bin / ld:找不到-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. 确保已安装MUMPS开发库。 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. 开发库的名称将采用libdmumps.solibdmumps.a的形式, libdmumps.so不添加任何额外内容。

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. 例如,MUMPS库可能安装在/ usr / lib / mumps,/ usr / lib64 / mumps或/ opt / mumps / lib中,默认情况下不会搜索其中的任何一个。 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.. 如果这些-L/path/to/library/dir在,但是它们的目录不在默认搜索路径上,那么必须在所有-ldmumps等之前添加-L/path/to/library/dir链接选项。

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. 无论如何,如果您的主程序是用C编写的,则应使用C链接程序驱动程序(适合您的gcc)链接它。 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. 您的C代码可能必须通过变形的名称引用Fortran函数,并且必须使用适合于调用所需函数的参数约定。 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. 您无法通过选择链接器来解决这个问题,因为所有函数调用的结构都是由编译器而不是链接器设置的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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