2009-11-21 74 views
0

喜正試圖在boost庫編譯一個簡單的程序,但我一直得到了鏈接錯誤升壓鏈接錯誤-regular表情 - C++

#include <iostream> 
#include <string> 
#include <boost\regex.hpp> // Boost.Regex lib 

using namespace std; 

int main() { 

    std::string s, sre; 
    boost::regex re; 

    while(true) 
    { 
     cout << "Expression: "; 
     cin >> sre; 
     if (sre == "quit") 
     { 
     break; 
     } 
     cout << "String:  "; 
     cin >> s; 

     try 
     { 
     // Set up the regular expression for case-insensitivity 
     re.assign(sre, boost::regex_constants::icase); 
     } 
     catch (boost::regex_error& e) 
     { 
     cout << sre << " is not a valid regular expression: \"" 
       << e.what() << "\"" << endl; 
     continue; 
     } 
     if (boost::regex_match(s, re)) 
     { 
     cout << re << " matches " << s << endl; 
     } 
    } 
} 

,但我一直得到了鏈接錯誤

[Linker error] undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)' 

哪有我解決這個問題?
PS:我使用DEVCPP IDE我從安裝升壓www.devpaks.org

回答

5

如果你從官方的源代碼分發Boost庫,請確保您使用的bjam(很多建立二進制文件boost庫只是頭文件,並不要求你這樣做;正則表達式庫需要被構建)。看起來這些庫是隨devpack發佈的,所以這對你來說應該不成問題。

確保您的鏈接器路徑中有boost lib目錄。與海灣合作委員會,你可以使用-L編譯標誌:

gcc [whatever you normally put here] -L/path/to/boost/lib/directory 

或Visual C++,你可以在「鏈接>常規」屬性頁上添加lib目錄下的「附加庫目錄」爲您的項目。使用Visual C++鏈接器,您不需要明確告訴鏈接器要將哪些特定的Boost庫拉入; Boost頭文件包含指令以自動引入正確的指令。對於g ++,您需要指定這些庫,因此您需要查看正則表達式庫的名稱並添加適當的編譯器指令以鏈接它(-llibname(這是一個小寫的L))。

+0

使用Visual Studio,您有自動鏈接支持,但gcc不支持它。 – 2009-11-21 06:23:13

+0

@gf:感謝您的單挑;我不知道。 – 2009-11-21 06:28:28