How can I create a string constant in gcc 4.9.2?
我在带有 GCC 4.9.2 的 Arch Linux 上运行,我在编译以下代码时遇到了问题:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#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 VM,它使用 GCC 4.8.2,但在我的本地机器上,它会生成以下错误:
1
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循环并留在一些’}’中。这使得底部的一些函数调用成为孤立的,这似乎导致了这个奇怪的错误。
- @CareyGregory我很抱歉,但事实证明有问题的代码不在我在这里发布的代码中。
- 没关系。您刚刚演示了为什么代码审查有效。
在我给出自己的答案之前,我想先概述一下我的代码在生成上述错误消息时的外观。这是world.h:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#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(我已经清空了函数以节省空间)
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include”world.h”
void initializeWorld() { } } } |
这是 GCC 提供的完整错误列表:
1
2 3 4 5 6 7 8 9 10 11 12 13 |
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 的建议解决后来的错误。
重要的是:
1
2 3 4 5 6 |
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())的错误函数调用,错误就消失了。
- 您应该单击绿色勾号以接受此答案,以将问题标记为已解决。在标题中添加单词”[已解决]”不是正确的方法。
通过 gcc -E 运行 – 这将扩展结果,此时一切都应该变得清晰。
- 我设法在不使用此选项的情况下解决了这个问题,但从我到目前为止的测试来看,它确实看起来很有趣。谢谢你给我看:)
- 添加您自己的答案,这样当人们再次查看时它就在那里。
- 谢谢你的建议。如果您愿意,可以阅读我的回答。
来源:https://www.codenong.com/28511357/