简体   繁体   English

GCJ 在编译时抛出错误:“对 main 的未定义引用”

[英]GCJ throws error: "Undefined reference to main" when compiling

I´d wanted to compile a simple Java "Hello World" program like it was repesented on the GeeksforGeeks Hello World Tutorial , by using gcj in Linux Ubuntu.我想通过在 Linux Ubuntu 中使用gcj来编译一个简单的 Java“Hello World”程序,就像GeeksforGeeks Hello World 教程中提到的那样 This is the source code:这是源代码:

class HelloWorld 
{ 
    public static void main(String args[]) 
    { 
        System.out.println("Hello, World"); 
    } 
} 

But gcj threw two errors:但是gcj抛出了两个错误:

  1. (.text+0x18): undefined reference to main (.text+0x18): 对main未定义引用
  2. collect2: error: ld returned 1 exit status collect2: 错误: ld 返回 1 个退出状态

Original output from the terminal:终端的原始输出:

gcj -o helloworld HelloWorld.java
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status

I´d take attention on the requirement, that the .java file should be named after the class which holds main :我会注意这个要求,.java 文件应该以包含main的类命名:

Important Points :要点:

  • The name of the class defined by the program is HelloWorld, which is same as name of file(HelloWorld.java).程序定义的类名为HelloWorld,与文件名(HelloWorld.java)相同。 This is not a coincidence.这并非巧合。 In Java, all codes must reside inside a class and there is at most one public class which contain main() method.在 Java 中,所有代码都必须驻留在一个类中,并且最多有一个包含 main() 方法的公共类。
  • By convention, the name of the main class(class which contain main method) should match the name of the file that holds the program.按照惯例,主类(包含 main 方法的类)的名称应该与包含程序的文件的名称相匹配。

What am I doing wrong?我究竟做错了什么?

您缺少--main=选项,从文档中此选项在链接时用于指定在运行生成的可执行文件时应调用其 main 方法的类的名称。

gcj -o helloworld --main=HelloWorld HelloWorld.java

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

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