java - GCJ 抛出错误 : "Undefined reference to main" when compiling

标签 java linux gcc compiler-errors gcj

我想编译一个简单的 Java“Hello World”程序,就像 GeeksforGeeks Hello World Tutorial 上所描述的那样。 ,通过在 Linux Ubuntu 中使用 gcj 来实现。这是源代码:

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

但是gcj抛出了两个错误:

  1. (.text+0x18):对 main 的 undefined reference
  2. 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 的类命名:

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.

我做错了什么?

最佳答案

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

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

关于java - GCJ 抛出错误 : "Undefined reference to main" when compiling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59802840/

相关文章:

java - 通过重绘方法在 GUI 中添加组件

java - 如何在Java中使用 'goto'的替代

java - IceFaces 1.8.2 "couldn' t 查找属性 : bridge"on button click 的容器

linux - Grep:与 -e 和 -o 选项一起使用

c++ - 为什么那个模板函数不能编译?

sql-server-2008-r2 - 在 64 位 Windows 2008R2 服务器上运行 32 位 JRE

linux - 为什么客户端在TCP握手过程中发送一个RST数据包?

c - fork 和信号 : how to send signals from parent process to specific child process

c++ - 为什么 GCC 减去 1 并比较 <= 2? cmp 在汇编中使用 2 的幂会更快吗?

linux - 将在 linux 中运行汇编代码的结果重定向到文本文件