規則
- cc_binary
- cc_import
- cc_library
- cc_shared_library
- cc_static_library
- cc_test
- cc_toolchain
- cc_toolchain_suite
- fdo_prefetch_hints
- fdo_profile
- memprof_profile
- propeller_optimize
cc_binary
查看規則來源cc_binary(name, deps, srcs, data, additional_linker_inputs, args, compatible_with, conlyopts, copts, cxxopts, defines, deprecation, dynamic_deps, env, exec_compatible_with, exec_group_compatible_with, exec_properties, features, hdrs_check, includes, licenses, link_extra_lib, linkopts, linkshared, linkstatic, local_defines, malloc, module_interfaces, nocopts, output_licenses, package_metadata, reexport_deps, restricted_to, stamp, tags, target_compatible_with, testonly, toolchains, visibility, win_def_file)
並產生可執行的二進位檔。
目標的
name
應與應用程式主要進入點的來源檔案名稱相同 (不含副檔名)。舉例來說,如果您的進入點位於 main.cc
,則名稱應為 main
。
隱含輸出目標
name.stripped
(僅在明確要求時建構):二進位檔的已移除版本。strip -g
會在二進位檔上執行,以移除偵錯符號。您可以使用--stripopt=-foo
,在指令列中提供其他剝除選項。name.dwp
(僅在明確要求時建構):如果啟用 Fission,則為適合用來偵錯遠端部署二進位檔的偵錯資訊套件檔案。否則:空白檔案。
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
deps
|
標籤清單;預設值為 這些可以是 linkopts 中參照這些指令碼,但請考慮該用途的 additional_linker_inputs 。 |
srcs
|
標籤清單;預設值為 系統會編譯所有 純組語檔案 (.s、.asm) 不會經過前置處理,通常是使用組語建構。預先處理的組語檔案 (.S) 會經過預先處理,通常是使用 C/C++ 編譯器建構。 系統不會編譯 所有
如果
允許的
... 和產生這些檔案的任何規則 (例如 |
data
|
標籤清單;預設值為 data
有關大多數建構規則定義的典型屬性的一般註解。
如果 如果 您的 C++ 程式碼可以存取這些資料檔案,如下所示:
|
additional_linker_inputs
|
標籤清單;預設值為
舉例來說,這裡可以提供已編譯的 Windows .res 檔案,以便嵌入二進位目標。 |
conlyopts
|
字串清單;預設值為 |
copts
|
字串清單;預設值為
這個屬性中的每個字串都會依指定順序新增至
如果套件宣告 feature
|
cxxopts
|
字串清單;預設值為 |
defines
|
字串清單;預設值為 -D 為前置字元,然後新增至這個目標的編譯指令列,以及每個依附於這個目標的規則。請務必謹慎操作,因為這可能會造成深遠影響,定義會新增至依附於這個目標的每個目標。如有疑問,請改為在 local_defines 中新增定義值。 |
dynamic_deps
|
標籤清單;預設值為 cc_shared_library 依附元件。
|
hdrs_check
|
字串;預設值為 |
includes
|
字串清單;預設值為 -isystem path_to_package/include_entry 。這項功能僅適用於不符合 Google 撰寫 #include 陳述式樣式的第三方程式庫。與 COPTS 不同,這些標記會新增至這項規則和所有依附於這項規則的規則。(注意:不是規則所依據的規則!)請務必謹慎操作,因為這項設定可能影響範圍廣泛。如有疑問,請在 COPTS 中加入「-I」旗標。
新增的 |
link_extra_lib
|
標籤;預設值為
根據預設,C++ 二進位檔會連結至 |
linkopts
|
字串清單;預設值為 LINKOPTS ,然後連結二進位目標。
如果這份清單中的元素不是以 |
linkshared
|
布林值;預設值為 linkshared=True 。這項功能預設為關閉。
這個標記表示連結會使用
如果您同時指定 |
linkstatic
|
布林值;預設值為 cc_binary 和 cc_test ,請以靜態模式連結二進位檔。如為 cc_library.link_static ,請參閱下文。
這項功能預設為
如果啟用這個選項,且這是二進位檔或測試,這個選項會告知建構工具盡可能連結使用者程式庫的 連結可執行檔的方法其實有三種:
如果
如果用於
在正式環境中,以 |
local_defines
|
字串清單;預設值為 -D 為前置字元,然後加入這個目標的編譯指令列,但不會加入其依附元件。與 defines 不同的是,定義只會新增至這個目標的編譯命令列。 |
malloc
|
標籤;預設值為
根據預設,C++ 二進位檔會連結至 |
module_interfaces
|
標籤清單;預設值為 C++ 標準對模組介面副檔名沒有限制
使用時須遵守 |
nocopts
|
字串;預設值為 COPTS (包括規則 copts 屬性中明確指定的值),都會從 COPTS 中移除,以編譯這項規則。這個屬性不應在 third_party 以外使用,也不應有此需求。除了「Make」變數替換之外,這些值不會經過任何前置處理。 |
reexport_deps
|
標籤清單;預設值為 |
stamp
|
整數;預設值為
除非依附元件有所變更,否則系統不會重建蓋章的二進位檔。 |
win_def_file
|
標籤;預設值為 只有在 Windows 是目標平台時,才應使用這項屬性。 連結共用程式庫時,可用於 匯出符號。 |
cc_import
查看規則來源cc_import(name, deps, data, hdrs, alwayslink, compatible_with, deprecation, exec_compatible_with, exec_group_compatible_with, exec_properties, features, includes, interface_library, linkopts, objects, package_metadata, pic_objects, pic_static_library, restricted_to, shared_library, static_library, strip_include_prefix, system_provided, tags, target_compatible_with, testonly, toolchains, visibility)
cc_import
規則可讓使用者匯入預先編譯的 C/C++ 程式庫。
常見用途如下:
1. 連結靜態程式庫
cc_import(
name = "mylib",
hdrs = ["mylib.h"],
static_library = "libmylib.a",
# If alwayslink is turned on,
# libmylib.a will be forcely linked into any binary that depends on it.
# alwayslink = True,
)
cc_import(
name = "mylib",
hdrs = ["mylib.h"],
shared_library = "libmylib.so",
)
在 Unix 上:
cc_import(
name = "mylib",
hdrs = ["mylib.h"],
# libmylib.ifso is an interface library for libmylib.so which will be passed to linker
interface_library = "libmylib.ifso",
# libmylib.so will be available for runtime
shared_library = "libmylib.so",
)
Windows:
cc_import(
name = "mylib",
hdrs = ["mylib.h"],
# mylib.lib is an import library for mylib.dll which will be passed to linker
interface_library = "mylib.lib",
# mylib.dll will be available for runtime
shared_library = "mylib.dll",
)
system_provided=True
在 Unix 上:
cc_import(
name = "mylib",
hdrs = ["mylib.h"],
interface_library = "libmylib.ifso", # Or we can also use libmylib.so as its own interface library
# libmylib.so is provided by system environment, for example it can be found in LD_LIBRARY_PATH.
# This indicates that Bazel is not responsible for making libmylib.so available.
system_provided = True,
)
Windows:
cc_import(
name = "mylib",
hdrs = ["mylib.h"],
# mylib.lib is an import library for mylib.dll which will be passed to linker
interface_library = "mylib.lib",
# mylib.dll is provided by system environment, for example it can be found in PATH.
# This indicates that Bazel is not responsible for making mylib.dll available.
system_provided = True,
)
在 Unix 上:
cc_import(
name = "mylib",
hdrs = ["mylib.h"],
static_library = "libmylib.a",
shared_library = "libmylib.so",
)
Windows:
cc_import(
name = "mylib",
hdrs = ["mylib.h"],
static_library = "libmylib.lib", # A normal static library
interface_library = "mylib.lib", # An import library for mylib.dll
shared_library = "mylib.dll",
)
Unix 和 Windows 的其餘部分相同:
# first will link to libmylib.a (or libmylib.lib)
cc_binary(
name = "first",
srcs = ["first.cc"],
deps = [":mylib"],
linkstatic = True, # default value
)
# second will link to libmylib.so (or libmylib.lib)
cc_binary(
name = "second",
srcs = ["second.cc"],
deps = [":mylib"],
linkstatic = False,
)
cc_import
支援 include 屬性。例如:
cc_import(
name = "curl_lib",
hdrs = glob(["vendor/curl/include/curl/*.h"]),
includes = ["vendor/curl/include"],
shared_library = "vendor/curl/lib/.libs/libcurl.dylib",
)
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
deps
|
標籤清單;預設值為 deps
有關大多數建構規則定義的典型屬性的一般註解。
|
hdrs
|
標籤清單;預設值為 |
alwayslink
|
布林值;預設值為 如果 alwayslink 無法在 Windows 上的 VS 2017 運作,這是已知問題,請將 VS 2017 升級至最新版本。 |
includes
|
字串清單;預設值為 -isystem path_to_package/include_entry 。這項功能僅適用於不符合 Google 撰寫 #include 陳述式樣式的第三方程式庫。與 COPTS 不同,這些標記會新增至這項規則和所有依附於這項規則的規則。(注意:不是規則所依據的規則!)請務必謹慎操作,因為這項設定可能影響範圍廣泛。如有疑問,請在 COPTS 中加入「-I」旗標。
預設 |
interface_library
|
標籤;預設值為 允許的檔案類型:
|
linkopts
|
字串清單;預設值為 LINKOPTS ,然後連結二進位目標。
如果這份清單中的元素不是以 |
objects
|
標籤清單;預設值為 |
pic_objects
|
標籤清單;預設值為 |
pic_static_library
|
標籤;預設值為 |
shared_library
|
標籤;預設值為 允許的檔案類型:
|
static_library
|
標籤;預設值為 允許的檔案類型:
|
strip_include_prefix
|
字串;預設值為 設定後,即可在路徑中存取這項規則 如果是相對路徑,系統會將其視為套件相對路徑。如果是絕對路徑,系統會解讀為存放區相對路徑。 在移除這個前置字元後,系統會將 這項屬性僅在 |
system_provided
|
布林值;預設值為 interface_library ,並將 shared_library 留空。 |
cc_library
查看規則來源cc_library(name, deps, srcs, data, hdrs, additional_compiler_inputs, additional_linker_inputs, alwayslink, compatible_with, conlyopts, copts, cxxopts, defines, deprecation, exec_compatible_with, exec_group_compatible_with, exec_properties, features, hdrs_check, implementation_deps, include_prefix, includes, licenses, linkopts, linkstamp, linkstatic, local_defines, module_interfaces, package_metadata, restricted_to, strip_include_prefix, tags, target_compatible_with, testonly, textual_hdrs, toolchains, visibility, win_def_file)
使用 cc_library()
處理以 C++ 編譯的程式庫。
視需求而定,結果可能是 .so
、.lo
或 .a
。
如果您使用靜態連結建構依附於 cc_library
的項目,依附元件程式庫規則的輸出內容就是 .a
檔案。如果指定 alwayslink=True
,您會取得 .lo
檔案。
共用程式庫的實際輸出檔案名稱為 libfoo.so
,其中 foo 是規則的名稱。其他種類的程式庫則分別以 .lo
和 .a
結尾。如果需要特定共用程式庫名稱 (例如定義 Python 模組),請使用 genrule 將程式庫複製到所需名稱。
檢查是否包含標頭
建構作業中使用的所有標頭檔案都必須在 hdrs
或 srcs
的 cc_*
規則中宣告。
這項規定會強制執行。
如果是 cc_library
規則,hdrs
中的標頭會組成程式庫的公開介面,可直接從程式庫本身的 hdrs
和 srcs
檔案,以及 cc_*
規則的 hdrs
和 srcs
檔案中納入,這些規則會在 deps
中列出程式庫。srcs
中的標頭只能直接從程式庫本身的 hdrs
和 srcs
檔案中納入。決定是否要將標頭放入 hdrs
或 srcs
時,請先確認您是否希望這個程式庫的使用者能夠直接納入標頭。這項決策與程式語言中 public
和 private
可見性之間的決策大致相同。
cc_binary
和 cc_test
規則沒有匯出的介面,因此也沒有 hdrs
屬性。屬於二進位檔或測試的所有標頭都應直接列在 srcs
中。
如要說明這些規則,請參閱下列範例。
cc_binary(
name = "foo",
srcs = [
"foo.cc",
"foo.h",
],
deps = [":bar"],
)
cc_library(
name = "bar",
srcs = [
"bar.cc",
"bar-impl.h",
],
hdrs = ["bar.h"],
deps = [":baz"],
)
cc_library(
name = "baz",
srcs = [
"baz.cc",
"baz-impl.h",
],
hdrs = ["baz.h"],
)
下表列出這個範例中允許直接加入的項目。
舉例來說,foo.cc
可以直接包含 foo.h
和 bar.h
,但不能包含 baz.h
。
包括檔案 | 允許的內含項目 |
---|---|
foo.h | bar.h |
foo.cc | foo.h bar.h |
bar.h | bar-impl.h baz.h |
bar-impl.h | bar.h baz.h |
bar.cc | bar.h bar-impl.h baz.h |
baz.h | baz-impl.h |
baz-impl.h | baz.h |
baz.cc | baz.h baz-impl.h |
納入檢查規則僅適用於直接納入的項目。在上述範例中,foo.cc
可包含 bar.h
,而 bar.h
可能包含 baz.h
,baz.h
則可包含 baz-impl.h
。從技術上來說,.cc
檔案的編譯作業可能會遞移地包含 hdrs
或 srcs
中任何 cc_library
的標頭檔案,而這些檔案位於遞移 deps
封閉中。在這種情況下,編譯器可能會在編譯 foo.cc
時讀取 baz.h
和 baz-impl.h
,但 foo.cc
不得包含 #include "baz.h"
。如要允許這麼做,必須將 baz
新增至 foo
的 deps
。
Bazel 依附於工具鍊支援,以強制執行納入檢查規則。
工具鍊必須支援 layering_check
功能,且必須明確要求,例如透過 --features=layering_check
指令列標記或 package
函式的 features
參數。Bazel 提供的工具鍊僅支援 Unix 和 macOS 上的 clang。
範例
我們使用 alwayslink
標記,強制連結器連結這段程式碼,即使主要二進位碼未參照該程式碼也一樣。
cc_library(
name = "ast_inspector_lib",
srcs = ["ast_inspector_lib.cc"],
hdrs = ["ast_inspector_lib.h"],
visibility = ["//visibility:public"],
deps = ["//third_party/llvm/llvm/tools/clang:frontend"],
# alwayslink as we want to be able to call things in this library at
# debug time, even if they aren't used anywhere in the code.
alwayslink = True,
)
以下範例來自
third_party/python2_4_3/BUILD
。
部分程式碼使用 dl
程式庫 (載入另一個動態程式庫),因此這項規則會指定 -ldl
連結選項,連結 dl
程式庫。
cc_library(
name = "python2_4_3",
linkopts = [
"-ldl",
"-lutil",
],
deps = ["//third_party/expat"],
)
以下範例來自 third_party/kde/BUILD
。
我們會在倉庫中保留預先建構的 .so
檔案。
標頭檔案位於名為 include
的子目錄中。
cc_library(
name = "kde",
srcs = [
"lib/libDCOP.so",
"lib/libkdesu.so",
"lib/libkhtml.so",
"lib/libkparts.so",
...more .so files... ,
],
includes = ["include"],
deps = ["//third_party/X11"],
)
以下範例來自 third_party/gles/BUILD
。
第三方程式碼通常需要一些 defines
和
linkopts
。
cc_library(
name = "gles",
srcs = [
"GLES/egl.h",
"GLES/gl.h",
"ddx.c",
"egl.c",
],
defines = [
"USE_FLOAT",
"__GL_FLOAT",
"__GL_COMMON",
],
linkopts = ["-ldl"], # uses dlopen(), dl library
deps = [
"es",
"//third_party/X11",
],
)
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
deps
|
標籤清單;預設值為 這些可以是 如需一般註解,請參閱 這些應該是 C++ 程式庫規則的名稱。
建構連結這個規則程式庫的二進位檔時,您也會連結 雖然名稱為「deps」,但並非所有這個程式庫的用戶端都屬於這裡。執行階段資料依附元件屬於 如要連結預先編譯的第三方程式庫,請將其名稱新增至 如要依附某個項目,但不要將其連結至這個程式庫,請將該項目的名稱新增至 |
srcs
|
標籤清單;預設值為 系統會編譯所有 純組語檔案 (.s、.asm) 不會經過前置處理,通常是使用組語建構。預先處理的組語檔案 (.S) 會經過預先處理,通常是使用 C/C++ 編譯器建構。 系統不會編譯 所有
如果
允許的
... 和產生這些檔案的任何規則 (例如 |
data
|
標籤清單;預設值為 data
有關大多數建構規則定義的典型屬性的一般註解。
如果 如果 您的 C++ 程式碼可以存取這些資料檔案,如下所示:
|
hdrs
|
標籤清單;預設值為 這是宣告標頭檔案的強烈建議位置,可說明程式庫的介面。這些標頭可供這項規則或相依規則中的來源納入。不應由這個程式庫的用戶端納入的標頭,應列在 允許的 |
additional_compiler_inputs
|
標籤清單;預設值為 |
additional_linker_inputs
|
標籤清單;預設值為
舉例來說,這裡可以提供已編譯的 Windows .res 檔案,以便嵌入二進位目標。 |
alwayslink
|
布林值;預設值為 srcs 中列出的所有檔案物件檔案,即使部分檔案不含二進位檔參照的符號也一樣。如果二進位檔中的程式碼未明確呼叫您的程式碼,例如您的程式碼註冊接收某項服務提供的某些回呼,這就很有用。如果 alwayslink 無法在 Windows 上的 VS 2017 運作,這是已知問題,請將 VS 2017 升級至最新版本。 |
conlyopts
|
字串清單;預設值為 |
copts
|
字串清單;預設值為
這個屬性中的每個字串都會依指定順序新增至
如果套件宣告 feature
|
cxxopts
|
字串清單;預設值為 |
defines
|
字串清單;預設值為 -D 為前置字元,然後新增至這個目標的編譯指令列,以及每個依附於這個目標的規則。請務必謹慎操作,因為這可能會造成深遠影響,定義會新增至依附於這個目標的每個目標。如有疑問,請改為在 local_defines 中新增定義值。 |
hdrs_check
|
字串;預設值為 |
implementation_deps
|
標籤清單;預設值為 deps 不同,這些程式庫 (以及所有遞移依附元件) 的標頭和包含路徑僅用於編譯這個程式庫,而非依附於這個程式庫的程式庫。以 implementation_deps 指定的程式庫仍會連結至依附於這個程式庫的二進位目標。 |
include_prefix
|
字串;預設值為 設定後,即可在規則的 系統會先移除 這項屬性僅在 |
includes
|
字串清單;預設值為 -isystem path_to_package/include_entry 。這項功能僅適用於不符合 Google 撰寫 #include 陳述式樣式的第三方程式庫。與 COPTS 不同,這些標記會新增至這項規則和所有依附於這項規則的規則。(注意:不是規則所依據的規則!)請務必謹慎操作,因為這項設定可能影響範圍廣泛。如有疑問,請在 COPTS 中加入「-I」旗標。
新增的 |
linkopts
|
字串清單;預設值為 cc_binary.linkopts 。
linkopts 屬性也會套用至任何直接或間接依附於這個程式庫的目標 (透過 deps 屬性,或透過其他類似的屬性:cc_binary 的 malloc 屬性)。相依性 linkopts 的優先順序高於依附的 linkopts (也就是相依性 linkopts 會顯示在指令列的後方)。在 --linkopt 中指定的 Linkopts 優先於規則 Linkopts。
請注意, 此外,請務必注意,系統不支援「-Wl,-soname」或「-Xlinker -soname」選項,因此請勿在這個屬性中指定這些選項。 |
linkstamp
|
標籤;預設值為 base 套件中用到。
|
linkstatic
|
布林值;預設值為 cc_binary 和 cc_test ,請以靜態模式連結二進位檔。如為 cc_library.link_static ,請參閱下文。
這項功能預設為
如果啟用這個選項,且這是二進位檔或測試,這個選項會告知建構工具盡可能連結使用者程式庫的 連結可執行檔的方法其實有三種:
如果
如果用於
在正式環境中,以 |
local_defines
|
字串清單;預設值為 -D 為前置字元,然後加入這個目標的編譯指令列,但不會加入其依附元件。與 defines 不同的是,定義只會新增至這個目標的編譯命令列。 |
module_interfaces
|
標籤清單;預設值為 C++ 標準對模組介面副檔名沒有限制
使用時須遵守 |
strip_include_prefix
|
字串;預設值為 設定後,即可在路徑中存取這項規則 如果是相對路徑,系統會將其視為套件相對路徑。如果是絕對路徑,系統會解讀為存放區相對路徑。 在移除這個前置字元後,系統會將 這項屬性僅在 |
textual_hdrs
|
標籤清單;預設值為 這是用來宣告無法自行編譯的標頭檔案位置;也就是說,這些檔案一律需要由其他來源檔案以文字形式納入,才能建構有效的程式碼。 |
win_def_file
|
標籤;預設值為 只有在 Windows 是目標平台時,才應使用這項屬性。 連結共用程式庫時,可用於 匯出符號。 |
cc_shared_library
查看規則來源cc_shared_library(name, deps, additional_linker_inputs, compatible_with, deprecation, dynamic_deps, exec_compatible_with, exec_group_compatible_with, exec_properties, exports_filter, features, package_metadata, restricted_to, roots, shared_lib_name, static_deps, tags, target_compatible_with, testonly, toolchains, user_link_flags, visibility, win_def_file)
並產生共用程式庫。
範例
cc_shared_library( name = "foo_shared", deps = [ ":foo", ], dynamic_deps = [ ":bar_shared", ], additional_linker_inputs = [ ":foo.lds", ], user_link_flags = [ "-Wl,--version-script=$(location :foo.lds)", ], ) cc_library( name = "foo", srcs = ["foo.cc"], hdrs = ["foo.h"], deps = [ ":bar", ":baz", ], ) cc_shared_library( name = "bar_shared", shared_lib_name = "bar.so", deps = [":bar"], ) cc_library( name = "bar", srcs = ["bar.cc"], hdrs = ["bar.h"], ) cc_library( name = "baz", srcs = ["baz.cc"], hdrs = ["baz.h"], )
在範例中,foo_shared
會靜態連結 foo
和 baz
,後者是遞移依附元件。系統不會連結 bar
,因為 dynamic_dep
bar_shared
已動態提供該連結。
foo_shared
會使用連結器指令碼 *.lds 檔案,控管要匯出的符號。cc_shared_library
規則邏輯不會控管要匯出哪些符號,只會使用假設要匯出的內容,在分析階段中,如果兩個共用程式庫匯出相同的目標,就會顯示錯誤。
系統會假設 cc_shared_library
的每個直接依附元件都已匯出。因此,Bazel 會在分析期間假設 foo_shared
正在匯出 foo
。baz
不會由 foo_shared
匯出。系統也會假設 exports_filter
比對到的每個目標都已匯出。
範例中的每個 cc_library
最多只能出現在一個 cc_shared_library
中。如果我們也想將 baz
連結到 bar_shared
,就需要在 baz
中加入 tags = ["LINKABLE_MORE_THAN_ONCE"]
。
由於 shared_lib_name
屬性,bar_shared
產生的檔案會命名為 bar.so
,而不是 Linux 預設的 libbar.so
。
錯誤
Two shared libraries in dependencies export the same symbols.
只要您建立的目標有兩個不同的cc_shared_library
依附元件匯出相同目標,就會發生這種情況。如要修正這個問題,請在其中一個 cc_shared_library
依附元件中停止匯出程式庫。
Two shared libraries in dependencies link the same library statically
當您使用兩個不同的 cc_shared_library
依附元件建立新的 cc_shared_library
,並靜態連結相同目標時,就會發生這種情況。與匯出錯誤類似。
如要修正這個問題,請停止將程式庫連結至其中一個cc_shared_library
依附元件。同時,仍連結該程式庫的項目必須匯出程式庫,這樣未連結的項目才能繼續查看符號。另一種方法是抽出匯出目標的第三個程式庫。第三種方式是使用 LINKABLE_MORE_THAN_ONCE
標記違規的 cc_library
,但這種修正方式不常見,而且您絕對要確保 cc_library
確實可多次連結。
'//foo:foo' is already linked statically in '//bar:bar' but not exported`
也就是說,您 deps
的遞移閉包中的程式庫可存取,不必經過其中一個 cc_shared_library
依附元件,但已連結至 dynamic_deps
中的不同 cc_shared_library
,且未匯出。
解決方法是從 cc_shared_library
依附元件匯出,或是提取匯出該元件的第三方 cc_shared_library
。
Do not place libraries which only contain a precompiled dynamic library in deps.
如果您有預先編譯的動態程式庫,則不需要也不會靜態連結至目前建立的 cc_shared_library
目標。因此不屬於 cc_shared_library
的 deps
。如果這個預先編譯的動態程式庫是其中一個 cc_libraries
的依附元件,則 cc_library
需要直接依附該程式庫。
Trying to export a library already exported by a different shared library
如果您在目前的規則中聲明要匯出目標,但該目標已由其中一個動態依附元件匯出,就會看到這則錯誤訊息。
如要修正這個問題,請從 deps
中移除目標,並只依賴動態依附元件,或確保 exports_filter
不會擷取這個目標。
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
deps
|
標籤清單;預設值為
只要這些直接依附元件的任何遞移程式庫依附元件尚未由
在分析期間,規則實作會將
如果將同一個程式庫靜態連結至多個 |
additional_linker_inputs
|
標籤清單;預設值為 user_link_flags 屬性執行這項操作。
|
dynamic_deps
|
標籤清單;預設值為 cc_shared_library 依附元件。
|
exports_filter
|
字串清單;預設值為
任何目標
請注意,這項屬性不會實際將依附項邊緣新增至這些目標,依附項邊緣應改由 允許使用的語法如下:
|
roots
|
標籤清單;預設值為 |
shared_lib_name
|
字串;預設值為 |
static_deps
|
字串清單;預設值為 |
user_link_flags
|
字串清單;預設值為
|
win_def_file
|
標籤;預設值為 只有在 Windows 是目標平台時,才應使用這項屬性。 連結共用程式庫時,可用於 匯出符號。 |
cc_static_library
查看規則來源cc_static_library(name, deps, compatible_with, deprecation, exec_compatible_with, exec_group_compatible_with, exec_properties, features, package_metadata, restricted_to, tags, target_compatible_with, testonly, toolchains, visibility)
產生的靜態程式庫包含 deps
中列出的目標物件檔案,以及這些檔案的遞移依附元件,並優先使用 PIC
物件。
輸出群組
linkdeps
文字檔,內含 deps
中列出目標的遞移依附元件標籤,這些依附元件未將任何物件檔案提供給靜態程式庫,但至少提供一個靜態、動態或介面程式庫。產生的靜態程式庫可能需要在連結時提供這些程式庫。
linkopts
文字檔,內含 deps
中列出目標的所有遞移依附元件的使用者提供 linkopts
。
重複符號
根據預設,cc_static_library
規則會檢查產生的靜態程式庫是否含有重複符號。如果發生這種情況,建構作業會失敗,並顯示錯誤訊息,列出重複的符號和包含這些符號的物件檔案。
您可以透過設定 features = ["-symbol_check"]
停用每個目標或套件的這項檢查,也可以透過 --features=-symbol_check
全域停用。
symbol_check
的工具鍊支援
Bazel 隨附的自動設定 C++ 工具鍊支援所有平台上的 symbol_check
功能。自訂工具鍊可透過下列兩種方式之一新增支援:
- 實作
ACTION_NAMES.validate_static_library
動作,並透過symbol_check
功能啟用該動作。系統會使用兩個引數叫用動作中設定的工具:要檢查重複符號的靜態程式庫,以及檢查通過時必須建立的檔案路徑。 symbol_check
功能會新增封存器旗標,導致建立靜態程式庫的動作因符號重複而失敗。
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
deps
|
標籤清單;預設值為 不提供任何物件檔案的依附元件不會納入靜態程式庫,但其標籤會收集在 |
cc_test
查看規則來源cc_test(name, deps, srcs, data, additional_linker_inputs, args, compatible_with, conlyopts, copts, cxxopts, defines, deprecation, dynamic_deps, env, env_inherit, exec_compatible_with, exec_group_compatible_with, exec_properties, features, flaky, hdrs_check, includes, licenses, link_extra_lib, linkopts, linkshared, linkstatic, local, local_defines, malloc, module_interfaces, nocopts, package_metadata, reexport_deps, restricted_to, shard_count, size, stamp, tags, target_compatible_with, testonly, timeout, toolchains, visibility, win_def_file)
cc_test()
規則會編譯測試。這裡的測試是某些測試程式碼的二進位包裝函式。
C++ 測試預設會動態連結。
如要靜態連結單元測試,請指定
linkstatic=True
。
最好註解說明測試需要 linkstatic
的原因,這可能不是顯而易見的事。
隱含輸出目標
name.stripped
(僅在明確要求時建構):二進位檔的已移除版本。strip -g
會在二進位檔上執行,以移除偵錯符號。您可以使用--stripopt=-foo
,在指令列中提供其他剝除選項。name.dwp
(僅在明確要求時建構):如果啟用 Fission,則為適合用來偵錯遠端部署二進位檔的偵錯資訊套件檔案。否則:空白檔案。
請參閱 cc_binary() 引數,但測試的 stamp
引數預設為 0,且 cc_test
具有所有測試規則 (*_test) 通用的額外
屬性。
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
deps
|
標籤清單;預設值為 這些可以是 linkopts 中參照這些指令碼,但請考慮該用途的 additional_linker_inputs 。 |
srcs
|
標籤清單;預設值為 系統會編譯所有 純組語檔案 (.s、.asm) 不會經過前置處理,通常是使用組語建構。預先處理的組語檔案 (.S) 會經過預先處理,通常是使用 C/C++ 編譯器建構。 系統不會編譯 所有
如果
允許的
... 和產生這些檔案的任何規則 (例如 |
data
|
標籤清單;預設值為 data
有關大多數建構規則定義的典型屬性的一般註解。
如果 如果 您的 C++ 程式碼可以存取這些資料檔案,如下所示:
|
additional_linker_inputs
|
標籤清單;預設值為
舉例來說,這裡可以提供已編譯的 Windows .res 檔案,以便嵌入二進位目標。 |
conlyopts
|
字串清單;預設值為 |
copts
|
字串清單;預設值為
這個屬性中的每個字串都會依指定順序新增至
如果套件宣告 feature
|
cxxopts
|
字串清單;預設值為 |
defines
|
字串清單;預設值為 -D 為前置字元,然後新增至這個目標的編譯指令列,以及每個依附於這個目標的規則。請務必謹慎操作,因為這可能會造成深遠影響,定義會新增至依附於這個目標的每個目標。如有疑問,請改為在 local_defines 中新增定義值。 |
dynamic_deps
|
標籤清單;預設值為 cc_shared_library 依附元件。
|
hdrs_check
|
字串;預設值為 |
includes
|
字串清單;預設值為 -isystem path_to_package/include_entry 。這項功能僅適用於不符合 Google 撰寫 #include 陳述式樣式的第三方程式庫。與 COPTS 不同,這些標記會新增至這項規則和所有依附於這項規則的規則。(注意:不是規則所依據的規則!)請務必謹慎操作,因為這項設定可能影響範圍廣泛。如有疑問,請在 COPTS 中加入「-I」旗標。
新增的 |
link_extra_lib
|
標籤;預設值為
根據預設,C++ 二進位檔會連結至 |
linkopts
|
字串清單;預設值為 LINKOPTS ,然後連結二進位目標。
如果這份清單中的元素不是以 |
linkshared
|
布林值;預設值為 linkshared=True 。這項功能預設為關閉。
這個標記表示連結會使用
如果您同時指定 |
linkstatic
|
布林值;預設值為 cc_binary 和 cc_test ,請以靜態模式連結二進位檔。如為 cc_library.link_static ,請參閱下文。
這項功能預設為
如果啟用這個選項,且這是二進位檔或測試,這個選項會告知建構工具盡可能連結使用者程式庫的 連結可執行檔的方法其實有三種:
如果
如果用於
在正式環境中,以 |
local_defines
|
字串清單;預設值為 -D 為前置字元,然後加入這個目標的編譯指令列,但不會加入其依附元件。與 defines 不同的是,定義只會新增至這個目標的編譯命令列。 |
malloc
|
標籤;預設值為
根據預設,C++ 二進位檔會連結至 |
module_interfaces
|
標籤清單;預設值為 C++ 標準對模組介面副檔名沒有限制
使用時須遵守 |
nocopts
|
字串;預設值為 COPTS (包括規則 copts 屬性中明確指定的值),都會從 COPTS 中移除,以編譯這項規則。這個屬性不應在 third_party 以外使用,也不應有此需求。除了「Make」變數替換之外,這些值不會經過任何前置處理。 |
reexport_deps
|
標籤清單;預設值為 |
stamp
|
整數;預設值為
除非依附元件有所變更,否則系統不會重建蓋章的二進位檔。 |
win_def_file
|
標籤;預設值為 只有在 Windows 是目標平台時,才應使用這項屬性。 連結共用程式庫時,可用於 匯出符號。 |
cc_toolchain
查看規則來源cc_toolchain(name, all_files, ar_files, as_files, compatible_with, compiler_files, compiler_files_without_includes, coverage_files, deprecation, dwp_files, dynamic_runtime_lib, exec_compatible_with, exec_group_compatible_with, exec_properties, exec_transition_for_inputs, features, libc_top, licenses, linker_files, module_map, objcopy_files, output_licenses, package_metadata, restricted_to, static_runtime_lib, strip_files, supports_header_parsing, supports_param_files, tags, target_compatible_with, testonly, toolchain_config, toolchain_identifier, toolchains, visibility)
代表 C++ 工具鍊。
這項規則負責:
-
收集 C++ 動作執行所需的所有構件。方法是使用
all_files
、compiler_files
、linker_files
等屬性,或是以_files
結尾的其他屬性。這些屬性最常見於檔案群組,會將所有必要檔案 globbing。 -
為 C++ 動作產生正確的命令列。這項操作是使用
CcToolchainConfigInfo
提供者 (詳情請見下文) 完成。
使用 toolchain_config
屬性設定 C++ 工具鍊。
如需詳細的 C++ 工具鍊設定和工具鍊選取說明文件,請參閱
這個頁面
。
使用 tags = ["manual"]
,避免在叫用 bazel build //...
時不必要地建構及設定工具鍊
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
all_files
|
標籤 (必填) 所有 cc_toolchain 構件的集合。這些構件會新增為所有 rules_cc 相關動作的輸入內容 (但使用下列屬性中更精確構件集的動作除外)。Bazel 假設all_files 是所有其他提供構件的屬性 (例如,linkstamp 編譯需要編譯和連結檔案,因此會採用 all_files ) 的超集。
這是 |
ar_files
|
標籤;預設值為 |
as_files
|
標籤;預設值為 |
compiler_files
|
標籤 (必填) 收集編譯動作所需的所有 cc_toolchain 構件。 |
compiler_files_without_includes
|
標籤;預設值為 |
coverage_files
|
標籤;預設值為 |
dwp_files
|
標籤 (必填) 收集 dwp 動作所需的所有 cc_toolchain 構件。 |
dynamic_runtime_lib
|
標籤;預設值為 啟用「static_link_cpp_runtimes」功能,並動態連結依附元件時,就會使用這個值。 |
exec_transition_for_inputs
|
布林值;預設值為 |
libc_top
|
標籤;預設值為 |
linker_files
|
標籤 (必填) 收集連結動作所需的所有 cc_toolchain 構件。 |
module_map
|
標籤;預設值為 |
objcopy_files
|
標籤 (必填) 收集 objcopy 動作所需的所有 cc_toolchain 構件。 |
output_licenses
|
字串清單;預設值為 |
static_runtime_lib
|
標籤;預設值為 啟用「static_link_cpp_runtimes」功能並靜態連結依附元件時,系統會使用這個值。 |
strip_files
|
標籤 (必填) 所有 strip 動作所需的 cc_toolchain 構件集合。 |
supports_header_parsing
|
布林值;預設值為 |
supports_param_files
|
布林值;預設值為 |
toolchain_config
|
標籤 (必填) 提供cc_toolchain_config_info 的規則標籤。
|
toolchain_identifier
|
字串;預設值為
在問題 #5380 修正前,建議您使用這種方式將 |
cc_toolchain_suite
查看規則來源cc_toolchain_suite(name, compatible_with, deprecation, features, licenses, package_metadata, restricted_to, tags, target_compatible_with, testonly, toolchains, visibility)
已淘汰:這項規則不會執行任何作業,日後將移除。
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
fdo_prefetch_hints
查看規則來源fdo_prefetch_hints(name, compatible_with, deprecation, exec_compatible_with, exec_group_compatible_with, exec_properties, features, package_metadata, profile, restricted_to, tags, target_compatible_with, testonly, toolchains, visibility)
代表工作區中的 FDO 預先擷取提示設定檔。範例:
fdo_prefetch_hints(
name = "hints",
profile = "//path/to/hints:profile.afdo",
)
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
profile
|
標籤 (必填) 提示設定檔的標籤,提示檔案的副檔名為 .afdo 標籤也可以指向 fdo_absolute_path_profile 規則。 |
fdo_profile
查看規則來源fdo_profile(name, compatible_with, deprecation, exec_compatible_with, exec_group_compatible_with, exec_properties, features, memprof_profile, package_metadata, profile, proto_profile, restricted_to, tags, target_compatible_with, testonly, toolchains, visibility)
代表工作區中的 FDO 設定檔。 範例:
fdo_profile(
name = "fdo",
profile = "//path/to/fdo:profile.zip",
)
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
memprof_profile
|
標籤;預設值為 |
profile
|
標籤 (必填) 產生 FDO 設定檔或規則的標籤。FDO 檔案可使用下列其中一個副檔名:未編列索引的 LLVM 設定檔為 .profraw、已編列索引的 LLVM 設定檔為 .profdata、包含 LLVM profraw 設定檔的 .zip、AutoFDO 設定檔為 .afdo、XBinary 設定檔為 .xfdo。標籤也可以指向 fdo_absolute_path_profile 規則。 |
proto_profile
|
標籤;預設值為 |
memprof_profile
查看規則來源memprof_profile(name, compatible_with, deprecation, exec_compatible_with, exec_group_compatible_with, exec_properties, features, package_metadata, profile, restricted_to, tags, target_compatible_with, testonly, toolchains, visibility)
代表工作區中的 MEMPROF 設定檔。 範例:
memprof_profile(
name = "memprof",
profile = "//path/to/memprof:profile.afdo",
)
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
profile
|
標籤 (必填) MEMPROF 設定檔的標籤,設定檔應具有 .profdata 副檔名 (適用於已建立索引/符號的 memprof 設定檔),或是 .zip 副檔名 (適用於內含 memprof.profdata 檔案的 zip 檔案)。標籤也可以指向 fdo_absolute_path_profile 規則。 |
propeller_optimize
查看規則來源propeller_optimize(name, cc_profile, compatible_with, deprecation, exec_compatible_with, exec_group_compatible_with, exec_properties, features, ld_profile, package_metadata, restricted_to, tags, target_compatible_with, testonly, toolchains, visibility)
代表工作區中的 Propeller 最佳化設定檔。 範例:
propeller_optimize(
name = "layout",
cc_profile = "//path:cc_profile.txt",
ld_profile = "//path:ld_profile.txt"
)