【问题标题】:How can I create a string constant in gcc 4.9.2?如何在 gcc 4.9.2 中创建字符串常量?
【发布时间】:2015-02-14 01:17:56
【问题描述】:

我在带有 GCC 4.9.2 的 Arch Linux 上运行,我在编译以下代码时遇到了问题:

#ifndef WORLD_H         
#define WORLD_H         
#include <string.h>
#include <stdio.h>      
//#include "removeBuffering.h"
//World dimensions      
#define WORLD_WIDTH 80          
#define WORLD_HEIGHT 20 
//World block types
#define FLAT_LAND '-'
//Instructions
#define MOVE_UP 'w'
#define MOVE_DOWN 's'
#define MOVE_RIGHT 'd'
#define MOVE_LEFT 'a'
#ifndef WIN32
#define COMMAND "clear" //Clears a linux console screen
#else
#define COMMAND "cls" //Clears a windows console screen
#endif
#define wipe() system( COMMAND )

它适用于我的 koding.com 虚拟机,它使用 GCC 4.8.2,但在我的本地机器上,它会生成以下错误:

include/world.h:17:17: error: expected declaration specifiers or ‘...’ before string constant
 #define COMMAND "clear" //Clears a linux console screen

我认为这是由于 GCC 4.9 中的某种变化,但我似乎找不到任何关于它的好信息,因此非常感谢任何帮助

【问题讨论】:

  • 在第一个#define之前显示代码。 #if#ifdef 块中有什么?
  • 错误信息是否有更多行?也许一个显示它是从哪里包含的?
  • 坦率地说,#define wipe() system(COMMAND) 是一种糟糕的做法。您所做的只是将一个众所周知的功能隐藏在一个新名称后面。它只是混淆了未来程序员的代码,特别是因为你没有明确wipe 是使用大写的宏。
  • @CareyGregory 你说的完全正确,所以我删除了违规行。
  • @immibis 原来问题出在 world.c 文件中,其中包含了 world.h 文件。我一直在删除一些while循环并留在一些'}'中。这使得底部的一些函数调用成为孤立的,这似乎导致了这个奇怪的错误。

标签: c gcc c-preprocessor


【解决方案1】:

通过gcc -E 运行它——这将扩展结果,此时一切都应该清楚了。

【讨论】:

  • 我设法在不使用此选项的情况下解决了这个问题,但从我迄今为止的测试来看,它确实看起来很有趣。谢谢你给我看:)
  • 添加您自己的答案,这样当人们再次查看时它就会出现。
  • 感谢您的建议。如果您愿意,可以阅读我的回答。
【解决方案2】:

在我给出自己的答案之前,我想先概述一下我的代码在生成上述错误消息时的外观。这是world.h:

#ifndef WORLD_H
#define WORLD_H
#include <string.h>
#include <stdio.h>
//#include "removeBuffering.h"
//World dimensions
#define WORLD_WIDTH 80
#define WORLD_HEIGHT 20
//World block types
#define FLAT_LAND '-'
//Instructions
#define MOVE_UP 'w'
#define MOVE_DOWN 's'
#define MOVE_RIGHT 'd'
#define MOVE_LEFT 'a'
#ifndef WIN32
#define COMMAND "clear" //Clears a linux console screen
#else
#define COMMAND "cls" //Clears a windows console screen
#endif
int cursorXPos;
int cursorYPos;
char world[WORLD_HEIGHT][WORLD_WIDTH+1]; //Space for null terminator
void initializeWorld();
void printWorld();
void getInput();
//void printHelp();

#endif

这里是world.c(我已经清空了函数以节省空间)

#include "world.h"
void initializeWorld()
{

}
void printWorld()
{

}
void getInput()
{

}
system(COMMAND);
printWorld();

这里是 GCC 提供的完整错误列表:

In file included from src/world.c:1:0:
include/world.h:17:17: error: expected declaration specifiers or ‘...’ before string constant
 #define COMMAND "clear" //Clears a linux console screen
                 ^
src/world.c:78:10: note: in expansion of macro ‘COMMAND’
   system(COMMAND);
          ^
src/world.c:79:3: warning: data definition has no type or storage class
   printWorld();
   ^
src/world.c:79:3: error: conflicting types for ‘printWorld’
src/world.c:13:6: note: previous definition of ‘printWorld’ was here
 void printWorld()

根据我的经验,处理列表中的第一个错误总是一个好主意,所以除了第一个错误之外,我没有过多关注任何事情,这就是我首先提出这个问题的原因.我最终尝试按照 Carey Gregory 和 immibis 的建议解决后来的错误。

重要的是:

src/world.c:79:3: warning: data definition has no type or storage class
   printWorld();
   ^
src/world.c:79:3: error: conflicting types for ‘printWorld’
src/world.c:13:6: note: previous definition of ‘printWorld’ was here
 void printWorld()

只要我移动了 printWorld()(和 system())的错误函数调用,错误就消失了。

【讨论】:

  • 您应该点击绿色勾号接受此答案,将问题标记为已解决。在标题中添加“[已解决]”这个词不是正确的方法。
猜你喜欢
  • 2020-12-22
  • 1970-01-01
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 2011-05-03
  • 1970-01-01
相关资源
最近更新 更多