我安装了:
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 提取并完成“安装”文件,我的意思是:
编译此包的最简单方法是:
cd' to the directory containing the package's source code and type
./配置” ...
输入`make'来编译包。
可选择输入“make check”...
输入`make install'来安装程序和任何数据文件 文档。
您可以从中删除程序二进制文件和目标文件 输入`make clean'的源代码目录... 之后尝试运行c ++示例 (cd examples / c ++和make)无事可做?
输出:
make: *** No rule to make target `../../lib/libconfig++.la', required by `example1'. Stop.
答案 0 :(得分:1)
运行此命令:
g++ example1.cpp -lconfig++ -Wall -o example1 && ./example1