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

Protocol buffers 如何在autoconf/automake中使用协议缓冲区?,protocol-buffers,automake,Protocol Buffers,Automake,正在寻找autoconf和automake规则的一个很好的示例,用于构建使用协议缓冲区的项目,将protoc添加到构建过程中的最佳方法?这似乎可行: 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])]

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

这似乎可行:

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])
Makefile.am:

%.pb.cc %.pb.h: %.proto
    $(PROTOC) --proto_path=$(dir $^) --cpp_out=$(dir $^) $^
然后将.pb.cc文件添加到源中。

configure.ac
就protobuf库而言,它使用
pkg config
,因此最好使用
pkg\u CHECK\u 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.])])
或者,允许用户使用
--with protoc=/path/to/protoc
或使用环境变量
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\u noinst\u数据指定protobuf源文件
。这是必要的,以确保它们被捆绑到源发行版
.tar.gz
文件中,该文件由
make dist
生成

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

使用
节点列表
前缀和
$(builddir)
路径指定生成的文件:

nodist_myprog_SOURCES = $(builddir)/whatever.pb.cc $(builddir)/whatever.pb.h
并使用
make clean
清洁它们:

MOSTLYCLEANFILES = whatever.pb.cc whatever.pb.h
使用
builded\u SOURCES
处理生成头文件的依赖关系:

BUILT_SOURCES = whatever.pb.h
您的编译器标志可能需要引用生成目录以查找头文件(在VPATH生成中工作):


Makefile.am
规则是否适用于VPATH生成(例如
mkdir生成;cd生成;./configure;make
)?输出文件可能应该放在
$builddir
中,而不是
$srcdir
中。使用
$(dir$^)
Makefile.am
规则中,我得到一个错误
Makefile.am:11:dir$^:非POSIX变量名;Makefile.am:11:(可能是GNU make扩展)
。如果
autoconf>=0.24
,则不需要
AC_SUBST(PROTOBUF_*)
,如《非常好》中所述,谢谢
nodist\u myprog\u SOURCES
现在需要使用
@builddir@/whatever.pb.cc
AM_CPPFLAGS += -I$(builddir)