一:什么是Libconfig?
程序开发过程中往往很多参数需要放在配置文件中,因为这样可以避免更改代码重新编译的问题。通常我们可以通过自己读init文件来实现,不过已经有很多人做了这方面的东西,可以借用,Libconfig就是其中之一。
Libconfig是一个用于处理结构化配置文件的简单库。 此文件格式比XML更紧凑,更易读,更适合内存受限的设备。 与XML不同,它是类型感知的,因此不必在应用程序代码中进行字符串解析。该库支持C和C ++语言,适用于兼容POSIX的UNIX和类UNIX系统(GNU / Linux,Mac OS X,Solaris,FreeBSD),Android和Windows(2000,XP及更高版本)。
二:怎么使用?
1. 下载
http://www.hyperrealm.com/main.php?s=libconfig
https://gitee.com/helloerror/Development-kit/tree/master/Libconfig
2. 解压后执行
./configure
make
make install
3. 使用可以参考examples
4. 编译时加上 -lconfig++ 或者-lconfig 选项
三:会遇到的问题及注意事项
错误:
Error while loading shared library: libconfig++.so.9
解决方式
执行:sudo ldconfig -v
(往/lib和/usr/lib里面加东西,不用修改/etc/ld.so.conf,但要调一下ldconfig,否则可能这个library会找不到)
四、参考文献
http://www.hyperrealm.com/libconfig/libconfig_manual.html#The-C_002b_002b-API
http://stackoverflow.com/questions/6792451/error-while-loading-shared-library-libconfig-so-9
程序代码包中examples目录下的相关示例!!!
五、简单测试
1. 配置文件用例example.cfg,内容如下:
// 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; };
};
2. 源文件example1.c,内容如下:
#include <stdio.h>
#include <stdlib.h>
#include <libconfig.h>
/* This example reads the configuration file 'example.cfg' and displays
* some of its contents.
*/
int main(int argc, char **argv)
{
config_t cfg;
config_setting_t *setting;
const char *str;
config_init(&cfg);
/* Read the file. If there is an error, report it and exit. */
if(! config_read_file(&cfg, "example.cfg"))
{
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
return(EXIT_FAILURE);
}
/* Get the store name. 读取配置文件中的“Basic store information:”信息 */
if(config_lookup_string(&cfg, "name", &str))
printf("Store name: %s\n\n", str);
else
fprintf(stderr, "No 'name' setting in configuration file.\n");
if(config_lookup_string(&cfg, "hours", &str))
printf("Store hours: %s\n\n", str);
else
fprintf(stderr, "No 'hours' setting in configuration file.\n");
/* Output a list of all books in the inventory. */
setting = config_lookup(&cfg, "inventory.books");
if(setting != NULL)
{
int count = config_setting_length(setting);
int i;
printf("%-30s %-30s %-6s %s\n", "TITLE", "AUTHOR", "PRICE", "QTY");
for(i = 0; i < count; ++i)
{
config_setting_t *book = config_setting_get_elem(setting, i);
/* Only output the record if all of the expected fields are present. */
const char *title, *author;
double price;
int qty;
if(!(config_setting_lookup_string(book, "title", &title)
&& config_setting_lookup_string(book, "author", &author)
&& config_setting_lookup_float(book, "price", &price)
&& config_setting_lookup_int(book, "qty", &qty)))
continue;
printf("%-30s %-30s $%6.2f %3d\n", title, author, price, qty);
}
putchar('\n');
}
/* Output a list of all movies in the inventory. */
setting = config_lookup(&cfg, "inventory.movies");
if(setting != NULL)
{
unsigned int count = config_setting_length(setting);
unsigned int i;
printf("%-30s %-10s %-6s %s\n", "TITLE", "MEDIA", "PRICE", "QTY");
for(i = 0; i < count; ++i)
{
config_setting_t *movie = config_setting_get_elem(setting, i);
/* Only output the record if all of the expected fields are present. */
const char *title, *media;
double price;
int qty;
if(!(config_setting_lookup_string(movie, "title", &title)
&& config_setting_lookup_string(movie, "media", &media)
&& config_setting_lookup_float(movie, "price", &price)
&& config_setting_lookup_int(movie, "qty", &qty)))
continue;
printf("%-30s %-10s $%6.2f %3d\n", title, media, price, qty);
}
putchar('\n');
}
config_destroy(&cfg);
return(EXIT_SUCCESS);
}
/* eof */
3. 编译
[root@localhost c]# gcc -lconfig example1.c
[root@localhost c]#
4. 运行
[root@localhost c]# ./a.out
Store name: Books, Movies & More
No 'hours' setting in configuration file.
TITLE AUTHOR PRICE QTY
Treasure Island Robert Louis Stevenson $ 29.99 5
Snow Crash Neal Stephenson $ 9.99 8
TITLE MEDIA PRICE QTY
Brazil DVD $ 19.99 11
The City of Lost Children DVD $ 18.99 5
Memento Blu-Ray $ 24.99 20
[root@localhost c]#