2 Replies - 163 Views - Last Post: 07 November 2019 - 07:38 PM Rate Topic: -----

#1 Evilgaga   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-October 19

c - creating a makefile causes error “expected ‘;’, ‘,’ or ‘)’ before

Posted 07 November 2019 - 07:04 PM

So I'm creating a maze game program but in order for me to be able to actually start coding, I'm supposed to create a makefile that compiles and links the mazes.c and mazeDisplay.c files into an executable called mazes. The makefile should allow make all and make clean commands to work properly, and I'm also supposed to include something called the -lX11 library in order for the code to link properly.

I've been trying to compile my program and create my makefile for the past 2 days, and I can't get anything to work. My compiler doesn't seem to recognize what a pointer is. I keep getting the error "error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token Graph *computeGraph(char maze[HEIGHT][WIDTH]) {" and I'm starting to get really anxious as my assignment is due soon and I'm unable to even compile the starting program files which I need to start my task because of this makefile issue. Please, any help would be appreciated. I've been losing my mind trying to do a simple task and I just can't figure out what the problem is. Is there something wrong with my makefile instructions?

Makefile:
    GCC = gcc

    all: mazes.o mazeDisplay.o
	    $(GCC) -o mazes maze.o mazeDisplay.o -lX11

    mazes.o: mazes.c mazeDisplay.h
	    $(GCC) -c mazes.c 

    mazeDisplay.o: mazeDisplay.c mazeDisplay.h
	    $(GCC) -c mazeDisplay.c

    clean:
	    rm -f $(OBJ) mazes



CODE:

mazes.c:
	#include <stdio.h>
	#include <X11/Xlib.h>
	#include <unistd.h>

	#include "graphSet.h"
	#include "mazeDisplay.h"

	#define SCALE 25

	// These are display-related variables
	Display *display;
	Window   win;
	GC       gc;


	// Draw the Maze on the window.  
	void drawMaze(char grid[WIDTH][HEIGHT]) {
	  // First erase background
	  XSetForeground(display, gc, 0xFFFFFF);
	  XFillRectangle(display, win, gc, 0, 0, 750, 750);
	  XFlush(display);

	  // Draw the grid maze
	  for (int y=0; y<WIDTH; y++) {
		for (int x=0; x<HEIGHT; x++) {
		  if (grid[y][x] == '1') 
		XSetForeground(display, gc, 0x333333);
		  else
			XSetForeground(display, gc, 0xFFFFFF);
		  XFillRectangle(display, win, gc, x*SCALE, y*SCALE, SCALE, SCALE);
		}
	  }
	  XFlush(display);
	}


	// Draws an edge with the given color (e.g., 0x0000FF is blue)
	// from cell (c1, r1) to cell (c2, r2) of the maze
	void drawEdgeWithColor(int c1, int r1, int c2, int r2, int color) {
	  XSetForeground(display, gc, color);
	  XDrawLine(display, win, gc, c1*SCALE + SCALE/2, r1*SCALE + SCALE/2, c2*SCALE + SCALE/2, r2*SCALE + SCALE/2);
	  XFlush(display);
	}

	// Draws a node with the given color (e.g., 0x0000FF is blue)
	// centered at the given cell (c1, r1) of the maze.
	void drawNodeWithColor(int c1, int r1, int color) {
	  XSetForeground(display, gc, color);
	  XFillArc(display, win, gc,
		   c1*SCALE-SCALE/4 + SCALE/2,
		   r1*SCALE-SCALE/4 + SCALE/2,
		   11, 11, 0, 360*64);
	  XFlush(display);
	}


	// Draw a single graph starting at the given root node n.
	void drawGraph(Node *n) {
	  // Quit recursion if there are no Nodes
	  if (n == NULL)
		return;

	   // Recursively draw in each direction.  Draw the edges before the recursive call so that
	  // vertices are drawn on top of the edges
	  if (n->up != NULL) {
		drawEdgeWithColor(n->x, n->y, n->up->x, n->up->y, 0x0000FF);
		drawGraph(n->up);
	  }
	  if (n->down != NULL) {
		drawEdgeWithColor(n->x, n->y, n->down->x, n->down->y, 0x0000FF);
		drawGraph(n->down);
	  }
	  if (n->left != NULL) {
		drawEdgeWithColor(n->x, n->y, n->left->x, n->left->y, 0x0000FF);
		drawGraph(n->left);
	  }
	  if (n->right != NULL) {
		drawEdgeWithColor(n->x, n->y, n->right->x, n->right->y, 0x0000FF);
		drawGraph(n->right);
	  }
	  drawNodeWithColor(n->x, n->y, 0xFF0000);
	}


	// Open a display window
	int openDisplayWindow() {
	  // Opens connection to X server
	  display = XOpenDisplay(NULL);

	  // Create a simple window
	  win = XCreateSimpleWindow(display,                 // our connection to server
					RootWindow(display, 0),  // parent window (none in this example)
					0, 0,                // x,y (w.r.t. parent ... ignored here)
					WIDTH*25,HEIGHT*25,      // width, height
					0,                   // border width
					0x000000,                // border color (ignored in this example)
								0xFFFFFF);               // background color = WHITE

	  // Set the name of the window
	  XStoreName(display, win, "Maze Displayer");

	  // Get the graphics context
	  gc = XCreateGC(display, win, 0, NULL);

	  // Make it visible
	  XMapWindow(display, win);
	  XFlush(display);
	  usleep(20000);  // sleep for 20 milliseconds.
	}


	// Close the display window
	int closeDisplayWindow() {
	  // Clean up and close the window
	  XFreeGC(display, gc);
	  XUnmapWindow(display, win);
	  XDestroyWindow(display, win);
	  XCloseDisplay(display);
	}


mazeDisplay.c:
	#include <stdio.h>
	#include <X11/Xlib.h>
	#include <unistd.h>

	#include "graphSet.h"
	#include "mazeDisplay.h"

	#define SCALE 25

	// These are display-related variables
	Display *display;
	Window   win;
	GC       gc;


	// Draw the Maze on the window.  
	void drawMaze(char grid[WIDTH][HEIGHT]) {
	  // First erase background
	  XSetForeground(display, gc, 0xFFFFFF);
	  XFillRectangle(display, win, gc, 0, 0, 750, 750);
	  XFlush(display);

	  // Draw the grid maze
	  for (int y=0; y<WIDTH; y++) {
		for (int x=0; x<HEIGHT; x++) {
		  if (grid[y][x] == '1') 
		XSetForeground(display, gc, 0x333333);
		  else
			XSetForeground(display, gc, 0xFFFFFF);
		  XFillRectangle(display, win, gc, x*SCALE, y*SCALE, SCALE, SCALE);
		}
	  }
	  XFlush(display);
	}


	// Draws an edge with the given color (e.g., 0x0000FF is blue)
	// from cell (c1, r1) to cell (c2, r2) of the maze
	void drawEdgeWithColor(int c1, int r1, int c2, int r2, int color) {
	  XSetForeground(display, gc, color);
	  XDrawLine(display, win, gc, c1*SCALE + SCALE/2, r1*SCALE + SCALE/2, c2*SCALE + SCALE/2, r2*SCALE + SCALE/2);
	  XFlush(display);
	}

	// Draws a node with the given color (e.g., 0x0000FF is blue)
	// centered at the given cell (c1, r1) of the maze.
	void drawNodeWithColor(int c1, int r1, int color) {
	  XSetForeground(display, gc, color);
	  XFillArc(display, win, gc,
		   c1*SCALE-SCALE/4 + SCALE/2,
		   r1*SCALE-SCALE/4 + SCALE/2,
		   11, 11, 0, 360*64);
	  XFlush(display);
	}


	// Draw a single graph starting at the given root node n.
	void drawGraph(Node *n) {
	  // Quit recursion if there are no Nodes
	  if (n == NULL)
		return;

	   // Recursively draw in each direction.  Draw the edges before the recursive call so that
	  // vertices are drawn on top of the edges
	  if (n->up != NULL) {
		drawEdgeWithColor(n->x, n->y, n->up->x, n->up->y, 0x0000FF);
		drawGraph(n->up);
	  }
	  if (n->down != NULL) {
		drawEdgeWithColor(n->x, n->y, n->down->x, n->down->y, 0x0000FF);
		drawGraph(n->down);
	  }
	  if (n->left != NULL) {
		drawEdgeWithColor(n->x, n->y, n->left->x, n->left->y, 0x0000FF);
		drawGraph(n->left);
	  }
	  if (n->right != NULL) {
		drawEdgeWithColor(n->x, n->y, n->right->x, n->right->y, 0x0000FF);
		drawGraph(n->right);
	  }
	  drawNodeWithColor(n->x, n->y, 0xFF0000);
	}


	// Open a display window
	int openDisplayWindow() {
	  // Opens connection to X server
	  display = XOpenDisplay(NULL);

	  // Create a simple window
	  win = XCreateSimpleWindow(display,                 // our connection to server
					RootWindow(display, 0),  // parent window (none in this example)
					0, 0,                // x,y (w.r.t. parent ... ignored here)
					WIDTH*25,HEIGHT*25,      // width, height
					0,                   // border width
					0x000000,                // border color (ignored in this example)
								0xFFFFFF);               // background color = WHITE

	  // Set the name of the window
	  XStoreName(display, win, "Maze Displayer");

	  // Get the graphics context
	  gc = XCreateGC(display, win, 0, NULL);

	  // Make it visible
	  XMapWindow(display, win);
	  XFlush(display);
	  usleep(20000);  // sleep for 20 milliseconds.
	}


	// Close the display window
	int closeDisplayWindow() {
	  // Clean up and close the window
	  XFreeGC(display, gc);
	  XUnmapWindow(display, win);
	  XDestroyWindow(display, win);
	  XCloseDisplay(display);
	}


mazeDisplay.h:
	#include <X11/Xlib.h>

	#define WIDTH  21
	#define HEIGHT 21

	// Draw the maze on the window.
	extern void drawMaze(char maze[WIDTH][HEIGHT]);

	// Draw the graph on the window.
	extern void drawGraph(Node *firstNodeOfGraph);

	// Open a display window
	extern int openDisplayWindow();

	// Close a display window
	extern int closeDisplayWindow();

	// Draws a node with the given color (e.g., 0x0000FF is blue)
	// centered at the given cell (c1, r1) of the maze.
	extern void drawNodeWithColor(int c1, int r1, int color);

	// Draws an edge with the given color (e.g., 0x0000FF is blue)
	// from cell (c1, r1) to cell (c2, r2) of the maze
	extern void drawEdgeWithColor(int c1, int r1, int c2, int r2, int color);


graphSet.h:
	// This struct represents a single intersection/Node in a maze.  It keeps track
	// of the x(i.e., column) and y (i.e. row) of the intersection in the maze
	// as well as the Nodes in all 4 directions around it).   NULL is used to
	// indicate that no Node is beside it in a particular direction.
	typedef struct nd {
	  int        x;
	  int        y;
	  struct nd *up;
	  struct nd *down;
	  struct nd *left;
	  struct nd *right;
	} Node;


	// This struct represents a single maze graph
	typedef struct gr {
	  Node       *rootNode;
	  struct gr  *nextGraph;
	} Graph;


	// This struct represents a set of maze graphs
	typedef struct {
	  Graph  *firstGraph;
	  Graph  *lastGraph;
	} GraphSet;


This post has been edited by Skydiver: 07 November 2019 - 07:40 PM
Reason for edit:: Put code in code tags. Learn to do this yourself.


Is This A Good Question/Topic? 0
  • +

Replies To: c - creating a makefile causes error “expected ‘;’, ‘,’ or ‘)’ before

#2 Evilgaga   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-October 19

Re: c - creating a makefile causes error “expected ‘;’, ‘,’ or ‘)’ before

Posted 07 November 2019 - 07:25 PM

Sorry I just realized I uploaded the wrong code for mazes.c.

Here is the code it should be:
#include <stdio.h>
#include <stdlib.h>

#include "graphSet.h"
#include "mazeDisplay.h"




// Compute the graph for the given maze and add it to the given graph set.
Graph *computeGraph(char maze[HEIGHT][WIDTH]) {

  // Create the initially-empty graph

  // Find a starting node, then trace out the maze recursively.  A starting node can be
  // found by searching from top to bottom, left to right, for a non-wall maze location.
  // You MUST NOT hard-code this start location ... it must be determined by your code.

  // To trace out the maze recursively, you will likely want to create a recursive
  // procedure that is called by this one.   It should take parameters to indicate
  // the location in the maze to start tracing from, the maze itself and also the node
  // that led to this node (i.e., the previous one in the tree that led here).  If you
  // start with the root node, then the previous node should be NULL.
  //
  // As you go through the maze, make sure to mark off maze locations that you have
  // visited (perhaps by putting a '2' character at that location) so that you do not
  // go back to that location again and end up with infinite recursion.  So you can
  // stop the recursion when you reach a wall (i.e., a '0' character in the maze) or a
  // '2' character.  A '1' character means that it is a free space that you just arrived
  // at for the first time.   Make sure to check recursively in all directions.  In my
  // solutions (shown on the assignment), I used an ordering of up/down/left/right.  So
  // if you want solutions to look like mine, you should follow that ordering as well.
  //
  // As you traverse the maze, make sure to connect the previous node to the current one.
  // You'll have to check which direction you can from (i.e., x and y values) so that
  // you know whether it is the up/down/left or right pointer to set.

  // You need not do this recursively, but it will lkely be a lot harder to do it non-
  // recursively.

  return NULL; // Remove this line when you write your code
}



// This procedure must clean up graph by removing all nodes in which the previous and
// next nodes have the same x or y value as it.
void cleanUpGraph(Node *n, Node *previousNode) {

}



// This is where it all begins
int main() {
  char mazes[5][HEIGHT][WIDTH] = {
    {"111111111111111111111",
     "100000001000100000001",
     "101111111010111011111",
     "100000000010000010001",
     "101110111111101110111",
     "100010001000000000001",
     "111011111111111110111",
     "101010001000100000001",
     "101110111011101011101",
     "100010000000001010001",
     "101010111011111111111",
     "101000001000100000001",
     "101111111110101111101",
     "100010100000100000101",
     "111110101110101111101",
     "100010001000000010101",
     "101010111111111010111",
     "101010001000000010001",
     "101111111010111011101",
     "100000000010001000001",
     "111111111111111111111"},

    {"111111111111111111111",
     "100000000000000000001",
     "101111111111111111111",
     "100000000000000000001",
     "101111111111111111111",
     "100000000000000000001",
     "111111111111111111101",
     "100000000000000000001",
     "101111111111111111111",
     "100000000000000000001",
     "111111111111111111101",
     "100000000000000000001",
     "101111111111111111111",
     "101111111111111111101",
     "101111111111111111101",
     "101000100010001000101",
     "101010101010101010101",
     "101010101010101010101",
     "101010101010101010101",
     "100010001000100010001",
     "111111111111111111111"},

    {"111111111111111111111",
     "100000000000000000001",
     "101010101010101010101",
     "100000000000000000001",
     "101110111011101110111",
     "100000000000000000001",
     "101111101111101111101",
     "100000000000000000001",
     "101111111001111111101",
     "100000000000000000001",
     "101111111111111111101",
     "100111111111111111001",
     "100011111111111110001",
     "100001111111111100001",
     "100000111111111000001",
     "100000011111110000001",
     "100000001111100000001",
     "100000000111000000001",
     "100000000010000000001",
     "100000000000000000001",
     "111111111111111111111"},

    {"111111111111111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111110111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111111111111111",
     "111111111111111111111"},

    {"111111111111111111111",
     "111100000000000000001",
     "111000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "100000000000000000001",
     "111111111111111111111"}};

  // Open a display window
  openDisplayWindow();



  // Allocate a GraphSet to store the graphs for each maze
  GraphSet *gSet;

  // Compute the graphs for each maze and add them to a Graph Set
  for (int i=0; i<5; i++) {
    Graph *g = computeGraph(mazes[i]);

    // Add g to gSet properly
    // ...
  }

  // Show the graphs
  Graph *g; // ... set this to the first graph in gSet ...

  for (int i=0; i<5; i++) {
    drawMaze(mazes[i]);  // Draw the maze
    // drawGraph(g->rootNode);    // Draw the graph

    getchar();  // Wait for user to press enter

    // cleanUpGraph(g->rootNode, NULL);   // Clean up the graph
    // drawMaze(mazes[i]);
    // drawGraph(g->rootNode);

    // ... get the next graph in the set ...
    // ... INSERT A LINE OF CODE HERE ...

    getchar();  // Wait again for the user to press ENTER before going on to the next maze
  }

  // Free up all allocated memory
  // ...

  // Close the display window
  closeDisplayWindow();
}


This post has been edited by Skydiver: 07 November 2019 - 07:39 PM
Reason for edit:: Put code in code tags. Learn to do this yourself.

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: c - creating a makefile causes error “expected ‘;’, ‘,’ or ‘)’ before

Posted 07 November 2019 - 07:38 PM

I've put your code in code tags. Please learn how to use code tags when you post code.

Please post the exact errors and warning you are getting. Don't summarize or paraphrase like you did in your original post. Just copy and paste them here verbatim. Don't use a screenshot. There are important clues in the error messages that will help identify and fix the problem. (Be sure to also put the error messages in code tags to preserve the formatting if the error messages quote code and have pointers to particular parts of a line.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1