Skip to content

Commit 87b3823

Browse files
committed
Add a hello world example
1 parent c3aa111 commit 87b3823

File tree

7 files changed

+114
-1
lines changed

7 files changed

+114
-1
lines changed

BUILD.bazel

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
cc_binary(
4+
name = "main",
5+
srcs = ["main.cc"],
6+
deps = [
7+
":hello_world",
8+
],
9+
)
10+
11+
cc_library(
12+
name = "hello_world",
13+
srcs = [
14+
"hello_world.cc",
15+
],
16+
hdrs = [
17+
"hello_world.h",
18+
],
19+
deps = [
20+
],
21+
)
22+
23+
cc_test(
24+
name="hello_world_test",
25+
srcs = [
26+
"hello_world_test.cc",
27+
],
28+
deps = [
29+
":hello_world",
30+
"@com_google_googletest//:gtest_main",
31+
],
32+
)

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
# bazel_cpp_example
1+
# Bazel C++ Example
2+
3+
Simple example to demonstrate how to use [Bazel](https://bazel.build) with C++.

WORKSPACE

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Bazel toolchains
2+
http_archive(
3+
name = "bazel_toolchains",
4+
urls = [
5+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-toolchains/archive/r324073.tar.gz",
6+
"https://github.com/bazelbuild/bazel-toolchains/archive/r324073.tar.gz",
7+
],
8+
strip_prefix = "bazel-toolchains-r324073",
9+
sha256 = "71548c0d6cd53eddebbde4fa9962f5395e82645fb9992719e0890505b177f245",
10+
)
11+
12+
# GoogleTest/GoogleMock framework. Used by most unit-tests.
13+
http_archive(
14+
name = "com_google_googletest",
15+
urls = ["https://github.com/google/googletest/archive/master.zip"],
16+
strip_prefix = "googletest-master",
17+
)
18+

hello_world.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <string>
2+
3+
#include "hello_world.h"
4+
5+
namespace {
6+
const char* message = "Hello World!";
7+
}
8+
9+
namespace hello_world {
10+
11+
HelloWorld::HelloWorld() {}
12+
13+
std::string HelloWorld::Message() {
14+
return message;
15+
}
16+
17+
} // namespace hello_world

hello_world.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef HELLO_WORLD_H_
2+
#define HELLO_WORLD_H_
3+
4+
#include <string>
5+
6+
namespace hello_world {
7+
8+
// HelloWorld is a simple class.
9+
class HelloWorld {
10+
public:
11+
// HelloWorld constructor.
12+
HelloWorld();
13+
14+
// Message returns a Hello World message.
15+
std::string Message();
16+
};
17+
18+
} // namespace hello_world
19+
20+
#endif // HELLO_WORLD_H_

hello_world_test.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "hello_world.h"
2+
3+
#include <iostream>
4+
5+
#include "gtest/gtest.h"
6+
7+
namespace {
8+
9+
TEST(HelloWorld, Message) {
10+
auto msg = hello_world::HelloWorld().Message();
11+
EXPECT_EQ(msg, "Hello World!");
12+
}
13+
14+
}

main.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <iostream>
2+
3+
#include "hello_world.h"
4+
5+
int main(int argc, char** argv) {
6+
auto msg = hello_world::HelloWorld().Message();
7+
std::cout << "stdout: " << msg << std::endl;
8+
std::cerr << "stderr: " << msg << std::endl;
9+
return 0;
10+
}

0 commit comments

Comments
 (0)