feat: dotalib
This commit is contained in:
@@ -5,4 +5,8 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
void print_hello_world(void);
|
||||
const char *get_str(void);
|
||||
|
||||
char *get_str_mut(void);
|
||||
|
||||
void println(const char *str);
|
||||
|
||||
10
__ffi/dotalib/justfile
Normal file
10
__ffi/dotalib/justfile
Normal file
@@ -0,0 +1,10 @@
|
||||
_:
|
||||
@just --list
|
||||
|
||||
bindgen:
|
||||
cbindgen --config cbindgen.toml --crate dotalib --lang c --output dotalib.h
|
||||
|
||||
build: bindgen
|
||||
cargo build
|
||||
gcc -Ltarget/debug/ -ldota test.c
|
||||
|
||||
@@ -1,4 +1,21 @@
|
||||
use std::{ffi::{CStr, CString}, os::raw::c_char};
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn print_hello_world() {
|
||||
println!("Hello World!");
|
||||
pub unsafe extern "C" fn println(str: *const c_char) {
|
||||
let c_str = CStr::from_ptr(str).to_str().unwrap();
|
||||
println!("{}", c_str);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn get_str_mut() -> *mut c_char {
|
||||
let c_string = CString::new("hello world!").expect("Error!");
|
||||
c_string.into_raw()
|
||||
}
|
||||
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn get_str() -> *const c_char {
|
||||
let c_string = CString::new("hello world!").expect("Error!");
|
||||
c_string.as_ptr()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#include <stdio.h>
|
||||
#include "dotalib.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
print_hello_world();
|
||||
println("hello world");
|
||||
char* s = get_str_mut();
|
||||
printf("Get: %s\n", s);
|
||||
free(s);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user