| author | |
| committer | |
| log | 4e9b8aec2cbaa9fba7b64b03e90d0426b09e1003 |
| tree | 6ca0fcf042f8b164d95b358ebaada7360f77ad51 |
| parent | abd73083e423fa1422aa64575e357b85c17a5101 |
| signature |
2 files changed, 8 insertions(+), 10 deletions(-)
test/standalone/simple/hello_world/hello_libc.zig+3-7| ... | ... | @@ -1,15 +1,11 @@ |
| 1 | const c = @cImport({ | |
| 2 | // See https://github.com/ziglang/zig/issues/515 | |
| 3 | @cDefine("_NO_CRT_STDIO_INLINE", "1"); | |
| 4 | @cInclude("stdio.h"); | |
| 5 | @cInclude("string.h"); | |
| 6 | }); | |
| 1 | extern fn printf(format: [*:0]const u8, ...) c_int; | |
| 2 | extern fn strlen(str: [*:0]const u8) usize; | |
| 7 | 3 | |
| 8 | 4 | const msg = "Hello, world!\n"; |
| 9 | 5 | |
| 10 | 6 | pub export fn main(argc: c_int, argv: **u8) c_int { |
| 11 | 7 | _ = argv; |
| 12 | 8 | _ = argc; |
| 13 | if (c.printf(msg) != @as(c_int, @intCast(c.strlen(msg)))) return -1; | |
| 9 | if (printf(msg) != @as(c_int, @intCast(strlen(msg)))) return -1; | |
| 14 | 10 | return 0; |
| 15 | 11 | } |
test/standalone/static_c_lib/foo.zig+5-3| ... | ... | @@ -1,12 +1,14 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const expect = std.testing.expect; |
| 3 | const c = @cImport(@cInclude("foo.h")); | |
| 3 | ||
| 4 | extern fn add(a: u32, b: u32) u32; | |
| 5 | extern var foo: u32; | |
| 4 | 6 | |
| 5 | 7 | test "C add" { |
| 6 | const result = c.add(1, 2); | |
| 8 | const result = add(1, 2); | |
| 7 | 9 | try expect(result == 3); |
| 8 | 10 | } |
| 9 | 11 | |
| 10 | 12 | test "C extern variable" { |
| 11 | try expect(c.foo == 12345); | |
| 13 | try expect(foo == 12345); | |
| 12 | 14 | } |