2014-02-08 515 views
1

我裝:編譯並運行libconfig ++

sudo apt-get install libconfig++-dev 

之後,我想編譯和運行示例程序libconfig ++。

計劃:

/* ---------------------------------------------------------------------------- 
    libconfig - A library for processing structured configuration files 
    Copyright (C) 2005-2010 Mark A Lindner 

    This file is part of libconfig. 

    This library is free software; you can redistribute it and/or 
    modify it under the terms of the GNU Lesser General Public License 
    as published by the Free Software Foundation; either version 2.1 of 
    the License, or (at your option) any later version. 

    This library is distributed in the hope that it will be useful, but 
    WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
    Lesser General Public License for more details. 

    You should have received a copy of the GNU Library General Public 
    License along with this library; if not, see 
    <http://www.gnu.org/licenses/>. 
    ---------------------------------------------------------------------------- 
*/ 

#include <iostream> 
#include <iomanip> 
#include <cstdlib> 
#include <libconfig.h++> 

using namespace std; 
using namespace libconfig; 

// This example reads the configuration file 'example.cfg' and displays 
// some of its contents. 

int main(int argc, char **argv) 
{ 
    Config cfg; 

    // Read the file. If there is an error, report it and exit. 
    try 
    { 
    cfg.readFile("example.cfg"); 
    } 
    catch(const FileIOException &fioex) 
    { 
    std::cerr << "I/O error while reading file." << std::endl; 
    return(EXIT_FAILURE); 
    } 
    catch(const ParseException &pex) 
    { 
    std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine() 
       << " - " << pex.getError() << std::endl; 
    return(EXIT_FAILURE); 
    } 

    // Get the store name. 
    try 
    { 
    string name = cfg.lookup("name"); 
    cout << "Store name: " << name << endl << endl; 
    } 
    catch(const SettingNotFoundException &nfex) 
    { 
    cerr << "No 'name' setting in configuration file." << endl; 
    } 

    const Setting& root = cfg.getRoot(); 

    // Output a list of all books in the inventory. 
    try 
    { 
    const Setting &books = root["inventory"]["books"]; 
    int count = books.getLength(); 

    cout << setw(30) << left << "TITLE" << " " 
     << setw(30) << left << "AUTHOR" << " " 
     << setw(6) << left << "PRICE" << " " 
     << "QTY" 
     << endl; 

    for(int i = 0; i < count; ++i) 
    { 
     const Setting &book = books[i]; 

     // Only output the record if all of the expected fields are present. 
     string title, author; 
     double price; 
     int qty; 

     if(!(book.lookupValue("title", title) 
      && book.lookupValue("author", author) 
      && book.lookupValue("price", price) 
      && book.lookupValue("qty", qty))) 
     continue; 

     cout << setw(30) << left << title << " " 
      << setw(30) << left << author << " " 
      << '$' << setw(6) << right << price << " " 
      << qty 
      << endl; 
    } 
    cout << endl; 
    } 
    catch(const SettingNotFoundException &nfex) 
    { 
    // Ignore. 
    } 

    // Output a list of all books in the inventory. 
    try 
    { 
    const Setting &movies = root["inventory"]["movies"]; 
    int count = movies.getLength(); 

    cout << setw(30) << left << "TITLE" << " " 
     << setw(10) << left << "MEDIA" << " " 
     << setw(6) << left << "PRICE" << " " 
     << "QTY" 
     << endl; 

    for(int i = 0; i < count; ++i) 
    { 
     const Setting &movie = movies[i]; 

     // Only output the record if all of the expected fields are present. 
     string title, media; 
     double price; 
     int qty; 

     if(!(movie.lookupValue("title", title) 
      && movie.lookupValue("media", media) 
      && movie.lookupValue("price", price) 
      && movie.lookupValue("qty", qty))) 
     continue; 

     cout << setw(30) << left << title << " " 
      << setw(10) << left << media << " " 
      << '$' << setw(6) << right << price << " " 
      << qty 
      << endl; 
    } 
    cout << endl; 
    } 
    catch(const SettingNotFoundException &nfex) 
    { 
    // Ignore. 
    } 

    return(EXIT_SUCCESS); 
} 

// eof 

示例配置文件:

// An example configuration file that stores information about a store. 

// Basic store information: 
name = "Books, Movies & More"; 

// Store inventory: 
inventory = 
{ 
    books = ({ title = "Treasure Island"; 
       author = "Robert Louis Stevenson"; 
       price = 29.99; 
       qty = 5; }, 
      { title = "Snow Crash"; 
       author = "Neal Stephenson"; 
       price = 9.99; 
       qty = 8; } 
     ); 

    movies = ({ title = "Brazil"; 
       media = "DVD"; 
       price = 19.99; 
       qty = 11; }, 
      { title = "The City of Lost Children"; 
       media = "DVD"; 
       price = 18.99; 
       qty = 5; }, 
      { title = "Memento"; 
       media = "Blu-Ray"; 
       price = 24.99; 
       qty = 20; 
      }, 
      { title = "Howard the Duck"; } 
      ); 
}; 

// Store hours: 
hours = 
{ 
    mon = { open = 9; close = 18; }; 
    tue = { open = 9; close = 18; }; 
    wed = { open = 9; close = 18; }; 
    thu = { open = 9; close = 18; }; 
    fri = { open = 9; close = 20; }; 
    sat = { open = 9; close = 20; }; 
    sun = { open = 11; close = 16; }; 
}; 

我真的試圖例如爲:

g++ -lconfig++ example1.cpp -o example1 

輸出:

/tmp/ccOI0efx.o: In function `main': 
example1.cpp:(.text+0x21): undefined reference to `libconfig::Config::Config()' (..) 

如何編譯這個程序?什麼是必要的命令?

另一種方式: 我也下載包: libconfig-1.4.9.tar.gz 提取物,做「安裝」文件,我的意思是:

編譯這個包的最簡單方法是:

  1. cd' to the directory containing the package's source code and type 的./configure '......

  2. 輸入`使' 來編譯軟件包。

  3. 可選,類型`做檢查 '......

  4. 輸入`make install' 來安裝程序和任何數據文件和 文檔。

  5. 您可以刪除該程序的二進制文件,並通過鍵入`使清潔」對象從 源代碼目錄中的文件... 之後嘗試運行C++例子 (CD例子/ C++,並)無關?

輸出:

make: *** No rule to make target `../../lib/libconfig++.la', required by `example1'. Stop. 
+0

當您嘗試這兩個選項,會發生什麼? –

+0

輸入: 克++ ++ -lconfig -o example1.cpp例1 輸出: example1.cpp :(文本+ 0×21):未定義參考'libconfig ::配置::配置()」 (...) – andrew

+1

唐除非你有一個非常具體的理由避免使用軟件包管理器附帶的版本,否則不要安裝源代碼軟件。 「我收到編譯錯誤,我不確定接下來要做什麼」並不是這樣的原因。 –

回答

1

運行以下命令:

g++ example1.cpp -lconfig++ -Wall -o example1 && ./example1