Automake配置无法编译我的单元测试

时间:2014-06-13 04:27:53

标签: autotools glib automake gobject

我是C和Automake开发的新手。所以这可能是一个愚蠢的问题。

我最近正在开发一个链接到GLib和GObject的C库。我通过编译得到了Makefile.am和configure.ac。但是,当我尝试编译单元测试时,会发生一系列错误。错误与此类似:

src/.libs/libfoobar.so: undefined reference to `g_intern_static_string'
src/.libs/libfoobar.so: undefined reference to `g_once_init_enter'
src/.libs/libfoobar.so: undefined reference to `g_free'
src/.libs/libfoobar.so: undefined reference to `g_once_init_leave'
src/.libs/libfoobar.so: undefined reference to `g_type_name'
src/.libs/libfoobar.so: undefined reference to `g_value_set_string'
src/.libs/libfoobar.so: undefined reference to `g_type_class_adjust_private_offset'
src/.libs/libfoobar.so: undefined reference to `g_return_if_fail_warning'
src/.libs/libfoobar.so: undefined reference to `g_object_new'
src/.libs/libfoobar.so: undefined reference to `g_type_class_peek_parent'
src/.libs/libfoobar.so: undefined reference to `g_param_spec_string'
src/.libs/libfoobar.so: undefined reference to `g_type_check_class_cast'
src/.libs/libfoobar.so: undefined reference to `g_type_class_add_private'
src/.libs/libfoobar.so: undefined reference to `g_type_register_static_simple'
src/.libs/libfoobar.so: undefined reference to `g_log'
src/.libs/libfoobar.so: undefined reference to `g_value_dup_string'
src/.libs/libfoobar.so: undefined reference to `g_type_instance_get_private'
src/.libs/libfoobar.so: undefined reference to `g_object_class_install_properties'
collect2: error: ld returned 1 exit status

我应该开始寻找来解决这个问题?

我项目的更多信息:

目录结构


  • /
    • 的src /
      • foobar的-的hello.c
      • foobar的-hello.h
    • 测试/
      • 测试foobar的-的hello.c
    • Makefile.am
    • configure.ac
    • autogen.sh
    • README.md

Autotools相关文件

Makefile.am

ACLOCAL_AMFLAGS  = -I m4 ${ACLOCAL_FLAGS}
AUTOMAKE_OPTIONS = subdir-objects

# -- Common build flags --------------
AM_CFLAGS = $(GLIB_CFLAGS) -I$(top_srcdir)/src


# -- Library -------------------------
lib_LTLIBRARIES = src/libfoobar.la

src_libfoobar_la_SOURCES = \
        src/foobar-hello.c \
        $(NULL)

src_libfoobar_la_include_HEADERS = \
        src/foobar-hello.h \
        $(NULL)

src_libfoobar_la_includedir = $(includedir)/foobar


# -- Tests ---------------------------
check_PROGRAMS = \
        tests/test_foobar_hello \
        $(NULL)

tests_test_foobar_hello_SOURCES = tests/test-foobar-hello.c
tests_test_foobar_hello_LDADD = src/libfoobar.la

TESTS = $(check_PROGRAMS)


# -- Common --------------------------
CLEANFILES = \
        $(NULL)

EXTRA_DIST = \
        autogen.sh \
        README.md \
        $(NULL)

configure.ac

AC_PREREQ(2.63)
AC_INIT([libfoobar], [0.1], [https://github.com/foobar/libfoobar/issues], [libfoobar], [https://github.com/foobar/libfoobar])

AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([m4])

AM_INIT_AUTOMAKE([1.11 no-dist-gzip dist-xz tar-ustar foreign])
AC_PROG_MKDIR_P

m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])])

# Check library versions
# then substitute the revelant cflag with actual library information
PKG_CHECK_MODULES([GLIB],[glib-2.0 > 2.19.0])
AC_SUBST(GLIB_CFLAGS)

# Initialize libtool
LT_INIT([disable-static])

# OUTPUT files
AC_CONFIG_FILES([
  Makefile
])


AC_OUTPUT

编辑:

解决方案

在阅读ldavis有用的答案后,我就是这样做的:

  1. 我在configure.ac中添加了这两行:

    PKG_CHECK_MODULES([GOBJECT],[gobject-2.0])
    AC_SUBST(GOBJECT_CFLAGS)
    
  2. 我更改了AM_CFLAGS中的Makefile.am行:

    AM_CFLAGS = $(GLIB_CFLAGS) $(GOBJECT_CFLAGS) -I$(top_srcdir)/src
    
  3. 我将此行添加到Makefile.am

    src_libfoobar_la_LIBADD = $(GLIB_LIBS) $(GOBJECT_LIBS)
    
  4. 它就像一个魅力。

1 个答案:

答案 0 :(得分:2)

您应该首先仔细查看错误:

  

collect2:错误:ld返回1退出状态

这告诉你它是一个链接器错误(ld是链接器)。

  

src / .libs / libfoobar.so:对`g_intern_static_string'

的未定义引用

链接器无法在您链接到测试程序的src/.libs/libfoobar.so(共享库)中找到符号。因此,src/.libs/libfoobar.so可能缺少至少一个依赖库,但可能缺少两个依赖库(前提libgliblibgobject)。它们需要与libfoobar.so链接。

PKG_CHECK_MODULES宏的现代版本假定AC_SUBST foo _LIBS foo _CFLAGS个变量,(例如GLIB_LIBSGLIB_CFLAGS)。这需要添加到Makefile中以链接libglib

<强> Makefile.am

src_libfoobar_la_LDADD = $(GLIB_LIBS)

这仍然无法链接您的测试代码,但您应该减少错误。您需要在PKG_CHECK_MODULESlibgobject configure.ac GOBJECT_CFLAGS,并在GOBJECT_LIBS的正确位置插入相应的Makefile.am和{{1}} }。