为什么我的C编译器很难找到一些头文件?

时间:2022-09-25 14:12:25

I'm working my way through Zed Shaw's Learn C the Hard Way book and I'm currently trying to work with the bstring library, writing tests for some of the more common functions from the library to learn them better. I was working on this on OS X at first, but now need to work on an Ubuntu box and the compiler is giving me grief.

我正在用我的方法通过Zed Shaw的学习——Hard way book,我现在正尝试着与bstring图书馆合作,为一些更常见的函数编写测试,以便更好地学习它们。我刚开始在OS X上工作,但是现在需要在Ubuntu框上工作,编译器给我带来了痛苦。

Here's the Makefile: http://pastebin.com/bxCcH0w5

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

Here's what I have so far for the unit tests (I've omitted the function that actually runs the tests; mu_assert is a macro in minunit.h):

这里是我到目前为止的单元测试的内容(我省略了实际运行测试的函数;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;
}

This code compiles cleanly in OS X, but here's the error I get on Ubuntu:

这段代码在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*) $

EDIT

Based on a comment that has since been deleted, I modified the Makefile to include -lBuild in CFLAGS and to add -llcthw at the end of the cc command (Thanks, gcc, for caring where an option flag is...). The tests files now compile, but the tests themselves fail with this error:

基于已被删除的注释,我修改了Makefile以在CFLAGS中包含-lBuild,并在cc命令的末尾添加-llcthw(谢谢,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.

WTF。

1 个解决方案

#1


2  

To statically link the library

静态链接库。

Try putting the reference to the library build/liblcthw.a after tests/bstr_tests.c in gcc's command line options, like so:

试着把参考资料放在图书馆的建筑/liblcthw上。测试/ bstr_tests之后。c在gcc的命令行选项中,如下所示:

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

To dynamically link the library

动态链接库。

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

Also add the path to the shared object liblcthw.so to the variable LD_LIBRARY_PATH of the environment in which you try to run the final executable.

还可以为共享对象liblcthw添加路径。因此,对于您试图运行最终可执行文件的环境的变量LD_LIBRARY_PATH。

#1


2  

To statically link the library

静态链接库。

Try putting the reference to the library build/liblcthw.a after tests/bstr_tests.c in gcc's command line options, like so:

试着把参考资料放在图书馆的建筑/liblcthw上。测试/ bstr_tests之后。c在gcc的命令行选项中,如下所示:

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

To dynamically link the library

动态链接库。

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

Also add the path to the shared object liblcthw.so to the variable LD_LIBRARY_PATH of the environment in which you try to run the final executable.

还可以为共享对象liblcthw添加路径。因此,对于您试图运行最终可执行文件的环境的变量LD_LIBRARY_PATH。