新聞中心
這里有您想知道的互聯(lián)網營銷解決方案
protobuf/C++安裝測試與項目中使用makefile編譯-創(chuàng)新互聯(lián)
安裝
當前文章:protobuf/C++安裝測試與項目中使用makefile編譯-創(chuàng)新互聯(lián)
文章轉載:http://www.ef60e0e.cn/article/dopjhh.html
# 依賴
sudo apt-get install build-essential
sudo apt install libtool autoconf make
sudo apt install make-guile
# 安裝
cd protobuf-3.19.4
sudo ./autogen.sh
sudo ./configure
sudo make
sudo make check
sudo make install
sudo ldconfig # refresh shared library cache
# 檢驗
protoc --version
demozcm@vm:~/Documents/protobuf_test$ tree
.
├── pb
│ ├── msg.pb.cc
│ ├── msg.pb.h
│ ├── msg.proto
└── src
└── test_protobuf.cpp
syntax = "proto3";
package demo;
message People {
string name = 1;
int32 id = 2;
string email = 3;
}
# 編譯出 pb.h pb.cc
protoc --cpp_out=. msg.proto
// 使用
#include#include"../pb/msg.pb.h"
#includeint main()
{
std::string data;
demo::People p;
p.set_name("zhang san");
p.set_id(101);
p.set_email("zhang@163.com");
p.SerializeToString(&data);
std::cout<<"serialize to string \n"<<"data size:"<
// 編譯
g++ src/test_protobuf.cpp pb/msg.pb.cc -o test -lprotobuf
運行zcm@vm:~/Documents/protobuf_test$ ./test
serialize to string
data size:32
zhang sane?zhang@163.com
parse from string
People:
Name: zhang san
ID: 101
Email: zhang@163.com
makefile編譯使用了makefile編譯整個項目,shell腳本生成與清除pb,未統(tǒng)一
zcm@vm:~/Documents/protobuf_test$ tree
.
├── build
│ └── test
├── makefile
├── pb
│ ├── clean.sh
│ ├── create.sh
│ ├── msg.pb.cc
│ ├── msg.pb.h
│ ├── msg.proto
│ ├── people.pb.cc
│ ├── people.pb.h
│ └── people.proto
└── src
├── makefile
└── test_protobuf.cpp
項目總體Makefileall:
cd pb && ./create.sh
cd src && make
clean:
cd pb && ./clean.sh
cd src && make clean
pb生成腳本與清除腳本# create.sh
BASE_DIR=..
SCR_DIR=$BASE_DIR/pb
DES_DIR=$BASE_DIR/pb
protoc --proto_path=$SCR_DIR --cpp_out=$DES_DIR $SCR_DIR/*.proto
# clean.sh
BASE_DIR=..
# SCR_DIR=$BASE_DIR/pb
DES_DIR=$BASE_DIR/pb
rm -f $DES_DIR/*.h $DES_DIR/*.cc
生成可執(zhí)行文件的Makefile.SUFFIXES: .sh .h .c .cc .cpp
# 命令
AR = ar
AR_FLAGS = crs
CXX = g++
CXXFLAGS = -std=c++11
# 地址變量
BASE_DIR = ..
BUILD_DIR = $(BASE_DIR)/build
INC_DIR = -I$(BASE_DIR)/pb
LIB_DIR =
# 依賴庫
CLIBS = -lprotobuf
CLIBS +=
# 生成可執(zhí)行文件
BIN = $(BUILD_DIR)/test
SERVER_SRCS = $(wildcard *.cpp $(BASE_DIR)/pb/*.cc)
SERVER_OBJS = $(patsubst %.cpp %.cc,%.o,$(SERVER_SRCS))
# make
all: build_dir bin
build_dir bin :$(BUILD_DIR) $(BIN)
$(BIN):$(SERVER_SRCS)
$(CXX) $(CXXFLAGS) $(INC_DIR) -o $@ $^ $(CLIBS)
$(BUILD_DIR):
@-mkdir -p $(BUILD_DIR)
# make clean
clean:
@-rm -rf $(BUILD_DIR)
運行zcm@vm:~/Documents/protobuf_test$ ./build/test
serialize to string
data size:32
zhang sane?zhang@163.com
parse from string
People:
Name: zhang san
ID: 101
Email: zhang@163.com
你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧
當前文章:protobuf/C++安裝測試與項目中使用makefile編譯-創(chuàng)新互聯(lián)
文章轉載:http://www.ef60e0e.cn/article/dopjhh.html