我正在尝试编写可与 libconfig 一起使用的应用程序。所以我有下一部分代码:
#include <stdio.h>
#include <stdlib.h>
#include <libconfig.h>
int main(int argc, char **argv)
{
if(argc > 1)
{
char *config_file_path = argv[1];
config_t cfg;
config_init(&cfg);
printf("loading config: %s...\n", config_file_path);
if (!config_read_file(&cfg, config_file_path))
{
fprintf(stderr, "%s:%d - %s\n", config_file_path,
config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
exit(EXIT_FAILURE);
}
long int port_number;
config_lookup_int(&cfg, "server_app.port", &port_number);
printf("port: %ld\n", port_number);
config_destroy(&cfg);
}
else
{
fprintf(stderr, "Wrong number of arguments. Usage: %s <cfg_file>\n", argv[0]);
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
并为其配置文件:
服务器配置文件
server_app:
{
port = 9034;
mail_queue_dir = "mail_queue";
@include "quote.cfg"
logs:
{
error_log = "logs/error.log";
};
connection_address = "127.0.0.1";
max_worker_number = 10;
number_of_pending_connections = 10;
};
报价单.cfg
# file: quote.cfg
quote = "dsf";
编译后,我尝试在参数中使用 server.cfg 运行 main(),但我总是在 @include 的行上收到“语法错误”。有任何想法吗?