libconfig:@include指令的语法错误

时间:2012-11-25 12:53:16

标签: c include libconfig

我正在尝试编写可与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.CFG

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;
};

quote.cfg

# file: quote.cfg
quote = "dsf";

编译后我尝试在params中运行带有server.cfg的main(),但我总是在@include的行上得到“语法错误”。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我使用libconfig-1.4.9尝试了您的配置,它可以正常运行:

./cfg server.cfg 
loading config: server.cfg...
port: 9034

您似乎使用旧版本,在解析@include时存在错误,通过此change log判断,您应该升级到libconfig-1.4.3或更新版本:

  

-----版本1.4.3 ------

     

2010-02-13 Mark Lindner   * lib / scanner.l - 用于将@include与前面的空格匹配的错误修正

答案 1 :(得分:0)

@include "quote.cfg"

应该是

#include "quote.cfg"

@include不是C语法的一部分。