Skip to content

Commit 321891c

Browse files
authored
Hello world C++ interop example (#5991)
Based on #5920. Added commented out better examples and clarified what is missing to support them. Added `tags` support to `carbon_binary` (based on #5967) to set the `BUILD` rule to manual until we can find `cstdio` in macos. ```shell $ bazel run examples/interop/cpp:hello_world ... Hello world! ```
1 parent 0691d48 commit 321891c

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

examples/interop/cpp/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
2+
# Exceptions. See /LICENSE for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
load("//bazel/carbon_rules:defs.bzl", "carbon_binary")
6+
7+
carbon_binary(
8+
name = "hello_world",
9+
srcs = ["hello_world.carbon"],
10+
# TODO: Remove when macos can find cstdio.
11+
tags = ["manual"],
12+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
2+
// Exceptions. See /LICENSE for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
import Cpp inline "#include <cstdio>";
6+
7+
fn Run() {
8+
// TODO: Requires operator <<, declaration type Var (cout) and class with
9+
// virtual bases (basic_ostream).
10+
// Cpp.std.cout << "Hello world!\n";
11+
12+
// TODO: Requires variadic function.
13+
// Cpp.printf("Hello world!\n");
14+
15+
// TODO: Requires nullable pointer.
16+
// Cpp.puts("Hello world!\n");
17+
18+
// TODO: Requires nullable void pointer.
19+
// Cpp.write(1, "Hello world!\n", 13);
20+
21+
// TODO: Requires Core.String API.
22+
// let message: str = "Hello world!\n\n";
23+
// for (c: Core.Char in msg) {
24+
// Cpp.putchar((c as u8) as i32);
25+
// }
26+
27+
let message: array(Core.Char, 13) =
28+
('H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n');
29+
for (c: Core.Char in message) {
30+
// TODO: u8 should probably have an implicit cast to i32.
31+
Cpp.putchar((c as u8) as i32);
32+
}
33+
}

0 commit comments

Comments
 (0)