[solved] ___morestack link error


#1

Hi,

I’m trying to run some Rust code from a C project and I run into a link error. The Rust code looks like this

#[no_mangle]
pub extern fn call_from_c() {
}

And the C program looks like this

extern void call_from_c();
int main() {
	call_from_c();
}

I build with this command line (rust code is built with Cargo)

clang test.c target/debug/libapi_test.rlib -o test

I then get this link error:

Undefined symbols for architecture x86_64:
  "___morestack", referenced from:
      _call_from_c in libapi_test.rlib(api_test.0.o)
      call_from_c::__rust_abi in libapi_test.rlib(api_test.0.o)

When searching it seems that other people have run into the same issue but I couldn’t really find and solution for it.

clang --version
Apple LLVM version 7.0.0 (clang-700.0.53.3)
Target: x86_64-apple-darwin15.0.0

rustc --version
rustc 1.3.0 (9a92aaf19 2015-09-15)

Any ideas how to solve this would be great.

Cheers!


#2

It seems that this requirement has been removed as of version 1.4


#3

How’re you compiling the Rust code here? If you’re linking Rust into C it’s highly recommended to generate a staticlib output crate type instead of an object file as that’ll pull in all Rust dependencies as well (including this symbol)


#4

Thanks for the reply,

What I did is is just to generate a regular cargo project (cargo new ), add some code to lib.rs, build and then link the C project with the resulting .rlib (target/debug/libapi_test.rlib)

Is there some more setting I need to add to the Cargo.toml file so Cargo will generate a lib that contains all the dependencies?

Cheers!


#5

Ah yeah rlib is meant for rustc consumption only, but if you add this to Cargo.toml it will generate libfoo.a which should be linkable:

[lib]
crate-type = ["staticlib"]
name = "..."

#6

Ah, I see.

Thanks! :slight_smile: