簡體   English   中英

提升鏈接器錯誤-正則表達式-C ++

[英]boost linker errors -regular expressions - c++

您好,我正在嘗試在Boost庫中編譯一個簡單的程序,但我一直收到鏈接器錯誤

#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)' 

我該如何解決?
附言 :我正在使用devcpp ide ,我從www.devpaks.org安裝了boost

如果您從官方源發行版中獲得Boost庫,請確保使用bjam構建二進制文件(許多boost庫僅是標頭,不需要這樣做;需要構建正則表達式庫)。 看起來這些庫是隨devpack版本一起分發的,所以這對您來說不是問題。

確保鏈接器路徑中有boost lib目錄。 使用gcc,可以使用-L編譯器標志:

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

或對於Visual C ++,只需將lib目錄添加到項目的“鏈接器>常規”屬性頁上的“其他庫目錄”。

使用Visual C ++鏈接器,您無需顯式告訴鏈接器要插入哪個特定的Boost庫。 Boost標頭包含指令,以自動插入正確的指令。 對於g ++,您需要指定庫,因此需要查找正則表達式庫的名稱,並添加適當的編譯器指令以-llibname鏈接( -llibname (小寫的L))。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM