Reputation: 320
I'm running on Arch Linux with GCC 4.9.2, and I've been having trouble compiling the following code:
#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 )
It works on my koding.com VM, which uses GCC 4.8.2 but on my local machine, it generates the following error:
include/world.h:17:17: error: expected declaration specifiers or ‘...’ before string constant
#define COMMAND "clear" //Clears a linux console screen
I'm thinking it's due to some sort of change in GCC 4.9, but I can't seem to find any good information about it, so any help would be greatly appreciated
Upvotes: 0
Views: 281
Reputation: 320
Before I give my own answer, I want to give you an overview of how my code looked while it was generating the error message above. Here's 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
Here's world.c (I've emptied the functions to save space)
#include "world.h"
void initializeWorld()
{
}
void printWorld()
{
}
void getInput()
{
}
system(COMMAND);
printWorld();
And here is the FULL error list provided by 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()
In my experience, it was always a good idea to deal with the very first error on the list, so I didn't pay much attention to anything besides the first error, which is why I asked the question in the first place. I eventually tried to resolve the later errors as Carey Gregory and immibis suggested.
The important ones were:
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()
As soon as I moved the misplaced function call for printWorld() (and system()), the errors disappeared.
Upvotes: 1
Reputation: 112356
Run this through gcc -E
-- this will expand the results, at which point all should become clear.
Upvotes: 1