【问题标题】:libcheck fails with /usr/bin/ld: cannot find @CHECK_CFLAGS@: No such file or directorylibcheck 因 /usr/bin/ld 失败:找不到 @CHECK_CFLAGS@:没有这样的文件或目录
【发布时间】:2022-06-17 06:14:29
【问题描述】:

我正在尝试按照检查教程here 尝试设置测试套件。我的项目结构是这样的

root
  |-----src
         |----- foo.h, foo.c
        test
         |----- root_test.h, root_test.c, foo_test.c
        Makefile.am
        configure.ac

我的 foo.h:

#ifndef FOO_H
#define FOO_H

int func();

#endif

foo.c:

#include "foo.h"

int func() { return 0; }

root_test.h:

#ifndef ROOT_TEST_H
#define ROOT_TEST_H

#include <check.h>

Suite *foo_suite();

#endif

root_test.c:

#include <check.h>
#include <stdlib.h>

#include "root_test.h"

int main() {
  int fc;
  SRunner *sr;

  sr = srunner_create(foo_suite());

  srunner_run_all(sr, CK_NORMAL);
  fc = srunner_ntests_failed(sr);
  srunner_free(sr);

  return fc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

foo_test.c:

#include <check.h>

#include "../src/foo.h"
#include "root_test.h"

START_TEST(foo_func) { ck_assert_int_eq(func(), 0); }
END_TEST

Suite *foo_suite() {
  Suite *s;
  TCase *tc;

  s = suite_create("Foo");
  tc = tcase_create("Core");

  tcase_add_test(tc, foo_func);

  suite_add_tcase(s, tc);
  return s;
}

我的 Makefile.am:

ACLOCAL_AMFLAGS = -I m4

lib_LTLIBRARIES = libroot.la

HFILES =\
  src/foo.h       

CFILES =\
  src/foo.c

libroot_la_SOURCES = $(HFILES) $(CFILES)

TESTS = libroot_test
check_PROGRAMS = libroot_test
libroot_test_SOURCES = $(HFILES) test/root_test.h test/root_test.c test/foo_test.c
libroot_test_CFLAGS = @CHECK_CFLAGS@
libroot_test_LDADD = libroot.la @CHECK_LIBS@

noinst_PROGRAMS = libroot_test

还有我的 configure.ac:

AC_PREREQ([2.71])
AC_INIT([libroot], [0.0.1], [swdon@users.noreply.github.com])
AC_CONFIG_AUX_DIR([build])
AC_CONFIG_MACRO_DIRS([m4])
AC_CONFIG_SRCDIR([src/foo.h])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects])
AM_PROG_AR

LT_INIT

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_SIZE_T

# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_REALLOC

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

我的autoreconf -i./configure 运行没有任何问题。当我运行 make check 时,我得到以下错误输出:

/bin/sh ./libtool  --tag=CC   --mode=link gcc @CHECK_CFLAGS@ -g -O2   -o libroot_test  test/libroot_test-libroot_test.o test/libroot_test-foo_test.o libroot.la @CHECK_LIBS@ 
libtool: link: gcc @CHECK_CFLAGS@ -g -O2 -o .libs/libroot_test test/libroot_test-libroot_test.o test/libroot_test-foo_test.o @CHECK_LIBS@  ./.libs/libroot.so -Wl,-rpath -Wl,/usr/local/lib
/usr/bin/ld: cannot find @CHECK_CFLAGS@: No such file or directory
/usr/bin/ld: cannot find @CHECK_LIBS@: No such file or directory
collect2: error: ld returned 1 exit status
make: *** [Makefile:687: libroot_test] Error 1

【问题讨论】:

    标签: c autotools


    【解决方案1】:

    Makefile.am 中出现的 @CHECK_CFLAGS@@CHECK_LIBS@ 字符串是 Autoconf 输出变量值的占位符,但您原来的 configure.ac 不包含任何会设置此类输出变量的内容。结果,占位符被原封不动地传递到生成的 makefile 中,它们会导致构建失败并出现观察到的错误。

    检查文档包含a section about integrating libcheck with Autotools-based build systems。它的建议是使用 pkg-config 提供的PKG_CHECK_MODULES。例如,

    # ...
    
    # Checks for libraries.
    PKG_CHECK_MODULES([CHECK], [check >= 0.9.6])
    
    # ...
    

    第二个宏参数指定 Check 的最低可接受版本,如果您不想指定最低版本,可以省略它。

    还请注意,旧版本的PKG_CHECK_MODULES 不会自动将相关变量 Autoconf 输出变量。如果您对这样的版本感到厌烦并且无法更新,那么您还想添加

    AC_SUBST([CHECK_CFLAGS])
    AC_SUBST([CHECK_LIBS])
    

    PKG_CHECK_MODULES之后。

    如果您没有 pkg​​-config 或不希望您的包构建依赖它,那么 Check 本身还提供了一个 AM_PATH_CHECK() 宏可以达到目的。但是,该宏已被弃用,因此可能会从某些未来版本的 Check 中删除。有关此宏的更多信息,请参阅文档。

    【讨论】:

    • 和往常一样,当您使用 Autoconf (AC_) 和 Automake (AM_) 附带的集合之外的宏时,在某处使用 m4_pattern_forbid([^PKG_CHECK_MODULES]) 很有用在PKG_CHECK_MODULES 之前:当autoreconf 时间没有安装pkg.m4 时,这将正确捕捉到这种情况。
    【解决方案2】:

    好的,所以我将AM_PATH_CHECK 添加到我的configure.ac 中,它现在可以工作了。我更新的 configure.ac:

    AC_PREREQ([2.71])
    AC_INIT([libccg],[0.0.1],[smadurange@users.noreply.github.com])
    AC_CONFIG_AUX_DIR([build])
    AC_CONFIG_MACRO_DIRS([m4])
    AC_CONFIG_SRCDIR([src/foo.h])
    AC_CONFIG_HEADERS([config.h])
    AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects])
    AM_PROG_AR
    AM_PATH_CHECK
    
    LT_INIT
    
    # Checks for programs.
    AC_PROG_CC
    
    # Checks for libraries.
    
    # Checks for header files.
    
    # Checks for typedefs, structures, and compiler characteristics.
    AC_TYPE_SIZE_T
    
    # Checks for library functions.
    AC_FUNC_MALLOC
    AC_FUNC_REALLOC
    
    AC_CONFIG_FILES([Makefile])
    AC_OUTPUT
    

    【讨论】:

    • The documentation 建议为此使用PKG_CHECK_MODULES,并指出AM_PATH_CHECK 宏已被弃用。另请参阅 the Autotools Mythbuster commentary about PKG_CHECK_MODULES 了解更多详细信息,尤其是在您使用该宏的旧版本时。
    • @JohnBollinger 确实是您的正确答案。感谢您指出。您想将其发布为答案以便我接受吗?
    • 好的,完成了,@kovac。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 2023-04-04
    • 2016-03-28
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多