gpt4 book ai didi

C 编译错误 - 艰难地学习 C(Ex 32)

转载 作者:太空狗 更新时间:2023-10-29 15:19:44 43 4
gpt4 key购买 nike

我很难获得 Exercise 32 中的代码以艰难的方式学习 C 语言以进行编译。

我已经逐字复制了创建者的 GitHub repo 中的代码甚至克隆到一个新的存储库中。我查看了其他存储库,在我的 MacOSX 和 Ubuntu 等上进行了尝试。我似乎所做的一切都无法编译。

我正在使用 Ubuntu 12.04(见下文)。

这是我的文件结构(注意——这是我只包含直接来自练习 32 的文件时的文件结构。显然,当我克隆 git 存储库时,我得到了更多的文件。我使用 diff 来确保我所有的文件,包括我的 make 文件,在我的简化版本中与 git 存储库完全相似):

$ pwd
/usr/local/me/code/C/liblcthw


$ ls
bin LICENSE Makefile README.md src tests

$ ls tests/
list_tests.c minunit.h runtests.sh

$ ls src/lcthw/
dbg.h list.c list.h

这是我的 make 命令。

$ make
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG -fPIC -c -o src/lcthw/list.o src/lcthw/list.c
ar rcs build/liblcthw.a src/lcthw/list.o
ranlib build/liblcthw.a
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG build/liblcthw.a tests/list_tests.c -o tests/list_tests
tests/list_tests.c: In function ‘main’:
tests/list_tests.c:111:1: warning: parameter ‘argc’ set but not used [-Wunused-but-set-parameter]
/tmp/ccXlNfMl.o: In function `test_create':
/usr/local/me/code/C/liblcthw/tests/list_tests.c:13: undefined reference to `List_create'
/tmp/ccXlNfMl.o: In function `test_destroy':
/usr/local/me/code/C/liblcthw/tests/list_tests.c:22: undefined reference to `List_clear_destroy'
/tmp/ccXlNfMl.o: In function `test_push_pop':
/usr/local/me/code/C/liblcthw/tests/list_tests.c:31: undefined reference to `List_push'
/usr/local/me/code/C/liblcthw/tests/list_tests.c:34: undefined reference to `List_push'
/usr/local/me/code/C/liblcthw/tests/list_tests.c:37: undefined reference to `List_push'
/usr/local/me/code/C/liblcthw/tests/list_tests.c:41: undefined reference to `List_pop'
/usr/local/me/code/C/liblcthw/tests/list_tests.c:44: undefined reference to `List_pop'
/usr/local/me/code/C/liblcthw/tests/list_tests.c:47: undefined reference to `List_pop'
/tmp/ccXlNfMl.o: In function `test_shift':
/usr/local/me/code/C/liblcthw/tests/list_tests.c:56: undefined reference to `List_shift'
/usr/local/me/code/C/liblcthw/tests/list_tests.c:59: undefined reference to `List_shift'
/usr/local/me/code/C/liblcthw/tests/list_tests.c:62: undefined reference to `List_shift'
/tmp/ccXlNfMl.o: In function `test_remove':
/usr/local/me/code/C/liblcthw/tests/list_tests.c:74: undefined reference to `List_remove'
/tmp/ccXlNfMl.o: In function `test_unshift':
/usr/local/me/code/C/liblcthw/tests/list_tests.c:86: undefined reference to `List_unshift'
/usr/local/me/code/C/liblcthw/tests/list_tests.c:89: undefined reference to `List_unshift'
collect2: ld returned 1 exit status
make: *** [tests/list_tests] Error 1

我正在使用 Ubuntu 12.04

$ lsb_release -a
LSB Version: core-2.0-amd64:core-2.0-noarch:core-3.0-amd64:core-3.0-noarch:core-3.1-amd64:core-3.1-noarch:core-3.2-amd64:core-3.2-noarch:core-4.0-amd64:core-4.0-noarch:cxx-3.0-amd64:cxx-3.0-noarch:cxx-3.1-amd64:cxx-3.1-noarch:cxx-3.2-amd64:cxx-3.2-noarch:cxx-4.0-amd64:cxx-4.0-noarch:desktop-3.1-amd64:desktop-3.1-noarch:desktop-3.2-amd64:desktop-3.2-noarch:desktop-4.0-amd64:desktop-4.0-noarch:graphics-2.0-amd64:graphics-2.0-noarch:graphics-3.0-amd64:graphics-3.0-noarch:graphics-3.1-amd64:graphics-3.1-noarch:graphics-3.2-amd64:graphics-3.2-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-3.2-amd64:printing-3.2-noarch:printing-4.0-amd64:printing-4.0-noarch:qt4-3.1-amd64:qt4-3.1-noarch
Distributor ID: Ubuntu
Description: Ubuntu 12.04.2 LTS
Release: 12.04
Codename: precise

最佳答案

从 github 存储库下载的练习 32 代码是特定于 BSD 系统(可能还有 OS X)的平台。具体来说,BSD 系统上有一些成功构建所需的符号,但很可能在 (Linux) Ubuntu 12.04 系统上不存在。这些包括:

mergesort
heapsort

尽管如此,库和单元测试都可以成功编译,但(最终)需要上述符号的一个单元测试除外。从 tests 目录中移动单个有问题的测试将允许其他测试在 (Linux) SuSE SLES 11 系统上编译,如果对下面显示的 makefile 中的缺陷进行轻微更改。

下载问题中提到的源后:

.../liblcthw-master> ll
total 28
drwxr-xr-x 2 mahonri users 4096 May 24 21:30 bin
drwxr-xr-x 2 mahonri users 4096 May 24 21:53 build
-rw-r--r-- 1 mahonri users 1548 May 8 2012 LICENSE
-rw-r--r-- 1 mahonri users 1139 May 8 2012 Makefile
-rw-r--r-- 1 mahonri users 1069 May 8 2012 README.md
drwxr-xr-x 3 mahonri users 4096 May 24 21:52 src
drwxr-xr-x 2 mahonri users 4096 May 24 21:53 tests

可以在 Makefile 中找到缺陷。这是下载的 Makefile 的有缺陷的部分:

# The Unit Tests
.PHONY: tests
tests: CFLAGS += $(TARGET)
tests: $(TESTS)
sh ./tests/runtests.sh

一个问题是将 $(TARGET) build/liblcthw.a 的值放在 CFLAGS 的末尾不起作用。这是因为 CFLAGS 针对的是编译器,而不是链接器。因此,生成的“测试”目标未与库链接。

另一个问题是至少有一个“测试”应用程序需要数学库。

要解决这些问题,必须将 Makefile 修改为类似于以下内容:

# The Unit Tests
.PHONY: tests
tests: LDLIBS += -lm -L./build -llcthw
tests: $(TESTS)
sh ./tests/runtests.sh

要构建 liblcthw.a 并构建“测试”应用程序,请针对此目录中更新的 Makefile 运行 make:

.../liblcthw-master> ll
total 28
drwxr-xr-x 2 mahonri users 4096 May 24 21:30 bin
drwxr-xr-x 2 mahonri users 4096 May 24 21:53 build
-rw-r--r-- 1 mahonri users 1548 May 8 2012 LICENSE
-rw-r--r-- 1 mahonri users 1139 May 8 2012 Makefile
-rw-r--r-- 1 mahonri users 1069 May 8 2012 README.md
drwxr-xr-x 3 mahonri users 4096 May 24 21:52 src
drwxr-xr-x 2 mahonri users 4096 May 24 21:53 tests
.../liblcthw-master> make
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG -fPIC -c -o src/lcthw/bstree.o src/lcthw/bstree.c
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
src/lcthw/darray_algos.c: In function ‘DArray_heapsort’:
src/lcthw/darray_algos.c:12: warning: implicit declaration of function ‘heapsort’
src/lcthw/darray_algos.c: In function ‘DArray_mergesort’:
src/lcthw/darray_algos.c:17: warning: implicit declaration of function ‘mergesort’
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/hashmap_algos.o src/lcthw/hashmap_algos.c
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG -fPIC -c -o src/lcthw/hashmap.o src/lcthw/hashmap.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
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG -fPIC -c -o src/lcthw/ringbuffer.o src/lcthw/ringbuffer.c
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG -fPIC -c -o src/lcthw/sarray.o src/lcthw/sarray.c
src/lcthw/sarray.c: In function ‘SuffixArray_create’:
src/lcthw/sarray.c:71: warning: implicit declaration of function ‘qsort_r’
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG -fPIC -c -o src/lcthw/stats.o src/lcthw/stats.c
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG -fPIC -c -o src/lcthw/tstree.o src/lcthw/tstree.c
ar rcs build/liblcthw.a src/lcthw/bstree.o src/lcthw/bstrlib.o src/lcthw/darray_algos.o src/lcthw/darray.o src/lcthw/hashmap_algos.o src/lcthw/hashmap.o src/lcthw/list_algos.o src/lcthw/list.o src/lcthw/radixmap.o src/lcthw/ringbuffer.o src/lcthw/sarray.o src/lcthw/stats.o src/lcthw/tstree.o
ranlib build/liblcthw.a
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG tests/bstree_tests.c -L./build -llcthw -o tests/bstree_tests
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG tests/bstr_tests.c -L./build -llcthw -o tests/bstr_tests
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG tests/darray_algos_tests.c -L./build -llcthw -o tests/darray_algos_tests
...

然后,makefile 继续运行测试可执行文件:

sh ./tests/runtests.sh
Running unit tests:
----
RUNNING: ./tests/bstree_tests
ALL TESTS PASSED
Tests run: 5
tests/bstree_tests PASS
tests/bstr_tests PASS
----
RUNNING: ./tests/darray_tests
ALL TESTS PASSED
Tests run: 8
tests/darray_tests PASS
----
RUNNING: ./tests/hashmap_algos_tests
ALL TESTS PASSED
Tests run: 4
tests/hashmap_algos_tests PASS
----
RUNNING: ./tests/hashmap_tests
ALL TESTS PASSED
Tests run: 5
tests/hashmap_tests PASS
----
RUNNING: ./tests/list_algos_tests
ALL TESTS PASSED
Tests run: 2
tests/list_algos_tests PASS
----
RUNNING: ./tests/list_tests
ALL TESTS PASSED
Tests run: 6
tests/list_tests PASS
----
RUNNING: ./tests/queue_tests
ALL TESTS PASSED
Tests run: 3
tests/queue_tests PASS
----
RUNNING: ./tests/radixmap_tests
ALL TESTS PASSED
Tests run: 1
tests/radixmap_tests PASS
----
RUNNING: ./tests/ringbuffer_tests
ALL TESTS PASSED
Tests run: 3
tests/ringbuffer_tests PASS
----
RUNNING: ./tests/sarray_tests
./tests/runtests.sh: line 3: 1033 Segmentation fault $VALGRIND ./$i 2>> tests/tests.log
ERROR in test tests/sarray_tests: here's tests/tests.log
------
DEBUG tests/ringbuffer_tests.c:60: ----- RUNNING: ./tests/ringbuffer_tests
DEBUG tests/ringbuffer_tests.c:53:
----- test_create
DEBUG tests/ringbuffer_tests.c:54:
----- test_read_write
DEBUG tests/ringbuffer_tests.c:55:
----- test_destroy
DEBUG tests/sarray_tests.c:46: ----- RUNNING: ./tests/sarray_tests
DEBUG tests/sarray_tests.c:39:
----- test_create
make: *** [tests] Error 1

注意:同样的缺陷似乎没有影响 OS X 8 构建(这让我自己感到困惑)。必须在 OS X 8 版本的构建中修复一个单独的缺陷。具体来说,list_algos.c 中的以下行:

...

inline List *List_merge(List *left, List *right, List_compare cmp)
{
List *result = List_create();
...

内联函数需要一个函数原型(prototype)。更改为以下内容:

...

extern List *List_merge(List *left, List *right, List_compare cmp);
inline List *List_merge(List *left, List *right, List_compare cmp)
{
List *result = List_create();
...

在这个单一的修改之后,OS X 版本能够make 成功。

关于C 编译错误 - 艰难地学习 C(Ex 32),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23851098/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com