环境:Ubuntu16.04 Ubuntu18.04
安装很方便,几分钟就能搞定
官方有example和文档(很多),example都很简单,实际应用复杂得多
然后,自己基于标准库"iostream",写了实例也能编译通过
编译命令很简单
在特定目录生成编译缓存
支持bazel clean,清除之前编译缓存及文件夹
没有找到类似cmake的 set(CMAKE_PREFIX_PATH /opt/****) 选项
所有与项目相关的头文件都必须写完整的绝对路径
通过在WORKSPACE中引用外部依赖的选项new_local_repository,但还是无法直接使用相对路径
若要使用bazel构建工具,则:
用这种方法装是最快的
打开powershell(管理员)
#安装choco
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
#安装bazel
choco install bazel
bazel version
遇到一个坑,要求msys2(一个shell工具),但是装完后用不了系统的环境变量
解决办法:新建一个环境变量name:“MSYS2_PATH_TYPE=inherit”(随便取) value:“inherit”
sudo apt install curl
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
#Step 1: Install the JDK
#Install JDK 8:
sudo apt-get install openjdk-8-jdk
#On Ubuntu 14.04 LTS you must use a PPA:
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update && sudo apt-get install oracle-java8-installer
#Step 2: Add Bazel distribution URI as a package source
#Note: This is a one-time setup step.
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
#If you want to install the testing version of Bazel, replace stable with testing.
#Step 3: Install and update Bazel
sudo apt-get update && sudo apt-get install bazel
#Once installed, you can upgrade to a newer version of Bazel with the following command:
sudo apt-get install --only-upgrade bazel
bazel version
export LD_LIBRARY_PATH=/usr/bin
includes不需要加-I,直接是相对路径;copts只在当前目标里有效
如果相对于主目录是./sub/subsub/a.h,sub/BUILD里,includes只需要写[‘subsub’], 如果是copts则需要写[’-Isub/subsub/’]
cc_library(
name = "some_lib",
srcs = ["some_lib.cc"],
hdrs = ["include/some_lib.h"],
copts = ["-Ilegacy/some_lib/include"],
)
# 通用方法,定义的值会作用到下面的每个子rule中。default_visibility指定了这个包的默认可见规则。可见的情况下才能被其他package调用。
package(default_visibility = ["//visibility:public"],)
# c++库文件,name指定了编译为库文件后的文件名,srcs和hdrs指定源文件和头文件,deps指定需要依赖的其他文件
cc_library(
name = "gradient_checker",
srcs = ["framework/gradient_checker.cc"],
hdrs = ["framework/gradient_checker.h"],
deps = [
":cc_ops",
],
)
# c++测试文件
tf_cc_test(
name = "gradients_array_grad_test",
srcs = ["gradients/array_grad_test.cc"],
deps = [
":array_grad",
],
)
# c++编译目标文件,为一个二进制可执行文件。name必须唯一,srcs指定了源文件,linkopts指定了链接规则,deps指定了依赖文件
tf_cc_binary(
name = "tutorials_example_trainer",
srcs = ["tutorials/example_trainer.cc"],
copts = tf_copts(),
linkopts = select({
"//tensorflow:windows": [],
"//tensorflow:windows_msvc": [],
"//tensorflow:darwin": [
"-lm",
"-lpthread",
],
"//tensorflow:ios": [
"-lm",
"-lpthread",
],
"//conditions:default": [
"-lm",
"-lpthread",
"-lrt",
],
}),
deps = [
":cc_ops",
"//tensorflow/core:core_cpu",
],
)
# 为多个编译目标target指定一个名字,glob是一个帮助函数,指定了目录中哪些文件会include,哪些会exclude。visibility指定了target的可见性,也就是可以被哪些package调用
filegroup(
name = "all_files",
srcs = glob(
["**/*"],
exclude = [
"**/METADATA",
"**/OWNERS",
],
),
visibility = ["//tensorflow:__subpackages__"],
)
#bazel build 包名:任务名
$ bazel build //src:hello-world
cc_binary(
name = "libmylib.so", #mylib是头文件的名字
srcs = ["mylib.cpp",
"mylib.h", #头文件和源文件,必须都有
],
deps = ["//XX:XX", #依赖,注意要把头文件和源文件中include的头文件所在的BUILD包都加进去
],
copts = ["-g", #编译时候的命令
],
linkopts = ["-lstdc++", #链接时候的命令
],
linkshared = True,
linkstatic = True,
)
$ bazel build //XX:libmylib.so
在libmylib.so目录下输入“ldd libmylib.so”,可查看libmylib.so正常被调用时需要在哪些位置找到哪些更底层的动态链接库文件,可以根据这个目录在新环境里安装相应的依赖
$ ldd libmylib.so