【问题标题】:C++ - Using HunSpell 1.3.2 with Visual Studio 2010C++ - 在 Visual Studio 2010 中使用 HunSpell 1.3.2
【发布时间】:2012-03-31 00:06:00
【问题描述】:

我的目标是创建一个简单的 Win32 控制台应用程序,它使用 HunSpell 对用户输入的单词进行拼写检查。 我尝试关注 this codeproject tutorial,它适用于 Visual Studio 2008 和 HunSpell 1.2.1。

我不想使用提供的代码,因为我打算自己编写。 此外,我想将 HunSpell 添加为 dll,而不是静态库。

以下是我采取的步骤:

  1. 创建了一个名为 myproject 的 Win32 控制台(空)项目。
  2. 从 SourceForge.org 下载了 HunSpell 1.3.2。
  3. 已将 hunspell-1.3.2\src\hunspellwin_api 复制到 myproject\myproject\HunSpell-Src
  4. 添加和转换项目 libhunspell myproject\myproject\HunSpell-Src\win-api\libhunspell.vcproj 到解决方案。
  5. 在配置管理器中让我的调试版本使用 debug_dll 和我的发布版本 release_dll 的 libhunspell。
  6. 重建libhunspell项目,libhunspell.dll分别生成在debug_dll和release_dll文件夹中。
  7. 使我的控制台项目依赖于 libhunspell。 (添加了对 libhunspell 的引用)
  8. 从 SourceForge.org 下载字典文件 en_US.aff 和 en_US.dic 到 myproject\myproject\HunSpell-Dic

我不知道如何/在哪里添加代码项目教程中提到的处理器定义 HSPELLEDIT_DLL。

按照MSDN 上“在控制台应用程序中使用类库中的功能”下列出的步骤并没有改变结果。

我想用这样的程序来测试它:

#include <iostream>
#include "HunSpell-Src/win_api/hunspelldll.h"

using namespace std;

void main()
{
    void *spellObj = hunspell_initialize("HunSpell-Dic\\en_us.aff", "HunSpell-Dic\\en_us.dic");

    char str[60];

    cin >> str;

    int result = hunspell_spell(spellObj, str);

    if(result == 0)
        cout << "Spelling error!";
    else
        cout << "Correct Spelling!";

    hunspell_uninitialize(spellObject);
}

如果我尝试编译,VS 会产生以下错误消息:

myproject\myproject\hunspell-src\win_api\hunspelldll.h(34): fatal error C1083: Cannot open include file: 'hunspell.hxx': No such file or directory

Hunspell.hxx 存在于 myproject\myproject\HunSpell-Src\hunspell 中。 IntelliSense 将#include "hunspell.hxx" 标记为错误,而选项卡没有聚焦并显示消息“错误:无法打开源文件 hunspell.hxx”,但在聚焦它之后,错误消失了。

感谢您的帮助。

【问题讨论】:

    标签: c++ visual-studio-2010 dll spell-checking hunspell


    【解决方案1】:

    除非您要实际使用代码项目作者的自定义控件,否则不需要预处理器定义 HSPELLEDIT_DLL。如果您想定义它(或其他预处理器定义),请参阅/D (Preprocessor Definitions)

    您的路径字符串需要是双 \\ 而不是单 \ 转义,并且您有一些编译问题:

    #include <iostream>
    #include "HunSpell-Src/win_api/hunspelldll.h"
    
    using namespace std;
    
    void main()
    {
        Hunspell *spellObj = (Hunspell *)hunspell_initialize("HunSpell-Dic\\en_us.aff", "HunSpell-Dic\\en_us.dic");
    //  ^change * type        ^cast returned void* to type that will be used later
    
        char str[60];
    
        cin >> str;
    
        int result = hunspell_spell(spellObj, str);
    
        if(result == 0)
            cout << "Spelling error!";
        else
            cout << "Correct Spelling!";
    
        hunspell_uninitialize(spellObj /*SpellObject is undefined*/);
    //                        ^use correct variable
    }
    

    对于 Hunspell.hxx,您需要告诉您的项目如何找到它。为此,请打开您的项目设置和 Hunspell.hxx 的路径到“配置属性”>“C++”>“常规”下的“其他包含目录”。参考/I (Additional Include Directories)

    基于您的目录结构:

    • 您的Project &gt; Properties &gt; Configuration Properties &gt; C++ &gt; General &gt; 'Additional Include Directories' 应如下所示:.\HunSpell-Src\hunspell;%(AdditionalIncludeDirectories)

    • 您的Project &gt; Properties &gt; Configuration Properties &gt; Linker &gt; General &gt; 'Additional Library Directories' 应如下所示:.\Debug_dll\libhunspell;%(AdditionalLibraryDirectories)

    您还需要将myproject\myproject\Debug_dll\libhunspell\libhunspell.dll 复制到您的项目输出目录 (.\Debug),否则您的 exe 将无法找到它。

    【讨论】:

    • 感谢您对 HSPELLEDIT_DLL 的澄清。用 \\ 修复这个愚蠢的错误并没有帮助。我相应地编辑/更正了我的问题。添加 Hunspell.hxx 的路径也没有帮助。
    • @red_rain:我下载了 hunspell 并模仿了你的 dir 结构,并且能够让测试成功运行。我已经编辑了我的答案,以说明我必须进行哪些更改才能使其编译/工作。
    • 感谢您的努力,现在可以使用了。我错过了Linker -&gt; General 中的路径,C++ -&gt; General 中的路径指向win_api 而不是hunspell,我不得不将dll 复制到.\Debug
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多