【问题标题】:libconfig: syntax error on @include directivelibconfig:@include 指令的语法错误
【发布时间】:2012-11-13 02:59:03
【问题描述】:

我正在尝试编写适用于 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";

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

【问题讨论】:

    标签: c include libconfig


    【解决方案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 马克·林德纳 * lib/scanner.l - 将@include 与前面的空格匹配的错误修复

    .

    【讨论】:

    • 你如何编译 c++ 示例?我收到如下错误:“未定义对 `libconfig::Config::Config()' 的引用”
    【解决方案2】:
    @include "quote.cfg"
    

    应该是

    #include "quote.cfg"
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-27
      • 2015-03-16
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      相关资源
      最近更新 更多