GCJ Linking Error: Says error: undefined reference

2019-08-13 10:14发布

The code is this:

public class Thingy 
{
  public static void main(String[] args)
  {
    System.out.println(Math.random());
  }
}

So, absolutely NOT an unusual case, just a basic example.

When I try to use GCJ's javac implementation to make bytecode instead of the OpenJDK one, it works fine.

neil@neil-Inspiron-M5030:~/javastuff$ javac Thingy.java
neil@neil-Inspiron-M5030:~/javastuff$ java Thingy
0.2794511843566371

But when I try to get GCJ to make native code, I get that linker error. If you want, I can give you the verbose output:

neil@neil-Inspiron-M5030:~/javastuff$ gcj --verbose Thingy.java
Using built-in specs.
Reading specs from /usr/lib/gcc/x86_64-linux-gnu/4.7/libgcj.spec
rename spec startfile to startfileorig
rename spec lib to liborig
COLLECT_GCC=gcj
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.2-1ubuntu1' --with-bugurl=file:///usr/share/doc/gcj-4.7/README.Bugs --enable-languages=c,c++,java --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.7/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.7 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.7 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-1ubuntu1) 
COLLECT_GCC_OPTIONS='-fsaw-java-file' '-v' '-fbootclasspath=./:/usr/share/java/libgcj-4.7.jar' '-g1' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/4.7/ecj1 Thingy.java -g1 -fbootclasspath=./:/usr/share/java/libgcj-4.7.jar -g1 -fsource=1.5 -ftarget=1.5 -fzip-dependency /tmp/cckwg5AG.zip -fzip-target /tmp/ccbixA8G.jar
COLLECT_GCC_OPTIONS='-fsaw-java-file' '-v' '-fbootclasspath=./:/usr/share/java/libgcj-4.7.jar' '-g1' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/4.7/jc1 /tmp/ccbixA8G.jar -fsource-filename=Thingy.java -fhash-synchronization -fno-use-divide-subroutine -fuse-boehm-gc -fnon-call-exceptions -fkeep-inline-functions -quiet -dumpbase Thingy.java -mtune=generic -march=x86-64 -auxbase Thingy -g1 -version -fsaw-java-file -fbootclasspath=./:/usr/share/java/libgcj-4.7.jar -faux-classpath /tmp/cckwg5AG.zip -o /tmp/cclZeoeJ.s
GNU Java (Ubuntu/Linaro 4.7.2-1ubuntu1) version 4.7.2 (x86_64-linux-gnu)
    compiled by GNU C version 4.7.2, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU Java (Ubuntu/Linaro 4.7.2-1ubuntu1) version 4.7.2 (x86_64-linux-gnu)
    compiled by GNU C version 4.7.2, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Class path starts here:
    /tmp/cckwg5AG.zip/ (zip)
    ./ (system)
    /usr/share/java/libgcj-4.7.jar/ (system) (zip)
COLLECT_GCC_OPTIONS='-fsaw-java-file' '-v' '-fbootclasspath=./:/usr/share/java/libgcj-4.7.jar' '-g1' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 as -v --64 -o /tmp/cczIy0oL.o /tmp/cclZeoeJ.s
GNU assembler version 2.22.90 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.22.90.20120924
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.7/:/usr/lib/gcc/x86_64-linux-gnu/4.7/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.7/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.7/:/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-fsaw-java-file' '-v' '-fbootclasspath=./:/usr/share/java/libgcj-4.7.jar' '-g1' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/4.7/collect2 --sysroot=/ --build-id --no-add-needed --as-needed --eh-frame-hdr -m elf_x86_64 --hash-style=gnu -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.7/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.7 -L/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.7/../../.. /tmp/cczIy0oL.o -lgcc_s -lgcc -lgcj -lm -lpthread -lrt -lz -ldl -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/4.7/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crtn.o
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o(.text+0x20): error: undefined reference to 'main'
collect2: error: ld returned 1 exit status

so, I suppose my question is, is does anyone have any idea what might have went wrong?

1条回答
爷的心禁止访问
2楼-- · 2019-08-13 10:30

I guess you need to add --main=Thingy to the command. Since several classes may have a 'main' method, the linker needs to be told which one to use.

Also, you might want to add -o <name of executable>, otherwise, you'll get a 'a.out' file, which is the default executable name for the GNU compilers (and any C compiler actually).

Regards

查看更多
登录 后发表回答