Rust modules inside RIOT

Currently, I am trying to figure out how to implement a network protocol module in Rust on RIOT, but I still can’t know how I can put the Rust code into the C application code. Specifically, my current setup is like this:

  • I have a rcoap.h header file in the sys/include that defines:
void hello(void);
  • The module sys/rcoap:
    • Makefile:
include $(RIOTBASE)/Makefile.base

include $(RIOTMAKE)/cargo-settings.inc.mk

APPLICATION_RUST_MODULE = rcoap 
USEMODULE += rust_riotmodules

# No need to set the Cargo features enabled through the general pseudomodules:
# They're added to the crate in cargo-targets.inc.mk no matter whether it's
# building the rust_riotmodules_standalone top level crate or an application
# crate.

include $(RIOTMAKE)/cargo-targets.inc.mk
  • Code:
#![no_std]

#[no_mangle]
pub extern "C" fn hello() {
    riot_wrappers::println!("Hello world");
}
  • And a C application examples/hello-rcoap like this:
#include <stdio.h>
#include "rcoap.h"

int main(void)
{
    hello();

    return 0;
}
  • I try to compile the application, but I still get the errors:
/usr/bin/ld: /media/duich1605/extras/projects/iot/rcoap/RIOT/examples/hello-rcoap/bin/native/application_hello-world/main.o: in function `main':
/media/duich1605/extras/projects/iot/rcoap/RIOT/examples/hello-rcoap/main.c:27: undefined reference to `hello'
collect2: error: ld returned 1 exit status

I have read rust modules inside the RIOT directory, but still can’t figure how I can link the header file and the rust source code together? Is there any specific configuration that allows me to do this?

1 Like

My guess is that the .o files produced by the rust compiler are not being included. Maybe @j-devel or @chrysn can figure out what your Makefile is missing.

I have never succeeded in letting rustc directly handle RIOT system’s .h files.

But, I do know that we can achieve a quite similar goal by

  • building a RIOT C module (with the custom .h file), and
  • referencing the function in Rust code through that C module

For instance, the rust-gcoap example does that for the do_vfs_init() function.

See

    extern "C" {
        fn do_vfs_init();
    }
USEMODULE += vfs