【问题标题】:Compiling Fortran 77 with an external library using a Makefile使用 Makefile 使用外部库编译 Fortran 77
【发布时间】:2019-03-02 02:26:15
【问题描述】:

我有主程序Engine.f 调用LIB.f 中的函数/外部。 与 C++ 和 Java 不同,主程序中没有包含,因此可以编译。

我的 Fortran 编译器如何知道我正在使用另一个库?

我正在使用来自 Eclipse 的 photran。

MAKE 文件:

.PHONY: all clean

# Change this line if you are using a different Fortran compiler
FORTRAN_COMPILER = gfortran

all: src/Engine.f
    $(FORTRAN_COMPILER) -O2 -g \
        -o bin/Engine.exe \
        src/Engine.f

clean:
    rm -f bin/Engine.exe *.mod

我在编译时遇到的错误:

undefined reference to (name of function in **LIB.f**)

【问题讨论】:

    标签: makefile compilation fortran dependencies


    【解决方案1】:
    .PHONY: all clean
    all: Engine.exe
    
    # Change this line if you are using a different Fortran compiler
    FORTRAN_COMPILER = gfortran
    FORTRAN_FLAGS=-O2 -g
    
    %.o: src/%.f
        $(FORTRAN_COMPILER) $(FORTRAN_FLAGS) -c $<
    
    Engine.exe: Lib.o Engine.o
        $(FORTRAN_COMPILER) $(FORTRAN_FLAGS) \
            -o bin/Engine.exe \
            Lib.o Engine.o
    
    clean:
        rm -f *.o *.mod
    

    在 FORTRAN 77 中,编译器“只是”需要在链接时在 .o 文件中提供函数。您可以在下面测试 Makefile,它应该可以满足您的需求。

    现代版本的 Fortran 使用模块文件来构建库,如果您升级到该版本的话。

    【讨论】:

    • 是的,你是对的@pierre de Buyl,当我编译它时,我得到了这个:make all make:Nothing to be done for 'all'。 **** 这意味着它根本无法编译。对吗?
    • 我忘了恢复 make 目标。试试make Engine.exe
    • 我又添加了一个“全部”目标
    • 我明白了:make all (new line) gfortran "O2 -g" -c src/LIB.f (new line) gfortran: error: O2 -g: No such file or directory (new line) make: *** [Makefile:17: LIB.o] Error 1 (new line) 16:43:01 Build Failed。 1 个错误,0 个警告。 (花了 360 毫秒)(新行)第 17 行是:$(FORTRAN_COMPILE ...
    • 我漏掉了一个空格,而且引号是错误的。我编辑了makefile。
    猜你喜欢
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多