gpt4 book ai didi

protocol-buffers - 如何在 autoconf/automake 中使用 Protocol Buffer ?

转载 作者:行者123 更新时间:2023-12-04 00:35:58 26 4
gpt4 key购买 nike

正在寻找用于构建使用 Protocol Buffer 的项目的 autoconf 和 automake 规则的好示例,将 protoc 添加到构建过程的最佳方法?

最佳答案

configure.ac
就 protobuf 库而言,它使用 pkg-config ,所以最好使用 PKG_CHECK_MODULES 来引用它。宏:

PKG_CHECK_MODULES(PROTOBUF, protobuf >= 2.4.0)
AC_SUBST(PROTOBUF_LIBS)
AC_SUBST(PROTOBUF_CFLAGS)
AC_SUBST(PROTOBUF_VERSION)

并检查 protoc路径中的命令。这是一个非常基本的检查,它在路径中:
AC_CHECK_PROG([PROTOC], [protoc], [protoc])
AS_IF([test "x${PROTOC}" == "x"],
[AC_MSG_ERROR([ProtoBuf compiler "protoc" not found.])])

或者,允许用户指定不同的 protoc--with-protoc=/path/to/protoc或使用环境变量 PROTOC :
# ProtoBuf compiler.
# First, specify with --with-protoc=/path/of/protoc.
# Or, specify with env variable PROTOC.
# If neither of the above, find it in the path.
#AC_MSG_CHECKING([for ProtoBuf compiler protoc])
AC_ARG_WITH([protoc],
[AS_HELP_STRING([--with-protoc=/path/of/protoc],
[Location of the protocol buffers compiler protoc. Defaults to looking on path.])],
[PROTOC="$withval"],
[ AS_IF([test "x${PROTOC}" == "x"],
[AC_PATH_PROG([PROTOC], [protoc], [no])])
]
)
#AC_MSG_RESULT([${PROTOC}])
AS_IF([test "${PROTOC}" == "no"], [AC_MSG_ERROR([ProtoBuf compiler "protoc" not found.])])
Makefile.am
添加规则以构建 proto文件:
%.pb.cc %.pb.h: %.proto
$(PROTOC) --proto_path=$(srcdir) --cpp_out=$(builddir) $^

使用 dist_noinst_DATA 指定 protobuf 源文件.这对于确保它们被捆绑在源代码分发中是必要的 .tar.gzmake dist 制作的文件.
dist_noinst_DATA = whatever.proto

(注意:对于较新版本的 autoconf/automake,可能需要使用 @builddir@ 而不是 $(builddir) 。)

使用 nodist_ 指定生成的文件前缀和 $(builddir)路径:
nodist_myprog_SOURCES = $(builddir)/whatever.pb.cc $(builddir)/whatever.pb.h

并用 make clean 清洁它们:
MOSTLYCLEANFILES = whatever.pb.cc whatever.pb.h

使用 BUILT_SOURCES处理构建头文件的依赖关系:
BUILT_SOURCES = whatever.pb.h

您的编译器标志可能需要引用构建目录来查找头文件(以在 VPATH 构建中工作):
AM_CPPFLAGS += -I$(builddir)

关于protocol-buffers - 如何在 autoconf/automake 中使用 Protocol Buffer ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13939904/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com