質問

I'm trying to compile this pure C source code which used hunspell library with gcc (version 4.6.3) on Ubuntu 10.10:

#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;
}

With command:

gcc -lhunspell-1.3 example.c

But I've got some linker issues:

/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

Also, I checked /usr/include/hunspell/ folder, file hunspell.h exists and contains all functions from my source.
What I'm doing wrong, and why I can't compile this source?

役に立ちましたか?

解決

Try:

$ gcc example.c -lhunspell-1.3

See the documentation for the -l option:

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, 'foo.o -lz bar.o' searches library 'z' after file 'foo.o' but before 'bar.o'. If 'bar.o' refers to functions in 'z', those functions may not be loaded.

So, you asked GCC to first search the library, then compile your code. You need to do it the other way around, you generally specify the libraries to link against last on the command line.

Also verify the on-disk name of the library file, often there are symlinks that remove the version number from the name, so perhaps your command should just be:

$ gcc example.c -lhunspell

to link against the "current" library version available on your system.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top