使用hunspell库编译源代码时的链接器问题

时间:2013-10-31 10:58:15

标签: c linker linker-errors hunspell

我正在尝试编译在Ubuntu 10.10上使用hunspell库和gcc(版本4.6.3)的纯C源代码:

#include <stdlib.h>
#include <stdio.h>
#include <hunspell/hunspell.h>

int main() {
    Hunhandle *spellObj = Hunspell_create("/home/artem/en_US.aff", "/home/artem/en_US.dic");

    char str[60];
    scanf("%s", str);
    int result = Hunspell_spell(spellObj, str);

    if(result == 0)
        printf("Spelling error!\n");
    else
        printf("Correct Spelling!");

    Hunspell_destroy(spellObj);
    return 0;
}

使用命令:

gcc -lhunspell-1.3 example.c

但我有一些链接器问题:

/tmp/cce0QZnA.o: In function `main':
example.c:(.text+0x22): undefined reference to `Hunspell_create'
example.c:(.text+0x52): undefined reference to `Hunspell_spell'
example.c:(.text+0x85): undefined reference to `Hunspell_destroy'
collect2: ld returned 1 exit status

另外,我检查了/usr/include/hunspell/文件夹,文件hunspell.h存在并包含我的所有功能。
我做错了什么,为什么我不能编译这个来源?

1 个答案:

答案 0 :(得分:3)

尝试:

$ gcc example.c -lhunspell-1.3
<{1}}选项

See the documentation

  

在您编写此选项的命令中,它会有所不同;链接器按照指定的顺序搜索和处理库和目标文件。因此,-l在文件'foo.o'之后但在'bar.o'之前搜索库'z'。如果'bar.o'引用'z'中的函数,则可能无法加载这些函数。

所以,你要求GCC 首先搜索库,然后编译你的代码。您需要以相反的方式执行此操作,通常指定要在命令行上与last连接的库。

同时验证库文件的磁盘名称,通常有符号链接从名称中删除版本号,所以也许你的命令应该是:

'foo.o -lz bar.o'

链接到您系统上可用的“当前”库版本。

相关问题