GCJ throws error: "Undefined reference to main" when compiling

GCJ throws error: "Undefined reference to main" when compiling

我想编译一个简单的 Java "Hello World" 程序,就像它在 GeeksforGeeks Hello World Tutorial 上所描述的那样,方法是在 Linux 中使用 gcj Ubuntu。这是源代码:

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

但是 gcj 抛出了两个错误:

  1. (.text+0x18):对 main
  2. 的未定义引用
  3. collect2:错误:ld 返回 1 退出状态

终端的原始输出:

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

我会注意这个要求,.java 文件应该以包含 main:

的 class 命名

Important Points :

  • The name of the class defined by the program is HelloWorld, which is same as name of file(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.
  • 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= 选项,来自 documentation链接时使用此选项指定 class 的名称,其主要方法应该当生成的可执行文件为 运行.

时被调用
gcj -o helloworld --main=HelloWorld HelloWorld.java