2012-03-14 79 views
2

我的目標是創建一個簡單的Win32控制檯應用程序,它使用HunSpell拼寫檢查用戶輸入的單詞。 我試圖關注用於Visual Studio 2008和HunSpell 1.2.1的this codeproject tutorialC++ - 在Visual Studio 2010中使用HunSpell 1.3.2

我不想使用提供的代碼,因爲我打算寫自己的代碼。 此外,我想添加HunSpell作爲一個DLL,而不是一個靜態庫。

以下是我所採取的步驟:

  1. 創建了一個Win32控制檯(空)與名MyProject的項目。
  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和libhunspell的發佈版本release_dll。
  6. 重建libhunspell項目,分別在debug_dll和release_dll文件夾中生成libhunspell.dll
  7. 使我的控制檯項目取決於libhunspell。 (已添加對libhunspell的引用)
  8. 從SourceForge.org下載它們之後,複製了字典文件en_US.aff & en_US.dic到myproject \ myproject \ HunSpell-Dic

我無法弄清楚在codeproject教程中提到的處理器如何添加處理器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」,但重點關注後錯誤消失。

謝謝你的幫助。

回答

4

除非要實際使用codeproject作者的自定義控件,否則不需要預處理器定義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 > Properties > Configuration Properties > C++ > General > 'Additional Include Directories'應該是這樣的:.\HunSpell-Src\hunspell;%(AdditionalIncludeDirectories)

  • Project > Properties > Configuration Properties > Linker > General > 'Additional Library Directories'應該是這樣的:.\Debug_dll\libhunspell;%(AdditionalLibraryDirectories)

您還需要將myproject\myproject\Debug_dll\libhunspell\libhunspell.dll複製到您的項目輸出目錄(。\ Debug),否則您的exe將無法找到它。

+0

感謝您對HSPELLEDIT_DLL的澄清。儘管如此,用\\修正這個愚蠢的錯誤並沒有幫助。我相應地編輯/糾正了我的問題。添加Hunspell.hxx的路徑也沒有幫助。 – 2012-03-14 15:00:32

+0

@red_rain:我下載了hunspell並模仿你的dir結構,並能夠成功運行測試。我已經編輯了我的答案,記下我必須改變才能編譯/工作。 – 2012-03-14 17:30:37

+0

謝謝你的努力,它現在有效。我錯過了'Linker - > General'中的路徑,'C++ - > General'中的路徑指向'win_api'而不是'hunspell',我不得不將dll複製到'。\ Debug'。 – 2012-03-15 08:15:29