仓颉 Hello
跳到导航
跳到搜索
Hello
// Hello, World! func fun1(str1:String) { println("Output " + str1 + " from fun1.\n") } main() { var s1 = "Hello, World!" println(s1) fun1("test") }
# compile cjc hello.cj -o hello # hello 793664(VSCode: 795384)
# result Hello, World! Output test from fun1.
C function
- 将调用的函数在 foreign 中定义,基本变量类型改为仓颉类型,变长用 ... 表达
- 结构体定义 @C
- 使用时,用 unsafe{}
编译时,会使用 C 库,如:gcc lib path: /lib/gcc/x86_64-linux-gnu/9。用 cjc ... -V 查看。
foreign { func srand(n: Int32): Unit func time(): Int32 func rand(): Int32 } main() { unsafe { var n1:Int32 = time() srand(n1) } let r1 = unsafe { rand() } println(r1) }
封装 C function
// foreign 中不允许重复引用函数 // 建议每个引用函数单独一行 foreign {func srand(n: Int32): Unit} foreign {func time(): Int32} foreign {func rand(): Int32} public func urand(): UInt32 { unsafe { var n1:Int32 = time() srand(n1) } let r1 = unsafe { rand() } UInt32(r1) }
main() { var n1 = urand() println(n1) }
封装 C function struct
调用方式类似类方法
foreign {func srand(n: Int32): Unit} foreign {func time(): Int32} foreign {func rand(): Int32} public struct UDEF{ //-- rand public func urand(): UInt32 { unsafe { var n1:Int32 = time() srand(n1) } let r1 = unsafe { rand() } UInt32(r1) } //== rand }
main() { var udef1 = UDEF() var n1 = udef1.urand() println(n1) }