Calling a C library function from Rust program (an executable compiled by rustc) is working well, and and a goal of the Rust team.
Calling a Rust crate function from C program (an executable compiled by clang) is working for simple stuffs, but if I spawn a task, it crashes.
How can I make Rust tasks to work?
Here's my sources and error messages. You also can download from https://github.com/Eonil/TeachingMyselfRust
a.rs
#[no_mangle]
pub fn test1()
{
let a1 = proc()
{
};
spawn(a1);
}
b.c
#include <stdio.h>
extern void test1();
int main(int argc, char** argv)
{
test1();
return 0;
}
build script
rm -rf ./Build
mkdir ./Build
rustc a.rs --crate-type=staticlib -o ./Build/rust-stuffs.a
clang b.c ./Build/rust-stuffs.a -o Build/out
cd Build
./out
execution result
warning: unlinked native library: System
There are not many persons who know what wonders are opened to them in the
stories and visions of their youth; for when as children we listen and dream,
we think but half-formed thoughts, and when as men we try to remember, we are
dulled and prosaic with the poison of life. But some of us awake in the night
with strange phantasms of enchanted hills and gardens, of fountains that sing
in the sun, of golden cliffs overhanging murmuring seas, of plains that stretch
down to sleeping cities of bronze and stone, and of shadowy companies of heroes
that ride caparisoned white horses along the edges of thick forests; and then
we know that we have looked back through the ivory gates into that world of
wonder which was ours before we were wise and unhappy.
fatal runtime error: assertion failed: !ptr.is_null()
stack backtrace:
1: 0x108bc7944 - rt::backtrace::imp::write::hdaa6a604147ca757g8b::v0.10
2: 0x108b5f4aa - rt::util::abort::h3d7b16d436532c64Bmc::v0.10
3: 0x108bc605a - rt::local_ptr::compiled::take::hb1c4b940f0aa52aea9a::v0.10
4: 0x108b5e6ad - task::TaskBuilder::spawn::h2dcf43b5eaa6805aQhC::v0.10
5: 0x108b5eb6d - task::spawn::hff1c6bd6cfded263NjC::v0.10
6: 0x108b29670 - test1
7: 0x108b295fd - main
./run.bash: line 8: 38948 Illegal instruction: 4 ./out