一、bazel编译成single target
(1)文件目录
WORKSPACE
main
BUILD
print.h
print.cpp
demo.cpp
(2)文件内容
BUILD
cc_library(
name = "print_h",
hdrs = ["print.h"],
)
cc_binary(
name = "demo",
srcs = ["demo.cpp", "print.cpp"],
deps = ["print_h"],
)
print.h
#include<stdio.h>
void print(void);
print.cpp
#include "print.h"
void print() {
printf("hello world\n");
}
demo.cpp
#include "print.h"
#include <stdio.h>
int main(int argc, char* argv[]) {
print();
return 0;
}
(3)编译命令文章来源地址https://www.toymoban.com/news/detail-745849.html
bazel build //main:demo
二、bazel编译成multi target
(1)文件目录
WORKSPACE
main
BUILD
print.h
print.cpp
demo.cpp
(2)文件内容
BUILD
cc_library(
name="print",
srcs=["print.cpp"],
hdrs=["print.h"],
)
cc_binary(
name="demo",
srcs=["demo.cpp"],
deps=[":print"],
)
print.h
#include<stdio.h>
void print(void);
print.cpp
#include "print.h"
void print() {
printf("hello world\n");
}
demo.cpp
#include "print.h"
#include <stdio.h>
int main(int argc, char* argv[]) {
print();
return 0;
}
(3)编译命令
bazel build //main:demo
三、bazel编译成multi package
(1)文件目录
WORKSPACE
lib
BUILD
print.h
print.cpp
main
BUILD
demo.cpp
(2)文件内容
lib
BUILD
cc_library(
name = "print",
srcs = ["print.cpp"],
hdrs = ["print.h"],
visibility = ["//main:__pkg__"],
)
print.h
#include<stdio.h>
void print(void);
print.cpp
#include "print.h"
void print() {
printf("hello world\n");
}
main
BUILD
cc_binary(
name="demo",
srcs=["demo.cpp"],
deps=["://lib:print"],
)
demo.cpp
#include "print.h"
#include <stdio.h>
int main(int argc, char* argv[]) {
print();
return 0;
}
(3)编译命令
bazel build //main:demo
四、bazel编译成静态库(.a文件)
1)文件目录
WORKSPACE
lib
BUILD
print.h
print.cpp
(2)文件内容
lib
BUILD
cc_library(
name="print",
srcs=["print.h","print.cpp"],
linkstatic=True
)
print.h
#include<stdio.h>
void print(void);
print.cpp
#include "print.h"
void print() {
printf("hello world\n");
}
(3)编译命令
bazel build //main:print
生成静态库 libprint.a
五、bazel编译调用静态库(.a文件)
(1)文件目录
WORKSPACE
main
BUILD
print.h
libprint.a
demo.cpp
(2)文件内容
main
BUILD
cc_import(
name="print",
hdrs=["print.h"],
static_library="libprint.a",
)
cc_binary(
name="demo",
srcs=["demo.cpp"],
deps=[":print"]
)
print.h
#include<stdio.h>
void print(void);
demo.cpp文章来源:https://www.toymoban.com/news/detail-745849.html
#include "print.h"
#include <stdio.h>
int main(int argc, char* argv[]) {
print();
return 0;
}
六、bazel编译成动态库(.so文件)
1)文件目录
WORKSPACE
lib
BUILD
print.h
print.cpp
(2)文件内容
lib
BUILD
cc_library(
name="libprint.so",
srcs=["print.h","print.cpp"],
linkshared=True
)
print.h
#include<stdio.h>
void print(void);
print.cpp
#include "print.h"
void print() {
printf("hello world\n");
}
(3)编译命令
bazel build //main:libprint.so
生成动态库 libprint.so
七、bazel编译调用动态库(.so文件)
(1)文件目录
WORKSPACE
main
BUILD
print.h
libprint.so
demo.cpp
(2)文件内容
main
BUILD
cc_import(
name="print",
hdrs=["print.h"],
shared_library="libprint.so"
)
cc_binary(
name="demo",
srcs=["demo.cpp"],
deps=[":print"]
)
print.h
#include<stdio.h>
void print(void);
demo.cpp
#include "print.h"
#include <stdio.h>
int main(int argc, char* argv[]) {
print();
return 0;
}
(3)编译命令
bazel build //main:demo
到了这里,关于bazel编译C++的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!