chore: setup debug project for binding C and Rust
This commit is contained in:
parent
77f520a1a3
commit
5645f6ab71
17
.gitignore
vendored
17
.gitignore
vendored
@ -1,16 +1 @@
|
||||
# ---> Rust
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
|
||||
/target
|
||||
|
16
Cargo.lock
generated
Normal file
16
Cargo.lock
generated
Normal file
@ -0,0 +1,16 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.155"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
|
||||
|
||||
[[package]]
|
||||
name = "utils-debug-c-lib"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "utils-debug-c-lib"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2"
|
@ -1,3 +1,3 @@
|
||||
# Krys4lide
|
||||
## À explorer
|
||||
|
||||
Logiciel de pharmacie
|
||||
- Générer des bindings RUST depuis des .h : https://jvns.ca/blog/2017/12/21/bindgen-is-awesome/
|
||||
|
5
build.rs
Normal file
5
build.rs
Normal file
@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
println!("cargo:rustc-link-search=native=./lib");
|
||||
println!("cargo:rustc-link-lib=static=p4pillondebuglib");
|
||||
println!("cargo:rerun-if-changed=lib/libp4pillondebuglib.a");
|
||||
}
|
BIN
lib/libp4pillondebuglib.a
Normal file
BIN
lib/libp4pillondebuglib.a
Normal file
Binary file not shown.
BIN
lib/p4pillondebuglib.o
Normal file
BIN
lib/p4pillondebuglib.o
Normal file
Binary file not shown.
32
makefile
Normal file
32
makefile
Normal file
@ -0,0 +1,32 @@
|
||||
# Detect the operating system
|
||||
ifeq ($(OS),Windows_NT)
|
||||
MKDIR = if not exist $(LIB_DIR) mkdir $(LIB_DIR)
|
||||
RM = del
|
||||
else
|
||||
MKDIR = mkdir -p $(LIB_DIR)
|
||||
RM = rm -f
|
||||
endif
|
||||
|
||||
CC = gcc
|
||||
AR = ar
|
||||
CFLAGS = -Wall -fPIC
|
||||
SRC_DIR = src
|
||||
LIB_DIR = lib
|
||||
SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
|
||||
OBJ_FILES = $(SRC_FILES:$(SRC_DIR)/%.c=$(LIB_DIR)/%.o)
|
||||
STATIC_LIB = $(LIB_DIR)/libp4pillondebuglib.a
|
||||
|
||||
all: $(STATIC_LIB)
|
||||
|
||||
$(STATIC_LIB): $(OBJ_FILES)
|
||||
$(MKDIR)
|
||||
$(AR) rcs $@ $^
|
||||
|
||||
$(LIB_DIR)/%.o: $(SRC_DIR)/%.c
|
||||
$(MKDIR)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
clean:
|
||||
$(RM) $(OBJ_FILES) $(STATIC_LIB)
|
||||
|
||||
.PHONY: all clean
|
90
src/main.rs
Normal file
90
src/main.rs
Normal file
@ -0,0 +1,90 @@
|
||||
extern crate libc;
|
||||
|
||||
use libc::{ c_char, c_int, c_void, size_t };
|
||||
use std::ffi::CStr;
|
||||
use std::ptr;
|
||||
|
||||
extern {
|
||||
fn hello() -> *const c_char;
|
||||
fn helloPtr(result: *mut c_char);
|
||||
fn helloPtrPtr(result: *mut *mut c_char, size: *mut size_t);
|
||||
fn helloVoidPtrPtr(result: *mut *mut c_void, size: *mut size_t);
|
||||
fn add(a: c_int, b: c_int) -> c_int;
|
||||
fn addPtr(a: c_int, b: c_int, result: *mut c_int);
|
||||
fn fillHexValues(result: *mut *mut c_void, size: *mut size_t);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
unsafe {
|
||||
let c_str = hello();
|
||||
let r_str = CStr::from_ptr(c_str).to_str().unwrap();
|
||||
println!("{} from C", r_str);
|
||||
}
|
||||
|
||||
let mut buffer: [c_char; 50] = [0; 50];
|
||||
unsafe {
|
||||
helloPtr(buffer.as_mut_ptr());
|
||||
let c_str = CStr::from_ptr(buffer.as_ptr());
|
||||
let r_str = c_str.to_str().unwrap();
|
||||
println!("{} from C Ptr", r_str);
|
||||
}
|
||||
|
||||
let mut buffer: *mut c_char = ptr::null_mut();
|
||||
let mut size: size_t = 0;
|
||||
unsafe {
|
||||
helloPtrPtr(&mut buffer, &mut size);
|
||||
|
||||
if !buffer.is_null() {
|
||||
let c_str = CStr::from_ptr(buffer);
|
||||
let r_str = c_str.to_str().unwrap();
|
||||
println!("{} from C Ptr Ptr (size: {})", r_str, size);
|
||||
|
||||
libc::free(buffer as *mut c_void);
|
||||
}
|
||||
}
|
||||
|
||||
let mut buffer: *mut c_void = ptr::null_mut();
|
||||
let mut size: size_t = 0;
|
||||
unsafe {
|
||||
helloVoidPtrPtr(&mut buffer, &mut size);
|
||||
|
||||
if !buffer.is_null() {
|
||||
let c_str = CStr::from_ptr(buffer as *const c_char);
|
||||
let r_str = c_str.to_str().unwrap();
|
||||
println!("{} from C Void Ptr Ptr (size: {})", r_str, size);
|
||||
|
||||
libc::free(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let result = add(1, 2);
|
||||
println!("Result of 1 + 2 = {}", result);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let mut result = 0;
|
||||
addPtr(1, 2, &mut result);
|
||||
println!("Result Ptr of 1 + 2 = {}", result);
|
||||
}
|
||||
|
||||
let mut buffer: *mut c_void = ptr::null_mut();
|
||||
let mut size: size_t = 0;
|
||||
unsafe {
|
||||
fillHexValues(&mut buffer, &mut size);
|
||||
|
||||
if !buffer.is_null() {
|
||||
let hex_values = std::slice::from_raw_parts(buffer as *const u8, size);
|
||||
for &byte in hex_values {
|
||||
print!("{:02X} ", byte);
|
||||
}
|
||||
println!();
|
||||
|
||||
libc::free(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
49
src/p4pillondebuglib.c
Normal file
49
src/p4pillondebuglib.c
Normal file
@ -0,0 +1,49 @@
|
||||
// mylib.c
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char* hello() {
|
||||
return "Hello, World!";
|
||||
}
|
||||
|
||||
void helloPtr(char *result) {
|
||||
sprintf(result, "Hello, World!");
|
||||
}
|
||||
|
||||
void helloPtrPtr(char **result, size_t *size) {
|
||||
const char *message = "Hello, World!";
|
||||
*size = strlen(message) + 1; // +1 for null terminator
|
||||
*result = (char *)malloc(*size);
|
||||
if (*result) {
|
||||
strcpy(*result, message);
|
||||
}
|
||||
}
|
||||
|
||||
void helloVoidPtrPtr(void **result, size_t *size) {
|
||||
const char *message = "Hello, World!";
|
||||
*size = strlen(message) + 1; // +1 for null terminator
|
||||
*result = malloc(*size);
|
||||
if (*result != NULL) {
|
||||
strcpy((char *)*result, message);
|
||||
}
|
||||
}
|
||||
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
// Add function returning result in a pointer
|
||||
void addPtr(int a, int b, int *result) {
|
||||
*result = a + b;
|
||||
}
|
||||
|
||||
void fillHexValues(void **result, size_t *size) {
|
||||
unsigned char values[5] = {0x05, 0xE7, 0x02, 0x00, 0x00};
|
||||
|
||||
*size = sizeof(values);
|
||||
*result = malloc(*size);
|
||||
if (*result != NULL) {
|
||||
memcpy(*result, values, *size);
|
||||
}
|
||||
}
|
14
src/p4pillondebuglib.h
Normal file
14
src/p4pillondebuglib.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef P4PILLONDEBUGLIB_H
|
||||
#define P4PILLONDEBUGLIB_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
char* hello();
|
||||
void helloPtr(char *result);
|
||||
void helloPtrPtr(char **result, size_t *size);
|
||||
void helloVoidPtrPtr(void **result, size_t *size);
|
||||
int add(int a, int b);
|
||||
void addPtr(int a, int b, int *result);
|
||||
void fillHexValues(void **result, size_t *size);
|
||||
|
||||
#endif // P4PILLONDEBUGLIB_H
|
Loading…
Reference in New Issue
Block a user