13

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
2
  • 2
    For anyone in the future who somehow ends up here: everything has changed - Rust now has pthreads, proc is no longer a thing, and C FFI (mostly) just works! See the book. Jul 21, 2017 at 11:57
  • 1
    @quadrupleslap I think you'd be better to make a new answer for this. An answer doesn't have to be long if it's correct.
    – eonil
    Aug 15, 2017 at 21:55

1 Answer 1

7

It's not that simple, I'm afraid. Rust tasks are not plain threads, like pthreads or others in C. They do need some support from Rust runtime, so in order to create tasks you need to start the runtime. One of the ways is to call a Rust function from your C program which in turn will call another C function:

#include <stdio.h>

extern int run(int argc, char** argv, void (*kont)(void));
extern void do_work();

void actual_main(void);

int main(int argc, char** argv) {
    return run(argc, argv, actual_main);
}

void actual_main(void) {
    printf("Hello from C program\n");
    do_work();
}

Rust:

extern crate native;

#[no_mangle]
pub extern fn run(argc: int, argv: *const *const u8, kont: extern fn()) {
    native::start(argc, argv, proc() kont());
}

#[no_mangle]
pub extern fn do_work() {
    spawn(proc() {
        println!("Hello from task");
    });
}

Compilation (on my Linux box; if you're using another OS, you probably have to tweak it):

% rustc --crate-type staticlib --crate-name ex lib.rs
% gcc -c example.c
% gcc -L. -o example example.o -lex -lm -lpthread -lgcc_s -ldl

Running:

% ./example
Hello from C program
Hello from task

This does require you to change the main function. In any case, all code which access runtime-specific functions from Rust (tasks, Rust I/O, boxes, etc) have to be "under" native::start() call in the call stack. This is the simplest way I know of. Or, if that is acceptable, you can write your main() function in Rust, so your executable binary will be rustc output, even though its work will actually happen in C code.

4
  • This magically works! native::start explains is the point of why I have to link C library into Rust program. I tested on OSX, and I confirmed even AppKit application is working for single spawning of task.
    – eonil
    Aug 19, 2014 at 18:08
  • One more caveat: build 64-bit binary by using rustc --codegen target-cpu=x86-64 to use modern OBJC runtime.
    – eonil
    Aug 19, 2014 at 18:25
  • It looks like libnative has disappeared recently so native::start() cannot be used anymore. However the issue is still here. Is there another solution?
    – noziar
    Dec 4, 2014 at 10:48
  • FYI I found a workaround using let task = box Task::new(None, None) and then task.run(|| { /* your_rust_code_here */ }).destroy(). This way we're sure that the rust code is inside a task. Not sure it is good practice, though.
    – noziar
    Dec 4, 2014 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.