feat: dotalib

This commit is contained in:
2021-01-10 18:21:01 +08:00
parent 9dc7182b86
commit 58ea520a63
4 changed files with 39 additions and 4 deletions

View File

@@ -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
View 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

View File

@@ -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()
}

View File

@@ -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);
}