Linking errors on FreeBsd

I'm trying to compile no-std binary on FreeBSD, and gets the following linking errors:

error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-Wl,--eh-frame-hdr" "-L" "/usr/home/main/.rustup/toolchains/nightly-x86_64-unknown-freebsd/lib/rustlib/x86_64-unknown-freebsd/lib" "/usr/home/main/no-std-test/target/debug/deps/no_std_test-e7837d28ea6608d6.17o5x6xpakwsacv7.rcgu.o" "-o" "/usr/home/main/no-std-test/target/debug/deps/no_std_test-e7837d28ea6608d6" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/usr/home/main/no-std-test/target/debug/deps" "-L" "/usr/home/main/.rustup/toolchains/nightly-x86_64-unknown-freebsd/lib/rustlib/x86_64-unknown-freebsd/lib" "-Wl,-Bstatic" "/usr/home/main/.rustup/toolchains/nightly-x86_64-unknown-freebsd/lib/rustlib/x86_64-unknown-freebsd/lib/librustc_std_workspace_core-2ab355ea51336e3d.rlib" "/usr/home/main/.rustup/toolchains/nightly-x86_64-unknown-freebsd/lib/rustlib/x86_64-unknown-freebsd/lib/libcore-46284c4f10a9894c.rlib" "/usr/home/main/.rustup/toolchains/nightly-x86_64-unknown-freebsd/lib/rustlib/x86_64-unknown-freebsd/lib/libcompiler_builtins-d7edd20b6a9d4baf.rlib" "-Wl,-Bdynamic"
  = note: ld: error: undefined symbol: _init_tls
          >>> referenced by crt1.c:66 (/usr/src/lib/csu/amd64/crt1.c:66)
          >>>               /usr/lib/Scrt1.o:(_start)
          
          ld: error: undefined symbol: main
          >>> referenced by crt1.c:76 (/usr/src/lib/csu/amd64/crt1.c:76)
          >>>               /usr/lib/Scrt1.o:(_start)
          
          ld: error: undefined symbol: exit
          >>> referenced by crt1.c:76 (/usr/src/lib/csu/amd64/crt1.c:76)
          >>>               /usr/lib/Scrt1.o:(_start)
          
          ld: error: undefined symbol: atexit
          >>> referenced by crt1.c:63 (/usr/src/lib/csu/amd64/crt1.c:63)
          >>>               /usr/lib/Scrt1.o:(_start)
          
          ld: error: undefined symbol: atexit
          >>> referenced by ignore_init.c:114 (/usr/src/lib/csu/common/ignore_init.c:114)
          >>>               /usr/lib/Scrt1.o:(handle_static_init)
          cc: error: linker command failed with exit code 1 (use -v to see invocation)

The -nodefaultlibs argument here looks very strange for me.

The following build command fixes the issue.

RUSTFLAGS="-C default_linker_libraries" cargo build

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.