1

我正在阅读Zed Shaw 的 Learn C the Hard Way一书,我目前正在尝试使用bstring 库,为库中的一些更常见的函数编写测试以更好地学习它们。起初我在 OS X 上工作,但现在需要在 Ubuntu 机器上工作,编译器让我很伤心。

这是 Makefile: http: //pastebin.com/bxCcH0w5

这是我到目前为止的单元测试(我省略了实际运行测试的函数;mu_assert 是 minunit.h 中的宏):

#include "minunit.h"
#include <lcthw/bstrlib.h>

char *test_bfromcstr()
{
    char *test = "asdf";
    bstring res = NULL;
    res = bfromcstr(test);

    mu_assert(res != NULL, "bstring should not be NULL.");
    mu_assert(blength(res) == (int) strlen(test), "bstring length should match our test data");
    mu_assert(strcmp((char *)bdata(res),test) == 0, "bstring data should be same.");

    bdestroy(res);

    return NULL;
}

char *test_blk2bstr()
{
    char *test = "qwerty";
    int L = 6;
    bstring res = NULL;
    res = blk2bstr(test, L);

    mu_assert(res != NULL, "bstring should not be NULL.");
    mu_assert(blength(res) == L, "bstring length should match our test data");
    mu_assert(strcmp((char *)bdata(res),test) == 0, "bstring data should be same.");

    bdestroy(res);

    return NULL;
}

此代码在 OS X 中编译干净,但这是我在 Ubuntu 上遇到的错误:

liblcthw(master*) $ make
ar rcs build/liblcthw.a src/lcthw/bstrlib.o src/lcthw/darray_algos.o src/lcthw/darray.o src/lcthw/list_algos.o src/lcthw/list.o src/lcthw/radixmap.o
ranlib build/liblcthw.a
cc -shared -o build/liblcthw.so src/lcthw/bstrlib.o src/lcthw/darray_algos.o src/lcthw/darray.o src/lcthw/list_algos.o src/lcthw/list.o src/lcthw/radixmap.o
cc -g -O2 -Wall -Wextra -Isrc/ -rdynamic -DNDEBUG  build/liblcthw.a    tests/bstr_tests.c   -o tests/bstr_tests
tests/bstr_tests.c: In function ‘test_bfromcstr’:
tests/bstr_tests.c:12:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:12:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:12:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:12:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:12:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:12:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c: In function ‘test_blk2bstr’:
tests/bstr_tests.c:28:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:28:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:28:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:28:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:28:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:28:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c: In function ‘main’:
tests/bstr_tests.c:136:1: warning: parameter ‘argc’ set but not used [-Wunused-but-set-parameter]
/tmp/ccZEG4Lr.o: In function `test_bfromcstr':
/home/blochs/cproject/liblcthw/tests/bstr_tests.c:8: undefined reference to `bfromcstr'
/home/blochs/cproject/liblcthw/tests/bstr_tests.c:14: undefined reference to `bdestroy'
/tmp/ccZEG4Lr.o: In function `test_blk2bstr':
/home/blochs/cproject/liblcthw/tests/bstr_tests.c:24: undefined reference to `blk2bstr'
/home/blochs/cproject/liblcthw/tests/bstr_tests.c:30: undefined reference to `bdestroy'
collect2: ld returned 1 exit status
make: *** [tests/bstr_tests] Error 1
liblcthw(master*) $

编辑

根据已被删除的评论,我修改了 Makefile 以包含在-lBuild命令末尾CFLAGS并添加(感谢 gcc,关心选项标志的位置......)测试文件现在可以编译,但测试本身因以下错误而失败:-llcthwcc

liblcthw(master*) $ make
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG  -fPIC   -c -o src/lcthw/bstrlib.o src/lcthw/bstrlib.c
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG  -fPIC   -c -o src/lcthw/darray_algos.o src/lcthw/darray_algos.c
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG  -fPIC   -c -o src/lcthw/darray.o src/lcthw/darray.c
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG  -fPIC   -c -o src/lcthw/list_algos.o src/lcthw/list_algos.c
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG  -fPIC   -c -o src/lcthw/list.o src/lcthw/list.c
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG  -fPIC   -c -o src/lcthw/radixmap.o src/lcthw/radixmap.c
ar rcs build/liblcthw.a src/lcthw/bstrlib.o src/lcthw/darray_algos.o src/lcthw/darray.o src/lcthw/list_algos.o src/lcthw/list.o src/lcthw/radixmap.o
ranlib build/liblcthw.a
cc -shared -o build/liblcthw.so src/lcthw/bstrlib.o src/lcthw/darray_algos.o src/lcthw/darray.o src/lcthw/list_algos.o src/lcthw/list.o src/lcthw/radixmap.o
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG  -Lbuild tests/bstr_tests.c -o tests/bstr_tests -llcthw -lbsd
tests/bstr_tests.c: In function ‘test_bfromcstr’:
tests/bstr_tests.c:12:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:12:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:12:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:12:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:12:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:12:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c: In function ‘test_blk2bstr’:
tests/bstr_tests.c:28:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:28:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:28:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:28:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:28:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c:28:2: warning: null argument where non-null required (argument 1) [-Wnonnull]
tests/bstr_tests.c: In function ‘main’:
tests/bstr_tests.c:136:1: warning: parameter ‘argc’ set but not used [-Wunused-but-set-parameter]
sh ./tests/runtests.sh
Running unit tests:
ERROR in test tests/bstr_tests: here's tests/tests.log
------
./tests/bstr_tests: error while loading shared libraries: liblcthw.so: cannot open shared object file: No such file or directory
make: *** [tests] Error 1
liblcthw(master*) $

哇。

4

1 回答 1

2

静态链接库

尝试将库的引用放在build/liblcthw.a in tests/bstr_tests.cgcc命令行选项之后,如下所示:

cc -g -O2 -Wall -Wextra -Isrc/ -rdynamic -DNDEBUG -o tests/bstr_tests tests/bstr_tests.c build/liblcthw.a

动态链接库

cc -g -O2 -Wall -Wextra -Isrc/ -rdynamic -DNDEBUG -o tests/bstr_tests tests/bstr_tests.c -Lbuild -llcthw

还将共享对象的路径添加到您尝试运行最终可执行文件的环境liblcthw.so变量中。LD_LIBRARY_PATH

于 2012-09-15T15:06:59.970 回答