【问题标题】:How Can I read a file at preprocessing time如何在预处理时读取文件
【发布时间】:2018-04-25 20:21:19
【问题描述】:

我需要根据我正在处理的项目的版本号编译我的代码。 简而言之,我将从文件中读取版本号并启用或禁用我的代码的某些部分/。
如何在 c/c++ 编译时从文件中读取版本号?

我正在尝试做这样的事情

static const unsigned int version =   
#include "version"
    ;

我收到编译器错误。

【问题讨论】:

  • 让您的构建系统提供宏...
  • 我认为这应该可以工作,尽管每个人都会有一个config.h#define VERSION 42.11,或者让构建系统传递一个-DVERSION=42.11
  • 而文件version的内容是...?
  • 您应该将构建日志添加到您的问题中,而不是评论中。
  • @kashifahmad edit 您的问题请添加更多信息。代码在 cmets 中几乎不可读。

标签: c++ c file cmake file-handling


【解决方案1】:

文件.c

#include <stdio.h>
static const float version = 
#include "file.h"
;

int main()
{
 printf("Version is : %f\n", version);
}

文件.h

37.11

基本有效

【讨论】:

  • 我可以做如下的事情吗? static const unsigned int version = #include "version" / 1000000) % 100 ; #if (version == 14) #define GDK14_SUPPORTED 1 #elif(version == 13) #define GDK14_SUPPORTED 0 #endif
  • 你为什么不自己试试呢?
  • @EladHazan 你只是复制粘贴的 OP 的代码并说它有效。
猜你喜欢
  • 1970-01-01
  • 2017-03-24
  • 2011-03-27
  • 1970-01-01
  • 1970-01-01
  • 2017-12-11
  • 1970-01-01
  • 2019-12-03
  • 1970-01-01
相关资源
最近更新 更多