linux下使用ProtoBuf, 有如下几种安装方式。
configure方式源码编译安装,发现使用的时候cmake找不到protobuf。
于是采用了 cmake 源码的安装方式。
1. 直接安装方式
安装: sudo apt-get install libprotobuf-dev protobuf-compiler
卸载: sudo apt-get remove libprotobuf-dev protobuf-compiler
2. configure方式源码编译安装
sudo apt-get install autoconf automake libtool curl make g++ unzip
git clone https://github.com/google/protobuf.git
cd protobuf
git submodule update --init --recursive
./autogen.sh
./configure --prefix=/usr/local/protobuf # 最好加上后面这个prefix,方便删除
make
make check
sudo make install
sudo ldconfig # refresh shared library cache.
参考链接:
https://www.cnblogs.com/learn21cn/p/6206206.html
//www.greatytc.com/p/05e91bb8506f
设置环境变量
# (动态库搜索路径) 程序加载运行期间查找动态链接库时指定除了系统默认路径之外的其他路径
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf/lib
# (静态库搜索路径) 程序编译期间查找动态链接库时指定查找共享库的路径
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/protobuf/lib
export PATH=$PATH:/usr/local/protobuf/bin
缺点:
发现上述方式安装之后,没有cmake的配置文件,所以cmake配置的时候导致cmake找不到
3. cmake方式源码编译安装
为了方便后续使用cmake方式使用,于是尝试用cmake和make的方式去编译安装:
sudo apt-get install make cmake
# 下载源码包
git clone https://github.com/google/protobuf.git
cd protobuf
git submodule update --init --recursive
# 以cmake make方式进行源码编译
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX="/home/temp/program/protobuf" ../cmake
make -j${nproc}
make install
也可以使用cmake GUI的方式进行cmake配置,如下:
注意其中 CMAKE_INSTALL_PREFIX
的设置,设置之后方便以后使用和删除
4. 例子
文件树
.
├── CMakeLists.txt
├── proto
│ └── test.proto
└── src
└── main.cpp
4.1 proto/test.proto
syntax = "proto2";
package test;
message something
{
optional int32 num=1;
repeated string strs = 2;
}
4.2 src/main.cpp
#include "test.pb.h"
int main()
{
test::something msg;
msg.set_num(42);
msg.add_strs("hello");
msg.add_strs("world");
msg.PrintDebugString();
return 0;
}
4.3 CMakeLists.txt (linux)
cmake_minimum_required(VERSION 3.5.1)
project(cmake_protobuf_test)
# ------------------------------------------------------------------
# Check C++11 or C++0x support
# ------------------------------------------------------------------
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_definitions(-DCOMPILEDWITHC11)
message(STATUS "Using flag -std=c++11.")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
# ------------------------------------------------------------------
# protobuf
# ------------------------------------------------------------------
set(Protobuf_DIR "/home/temp/program/protobuf2/lib/cmake/protobuf") # 注意配置成自己的路径
set(protobuf_MODULE_COMPATIBLE ON CACHE BOOL "")
find_package(Protobuf REQUIRED CONFIG)
# find_package(Protobuf REQUIRED)
if (PROTOBUF_FOUND)
message("protobuf found")
else ()
message(FATAL_ERROR "Cannot find Protobuf")
endif ()
# Compile the .proto file into .cpp and .h file
set(PROTO_FILES proto/test.proto)
PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS ${PROTO_FILES})
message("PROTO_SRCS = ${PROTO_SRCS}")
message("PROTO_HDRS = ${PROTO_HDRS}")
message("PROTOBUF_LIBRARIES = ${PROTOBUF_LIBRARIES}")
# ------------------------------------------------------------------
# source files
# ------------------------------------------------------------------
include_directories("${CMAKE_SOURCE_DIR}/src")
AUX_SOURCE_DIRECTORY("${CMAKE_SOURCE_DIR}/src" PROJECT_SRCS)
FILE(GLOB PROJECT_HDRS ${CMAKE_SOURCE_DIR}/src/*.h ${CMAKE_SOURCE_DIR}/src/*.hpp)
# ------------------------------------------------------------------
# ${PROJECT_NAME}
# ------------------------------------------------------------------
add_executable(${PROJECT_NAME} ${PROJECT_SRCS} ${PROJECT_HDRS}
${PROTO_SRCS} ${PROTO_HDRS})
target_link_libraries(${PROJECT_NAME} ${PROTOBUF_LIBRARIES} pthread)
target_include_directories(${PROJECT_NAME}
PUBLIC ${CMAKE_CURRENT_BINARY_DIR}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
PUBLIC ${PROTOBUF_INCLUDE_DIRS})
注意上面链接的时候要加上pthread,否则编译通过,运行时报错,报错信息如下:
[libprotobuf FATAL /home/temp/code/test/protobuf/src/google/protobuf/generated_message_util.cc:795] CHECK failed: (scc->visit_status.load(std::memory_order_relaxed)) == (SCCInfoBase::kRunning):
terminate called after throwing an instance of 'google::protobuf::FatalException'
what(): CHECK failed: (scc->visit_status.load(std::memory_order_relaxed)) == (SCCInfoBase::kRunning):
4.4 linux下运行
mkdir build
cd build
cmake ..
make
./cmake_protobuf_test
然后就能输出以下信息:
num: 42
strs: "hello"
strs: "world"