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

查看:85
本文介绍了编译模块时未定义对"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天全站免登陆