【发布时间】:2015-10-01 02:48:55
【问题描述】:
我使用 ar 制作了一个静态库来制作一个简单的字数计数器,但是当我到达我的 makefile 中的链接阶段时,我收到以下错误:
g++ -o wordcount obj/word.o obj/main.o -Wall -L lib -llinkedlist
obj/word.o: In function `cleanUp(linkedList*)':
word.c:(.text+0x83): undefined reference to `ll_clear(linkedList*)'
obj/word.o: In function `initialize(linkedList*)':
word.c:(.text+0xa7): undefined reference to `ll_init(linkedList*)'
obj/word.o: In function `getTotalWordCount(linkedList*)':
word.c:(.text+0xbd): undefined reference to `ll_getIterator(linkedList*)'
word.c:(.text+0xd7): undefined reference to `ll_next(linkedListIterator*)'
word.c:(.text+0xea): undefined reference to `ll_hasNext(linkedListIterator*)'
obj/word.o: In function `getWord(linkedList*, unsigned int)':
word.c:(.text+0x118): undefined reference to `ll_get(linkedList*, unsigned int)'
obj/word.o: In function `findWord(linkedList*, char*)':
word.c:(.text+0x12e): undefined reference to `ll_getIterator(linkedList*)'
word.c:(.text+0x148): undefined reference to `ll_next(linkedListIterator*)'
word.c:(.text+0x178): undefined reference to `ll_hasNext(linkedListIterator*)'
obj/word.o: In function `combineCounts(linkedList*, wordData*)':
word.c:(.text+0x26e): undefined reference to `ll_add(linkedList*, void const*, unsigned int)'
obj/main.o: In function `main':
main.c:(.text+0x1df): undefined reference to `ll_getIterator(linkedList*)'
main.c:(.text+0x1f2): undefined reference to `ll_next(linkedListIterator*)'
main.c:(.text+0x25c): undefined reference to `ll_hasNext(linkedListIterator*)'
collect2: error: ld returned 1 exit status
makefile:38: recipe for target 'wordcount' failed
make: *** [wordcount] Error 1
但是,使用 nm 查看我的库的符号表会产生以下结果(为了便于阅读而截断):
linkedlist.o:
00000028 T ll_add
00000124 T ll_addIndex
000003b7 T ll_clear
00000363 T ll_get
00000438 T ll_getIterator
00000477 T ll_hasNext
00000000 T ll_init
00000493 T ll_next
00000285 T ll_remove
00000420 T ll_size
我为类似问题找到的所有答案都提到您指定库的顺序很重要,但我已经在所有其他目标文件之后添加了我的库。有没有人知道我可能会出错的地方?
【问题讨论】:
-
linked_list.o 是 C 文件还是 C++ 文件?
-
所以你肯定有 lib 与 obj 处于同一级别,并且链表被放置在其中作为 .a 或 .so 文件正确吗?
-
linkedlist真的是 library 而不是 object 文件吗? -
如果你确实在用 C 编程,不要使用 g++ 来编译或链接你的代码。如果您从 C++ 代码调用 C 库,该 C 库的头文件是否具有必要的
extern "C"包装器? -
怀疑是gcc -o wordcount obj/word.o obj/main.o
/linkedlist.o
标签: c gcc static-libraries static-linking