静态库的链接器错误

时间:2013-01-21 17:20:59

标签: c++ g++ static-libraries linker-errors header-files

我已经成功编译了我的拼写检查程序和libspellcheck库。现在我的问题是允许其他开发人员使用我的libspellcheck。我创建了一个简单的测试程序:

#include <spellcheck.h>
#include <iostream>

using namespace std;

int main(void)
{

    bool spell_result = check_spelling("english.dict", "abed");

    if(spell_result == true)
    {
        cout << "your dictionary / libspellcheck works!" << endl;
    }
    else
    {
        cout << "problem with your dictionary / libspellcheck" << endl;
    }

    return 0;
}

如果一切正常,程序将输出:

你的词典/ libspellcheck有效

但是,这个程序甚至不会编译。我用过:

g ++ -lspellcheck -o test test.cpp

它不起作用。我相信这是头文件的一个问题,因为编译器给了我这个:

test.cpp: In function ‘int main()’:
test.cpp:9:59: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
test.cpp:9:59: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
/tmp/ccIz3ivT.o: In function `main':
test.cpp:(.text+0x19): undefined reference to `check_spelling(char*, char*)'
collect2: ld returned 1 exit status

唯一的问题是,spellcheck.h位于/ usr / include,这是我认为应该的地方。我的问题是,如何修复此错误,这是我的头文件的问题,或者我的libspellcheck是一个问题。如果你需要查看其他代码,我很乐意提供它,因为spellcheck和libspellcheck是根据GPL许可的。

1 个答案:

答案 0 :(得分:2)

假设标题中的check_spelling声明是正确的,请尝试以下操作:

g++ -o test test.cpp -lspellcheck

-l对象之后取决于命令行中的库)。从标题开始,确实找到并使用了 ,否则您将从编译器而不是链接器中获得错误。