Hello everyone, i use rust call c loop(for or while), use the printf display something, but loop can't display the contents of printf.
c code:
#include <stdio.h>
#include <unistd.h>
void print_c() {
int s = 3;
while (1) {
printf("%d\n", s);
sleep(1);
}
}
However, when using C++, it can be successfully called. Is there any restriction on the use?
c++ code:
#include <iostream>
#include <stdint.h>
#include <unistd.h>
extern "C" {
int print_it(int32_t num) {
while (1) {
std::cout << num << std::endl;
sleep(1);
}
}
}
Add rust code,call c/c++ lib function.
C is packaged as a shared library, named cthread. C++ is named cppthread.
rust call c lib:
#[link(name = "cthread")]
extern {
fn print_c();
}
fn main() {
unsafe { print_c() };
}
rust call c++ lib:
#[link(name = "cppthread")]
extern {
fn print_it();
}
fn main() {
unsafe { print_it() };
}