Skip to content

Instantly share code, notes, and snippets.

@elorest
Last active July 14, 2017 04:50
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 elorest/73b215c4265ba60e416975f60f96ac30 to your computer and use it in GitHub Desktop.
Save elorest/73b215c4265ba60e416975f60f96ac30 to your computer and use it in GitHub Desktop.
Cbinding Hello World Example
{% `gcc -c say.c` %} # Uses macro to compile object file before compile time.
@[Link(ldflags: "#{__DIR__}/say.o")]
@[Link("m")]
lib LibSay
struct Person
name : UInt8*
height : UInt8*
age : Int32
end
fun hello_ptr(p : LibSay::Person*) : Void
fun hello(p : LibSay::Person) : Void
end
p = LibSay::Person.new(name: "Isaac", age: 33, height: "5'11\"")
LibSay.hello_ptr(pointerof(p))
LibSay.hello(p)
lib LibMath
fun pow(x : Float64, y : Float64) : Float64
end
puts LibMath.pow(2,3).to_i
#include <stdio.h>
struct Person{
char * name;
char * height;
int age;
};
void hello_ptr(const struct Person * p){
printf("Hello %s! Are you really %d and only %s tall?\n", p->name, p->age, p->height);
}
void hello(struct Person p){
printf("Hello %s! Are you really %d and only %s tall?\n", p.name, p.age, p.height);
}
@elorest
Copy link
Author

elorest commented Jul 14, 2017

Run crystal helloworld.cr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment