编译模块时未定义对“main"的引用 [英] Undefined reference to 'main' when compiling a module

查看:65
本文介绍了编译模块时未定义对“main"的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Fortran,但我一直在尝试编译一个模块以供以后使用.

I am learning Fortran, and I'm stuck trying to compile a module for later use.

在主程序中我有这个,它要求输入两个数字,然后调用模块中的函数:

In the main program I have this, which asks for two numbers and then calls the function in the module:

use exponentiate
integer::a,b
write *, 'A'
read *, 'a
write *, 'B'
read *, 'b
write *,expo(a,b)

(我还没有尝试过,因为我需要先编译模块,但这不是问题)

(I haven't tried that out because I need to compile the module first, but that's not the issue)

然后,在另一个文件上我有这个代码,它(如果我理解正确的话)只是一个标准模块,带有一个对两个数字取幂的函数.

Then, on another file I have this code, which (If I understood anything correctly) is just a standard module with a function that exponentiates two numbers.

module exponentiate
interface test 
  module procedure expo
end interface
contains
  function expo(a,b)
    type(integer), intent(in)::a,b
    type(integer) expo
    integer::temp
    temp=a
    do i=1,b
      temp=temp*a
    end do
    expo=temp
  end function expo
end module exponentiate

我一直试图根据编译器错误找出语法,因为 Fortran 95 规范不可读且几乎无用.有了这个和一些 Wikipedia/SO 帮助,我已经能够弄清楚一些事情,但我不知道为什么会弹出这个编译器错误.

I've been trying to figure the syntax out based on compiler errors since the Fortran 95 specification is unreadable and nearly useless. With that and some Wikipedia/SO help I've been able to figure some things out, but I have no idea why this compiler error pops up.

我不确定这是否是因为某些语法问题或 gfortran 的误用,因此我们将不胜感激.

I'm not sure if this is because of some syntax issue or a misuse of gfortran, so any help would be appreciated.

推荐答案

如果没有相反的选项,编译器驱动程序(以及许多其他的,无论语言如何)将假定它们被提供了一个完整程序所需的所有组件或类似 - 将源代码编译为目标代码(编译正确),将该目标代码与任何其他指定的目标代码或库链接并生成可执行文件.

Without options to the contrary, that compiler driver (and many others, regardless of language) will assume that they are being given all the components necessary for a complete program or similar - compiling source to object code (compilation proper), linking that object code with any other specified object code or libraries and generating an executable.

在 Fortran 程序的上下文中,如果没有某种形式的主程序,则无法完成该过程.许多 Fortran 编译器对主程序的内部名称是 main 或类似名称(大小写和附加下划线存在变化) - 您只是看到编译器无法使用主程序的后果司机.

In the context of a Fortran program, that process cannot be taken through to completion without a main program of some form. The internal name that many Fortran compilers have for the main program is main or similar (variations exist with case and additional underscores) - you are simply seeing the consequences of your main program not being made available to the compiler driver.

使用该编译器驱动程序(和大多数其他驱动程序),要仅将 Fortran 源代码编译为目标代码(即 file.f90 -> file.o),请提供 -c 命令行选项.然后,当您准备好为您的程序构建最终可执行文件时,您可以将带有目标代码的结果文件提供给编译器驱动程序的稍后调用.

With that compiler driver (and most others), to compile Fortran source through to object code only (i.e. file.f90 -> file.o) supply the -c command line option. You can then provide the resulting file with the object code to a later invocation of the compiler driver when you are ready to build the final executable for your program.

或者,在命令行中模块的源文件名称之后提供带有主程序的源文件名称(以及任何其他源文件的名称).

Alternatively, supply the name of the source file with your main program (and the name of any other source files) after the name of the source file for the module on the command line.

这篇关于编译模块时未定义对“main"的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆