我想编译一个简单的 Java“Hello World”程序,就像 GeeksforGeeks Hello World Tutorial 上所描述的那样。 ,通过在 Linux Ubuntu 中使用 gcj
来实现。这是源代码:
class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello, World");
}
}
<小时/>
但是gcj
抛出了两个错误:
- (.text+0x18):对
main
的 undefined reference - 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/