Skip to content

Instantly share code, notes, and snippets.

@mbrubeck
Created June 3, 2016 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbrubeck/a5a86f932b4f08dd3b9eabbcf73c1094 to your computer and use it in GitHub Desktop.
Save mbrubeck/a5a86f932b4f08dd3b9eabbcf73c1094 to your computer and use it in GitHub Desktop.
extern crate gcc;
fn main() {
gcc::compile_library("libtest.a", &["test.c"]);
}
[package]
name = "segv"
version = "0.1.0"
build = "build.rs"
[[bin]]
name = "segv"
path = "main.rs"
[build-dependencies]
gcc = "0.3"
extern {
fn test(str: *const u8, len: usize) -> *mut u8;
}
fn main() {
let s = "hello";
unsafe {
test(s.as_ptr(), s.len());
}
}
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
char *test(const char *str, size_t len)
{
char *new_str;
new_str = malloc(len + 1);
if (!new_str)
return NULL;
memcpy(new_str, str, len);
new_str[len] = '\0';
return new_str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment