thepowersgang / mrustc

Alternative rust compiler (re-implementation)
MIT License
2.18k stars 109 forks source link

How to fix `Undefined symbols for architecture x86_64` #295

Closed chenzww closed 1 year ago

chenzww commented 1 year ago

Environment: OS: macOS 12.6.3 command line: clang main.c

Clang Failed to build a c program which transpiled from a simple rust main file use mrustc. Error message:

Undefined symbols for architecture x86_64:
  "_ZRG2cD8std0_0_02rt19lang_start_internal0g", referenced from:
      _ZRG2cD8std0_0_02rt10lang_start0g in main-eb1c43.o
  "_ZRG2cD8std0_0_05alloc8rust_oom0g", referenced from:
      _oom_impl in main-eb1c43.o
  "_ZRG2cD8std0_0_09panicking16rust_begin_panic0g", referenced from:
      _panic_impl in main-eb1c43.o
  "_ZRG3cD8std0_0_02io5stdio6_print0g", referenced from:
      _ZRG1cD3bin4main0g in main-eb1c43.o
  "___rdl_alloc", referenced from:
      ___rust_alloc in main-eb1c43.o
  "___rdl_alloc_zeroed", referenced from:
      ___rust_alloc_zeroed in main-eb1c43.o
  "___rdl_dealloc", referenced from:
      ___rust_dealloc in main-eb1c43.o
  "___rdl_realloc", referenced from:
      ___rust_realloc in main-eb1c43.o
ld: symbol(s) not found for architecture x86_64

However, the main.c file build and run succeed in Clion IDE.

thepowersgang commented 1 year ago

To successfully link, libstd and friends also need to be linked in. mrustc should have tried to call the compiler for you, is there a reason you're trying to manually compile the emitted code?

chenzww commented 1 year ago

I'am a beginner in c language and clang compiler. Can you tell me a common way to pass options to gcc or clang compiler to build it?

thepowersgang commented 1 year ago

when you ran mrustc my_rust_file.rs the debug file created will have included the gcc command it was trying to run (and mrustc should have called it for you) You should not need to manually compile the .c file

chenzww commented 1 year ago

Thanks. After I took a close look at the the file which the suffix of file name is _cmd.txt. I finally fully understand the rule of compilation.