如何在autoconf/automake中使用协议缓冲区?

Aar*_*ron 12 automake protocol-buffers

寻找autoconf和automake规则的一个很好的例子,用于构建使用协议缓冲区的项目,将protoc添加到构建过程的最佳方法是什么?

Cra*_*een 15

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)
Run Code Online (Sandbox Code Playgroud)

并检查protoc路径中的命令.这是一个非常基本的检查,它在路径中:

AC_CHECK_PROG([PROTOC], [protoc], [protoc])
AS_IF([test "x${PROTOC}" == "x"],
    [AC_MSG_ERROR([ProtoBuf compiler "protoc" not found.])])
Run Code Online (Sandbox Code Playgroud)

或者,允许用户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.])])
Run Code Online (Sandbox Code Playgroud)

Makefile.am

添加规则以构建proto文件:

%.pb.cc %.pb.h: %.proto
    $(PROTOC) --proto_path=$(srcdir) --cpp_out=$(builddir) $^
Run Code Online (Sandbox Code Playgroud)

使用指定protobuf源文件dist_noinst_DATA.这是确保它们捆绑在一个源代码分发.tar.gz文件中所必需的make dist.

dist_noinst_DATA = whatever.proto
Run Code Online (Sandbox Code Playgroud)

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

使用nodist_前缀和$(builddir)路径指定生成的文件:

nodist_myprog_SOURCES = $(builddir)/whatever.pb.cc $(builddir)/whatever.pb.h
Run Code Online (Sandbox Code Playgroud)

并用以下方法清洁它们make clean:

MOSTLYCLEANFILES = whatever.pb.cc whatever.pb.h
Run Code Online (Sandbox Code Playgroud)

使用BUILT_SOURCES来处理内置头文件的依赖性:

BUILT_SOURCES = whatever.pb.h
Run Code Online (Sandbox Code Playgroud)

您的编译器标志可能需要引用构建目录来查找头文件(在VPATH构建中工作):

AM_CPPFLAGS += -I$(builddir)
Run Code Online (Sandbox Code Playgroud)


Aar*_*ron 7

这似乎有效:

configure.ac:

AC_ARG_WITH([protobuf-libdir],
    [AS_HELP_STRING([--with-protobuf-libdir=LIB_DIR],
        [location of the protocol buffers libraries, defaults to /usr/lib])],
    [PROTOBUF_LIBDIR="$withval"],
    [PROTOBUF_LIBDIR='/usr/lib'])
AC_SUBST([PROTOBUF_LIBDIR])

LDFLAGS="$LDFLAGS -L$PROTOBUF_LIBDIR"

AC_CHECK_LIB([protobuf], [main], [], [AC_MSG_ERROR([cannot find protobuf library])])

AC_ARG_WITH([protoc],
    [AS_HELP_STRING([--with-protoc=PATH],
        [location of the protoc protocol buffer compiler binary, defaults to protoc])],
    [PROTOC="$withval"],
    [PROTOC='protoc'])
AC_SUBST([PROTOC])
Run Code Online (Sandbox Code Playgroud)

Makefile.am:

%.pb.cc %.pb.h: %.proto
    $(PROTOC) --proto_path=$(dir $^) --cpp_out=$(dir $^) $^
Run Code Online (Sandbox Code Playgroud)

然后将.pb.cc文件添加到SOURCES.