Skip to content

Commit

Permalink
cpp: rewrite example to have library, binary, and test (#243)
Browse files Browse the repository at this point in the history
A single file is too simple.
  • Loading branch information
jayconrod authored Oct 5, 2023
1 parent c35ceb7 commit 66cb996
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
15 changes: 15 additions & 0 deletions cpp/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
cc_binary(
name = "cpp",
srcs = ["main.cc"],
deps = [":cpp_lib"],
)

cc_library(
name = "cpp_lib",
srcs = [
"hello.cc",
"hello.h",
],
)

cc_test(
name = "cpp_test",
srcs = ["hello_test.cc"],
deps = [":cpp_lib"],
)
7 changes: 7 additions & 0 deletions cpp/hello.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "cpp/hello.h"

#include <string>

std::string hello() {
return "hello";
}
8 changes: 8 additions & 0 deletions cpp/hello.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef cpp_hello_h
#define cpp_hello_h

#include <string>

std::string hello();

#endif
11 changes: 11 additions & 0 deletions cpp/hello_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>
#include "cpp/hello.h"

int main() {
auto got = hello();
if (got != "hello") {
std::cerr << "got '" << got << "', want 'hello'" << std::endl;
return 1;
}
return 0;
}
12 changes: 7 additions & 5 deletions cpp/main.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <stdio.h>

int main(int argc, char** argv) {
printf("Hello World\n");
}
#include <iostream>
#include "cpp/hello.h"

int main() {
std::cout << hello() << std::endl;
return 0;
}

0 comments on commit 66cb996

Please sign in to comment.