我正在通过Zed Shaw's Learn C the Hard Way书籍,我正在尝试使用the bstring library,为库中的一些更常见的函数编写测试,以便更好地学习它们。我最初在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
,并在 end 添加-llcthw
cc
命令(谢谢,gcc,关心选项标志的位置......)。测试文件现在编译,但测试本身失败并出现此错误:
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*) $
WTF。
答案 0 :(得分:2)
静态链接库
尝试在build/liblcthw.a
的命令行选项中 tests/bstr_tests.c
之后添加对gcc
库的引用,如下所示:
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
。