我是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
我应该开始寻找来解决这个问题?
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有用的答案后,我就是这样做的:
我在configure.ac
中添加了这两行:
PKG_CHECK_MODULES([GOBJECT],[gobject-2.0])
AC_SUBST(GOBJECT_CFLAGS)
我更改了AM_CFLAGS
中的Makefile.am
行:
AM_CFLAGS = $(GLIB_CFLAGS) $(GOBJECT_CFLAGS) -I$(top_srcdir)/src
我将此行添加到Makefile.am
:
src_libfoobar_la_LIBADD = $(GLIB_LIBS) $(GOBJECT_LIBS)
它就像一个魅力。
答案 0 :(得分:2)
您应该首先仔细查看错误:
collect2:错误:ld返回1退出状态
这告诉你它是一个链接器错误(ld
是链接器)。
src / .libs / libfoobar.so:对`g_intern_static_string'
的未定义引用
链接器无法在您链接到测试程序的src/.libs/libfoobar.so
(共享库)中找到符号。因此,src/.libs/libfoobar.so
可能缺少至少一个依赖库,但可能缺少两个依赖库(前提libglib
和libgobject
)。它们需要与libfoobar.so
链接。
PKG_CHECK_MODULES
宏的现代版本假定到AC_SUBST
foo _LIBS
和 foo _CFLAGS
个变量,(例如GLIB_LIBS
和GLIB_CFLAGS
)。这需要添加到Makefile中以链接libglib
:
<强> Makefile.am 强>
src_libfoobar_la_LDADD = $(GLIB_LIBS)
这仍然无法链接您的测试代码,但您应该减少错误。您需要在PKG_CHECK_MODULES
中libgobject
configure.ac
GOBJECT_CFLAGS
,并在GOBJECT_LIBS
的正确位置插入相应的Makefile.am
和{{1}} }。