bazel安装
安装链接: bezal 安装.
编译的文件结构
文件内容
- print_time.h文件内容
1 void print_time_fun();
- print_time.cpp 文件内容
1 #include <iostream>
2 #include <ctime>
3 #include "print_time.h"
4
5 void print_time_fun()
6 {
7 std::cout << "function in file: print_time.cpp" << std::endl;
8 std::time_t time = std::time(nullptr);
9 std::cout << time << std::endl;
10 std::cout << std::asctime(std::localtime(&time)) << std::endl;
11 }
- main.cpp 文件内容
1 #include <iostream>
2 #include <ctime>
3 #include "lib/print_time.h"
4
5 void print_time()
6 {
7 std::time_t time = std::time(nullptr);
8 std::cout << std::asctime(std::localtime(&time));
9 }
10
11 int main()
12 {
13 std::cout << "Hello world" << std::endl;
14 print_time();
15 print_time_fun();
16 return 0;
17 }
bazel 编译
- WORKSPACE文件所在的目录为该工程的根目录,该文件可空
- 根目录下可以有多个子目录和子BUILD
- BUILD中定义了编译的相关依赖关系
- lib/BUILD文件
1 cc_library (
2 name = "print_time",
3 srcs = ["print_time.cpp"],
4 hdrs = ["print_time.h"],
5 visibility = ["//main:__pkg__"],
6 )
- main/BUILD 文件
1 cc_binary (
2 name = "main",
3 srcs = ["main.cpp"],
4 deps = [
5 "//lib:print_time",
6 ]
7 )
- 编译命令:bazel build //main:main
- 执行:./bazel-bin/main/main
- 结果
~/lwm_learn/bazel_learn_examples/cpp-tutorial/eg1$ ./bazel-bin/main/main
Hello world
Fri Jun 21 16:01:58 2019
function in file: print_time.cpp
1561104118
Fri Jun 21 16:01:58 2019