【问题标题】:error: expected declaration specifiers or '…' before string constant错误:字符串常量之前的预期声明说明符或“...”
【发布时间】:2016-04-13 18:30:44
【问题描述】:

有人知道这段代码有什么问题吗?我无法在可比较的问题中找到问题。

代码是用 C 编写的,我不断收到此错误。我确实将 -D SET_MIN_TEMP=5 -D Set_MAX_TEMP=30 添加到 gcc 编译行以确保 ifndefs 应该是错误的......

#ifndef CONFIG_H
#define CONFIG_H


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>

#ifndef RUN_AVG_LENGTH
    #define RUN_AVG_LENGTH 5
#endif

#ifndef SET_MIN_TEMP
    printf("please set SET_MIN_TEMP \n");
#endif

#ifndef SET_MAX_TEMP
    printf("please set SET_MAX_TEMP \n");
#endif

typedef uint16_t sensor_id_t;
typedef uint16_t room_id_t;
typedef double sensor_value_t;
typedef time_t sensor_ts_t;     // UTC timestamp as returned by time() - notice that the size of time_t is different on 32/64 bit machine

typedef struct {
  sensor_id_t id;
  sensor_value_t value;
  sensor_ts_t ts;
} sensor_data_t;

typedef struct {
    sensor_id_t sensor_id;
    room_id_t room_id;
    double running_avg[5];
    sensor_ts_t timestamp;
} sensor_node_t;


#endif // CONFIG_H

【问题讨论】:

  • Set_MAX_TEMP != SET_MAX_TEMP 因为 C 区分大小写。
  • 哪一行出现错误?为什么像这样在顶层有printf() 行?
  • 如果您想在未定义宏时导致编译时错误,请使用#error
  • 该死的我怎么会错过那个-.-,谢谢兄弟
  • 好吧,我之前使用过#warning,但是因为我一直收到错误而改变了它,现在我也知道为什么了......

标签: c


【解决方案1】:

您不能在函数外使用函数调用 (printf)。如果你想在编译时报告错误,你应该看看#error...

here

【讨论】:

  • #ifndef 应该隐藏这些调用并防止错误。
  • 是的,但似乎 OP 传递了错误的参数(大小写),所以错误的 printf 语句将保留...
  • 我认为这就是重点——他真的很想知道为什么#ifndef 不起作用。结果证明是一个简单的错字。
  • 我也这么认为,不过printf是错的,应该换掉...
  • 我同意这一点,这就是我 15 分钟前在评论中写它的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 2023-01-19
  • 2011-09-04
  • 2014-06-08
相关资源
最近更新 更多