【问题标题】:gcc static linking undefined referencesgcc静态链接未定义的引用
【发布时间】: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


【解决方案1】:

问题是该库是由 C 编译器编译的,但我编译了新的源代码,这些源代码使用 C++ 编译器引用了该库。这两种方法的目标文件输出不同。以如下代码为例:

// main.c
#include <stdio.h>

void foo(int x) {
  printf("%d\n");
}

int main(int argc, char** argv) {
  foo(argc);
  return 0;
}

将此文件作为 C 程序编译为目标文件将产生与编译为 C++ 程序不同的符号。示例:

# Compile as C program
$ gcc -c main.c; nm main.o
00000000 T foo
00000018 T main
         U printf

# Compile as C++ program
$ g++ -c main.c; nm main.o
00000018 T main
         U printf
00000000 T _Z3fooi

如您所见,foo 函数使用两个不同的符号编译。由于新的源文件被编译为 C++,链接器需要 C++ 风格的符号,而库包含 C 风格的符号。链接器无法匹配符号,因​​此由于 C++ 符号未定义而引发错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多