Last active
July 14, 2017 04:50
-
-
Save elorest/73b215c4265ba60e416975f60f96ac30 to your computer and use it in GitHub Desktop.
Cbinding Hello World Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% `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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run
crystal helloworld.cr