c-创建makefile会导致在“ *”令牌之前出现“预期的';',','或')'错误”

问题描述 投票:-1回答:2

因此,我正在创建一个迷宫游戏程序,但是为了使我能够真正开始编码,我应该创建一个makefile,该文件将mazes.c和mazeDisplay.c文件编译并链接到一个名为mazes的可执行文件中。 。 makefile应该允许make all和make clean命令正常工作,并且我还应该包括-lX11库,以便代码正确链接。

在过去的两天里,我一直在尝试编译程序并创建我的makefile,但我什么都无法工作。我的编译器似乎无法识别什么是指针。我在'*'令牌图* computeGraph(char maze [HEIGHT] [WIDTH]){“之前一直收到错误“错误:预期的';',','或')',我开始感到非常焦虑我的任务很快就要到期,由于这个makefile问题,我什至无法编译启动程序所需的启动程序文件。请任何帮助将不胜感激。我一直在试图做一个简单的任务时迷失了方向,我只是想不出问题出在哪里。我的makefile指令有问题吗?

Makefile:

GCC = gcc

all: mazes.o mazeDisplay.o
    $(GCC) -o mazes mazes.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 *.o mazes

CODE:

mazes.c:

#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();
}

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;

登录品牌:

student@COMP2401-F19:~/Desktop/Mazes$ make

gcc -c mazes.c

mazes.c:11:7: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token

 Graph *computeGraph(char maze[HEIGHT][WIDTH]) {


makefile:7: recipe for target 'mazes' failed

make: *** [mazes.o] Error 1
c pointers makefile compiler-errors maze
2个回答
0
投票

makefile有点奇怪:

规则:

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

应该参考mazes.o,而不是maze.o。一旦修复,该项目就会为我编译(Mint 19 / Gcc 7.4)。运行它会显示迷宫。


0
投票

查看您的pastebin,正在编译的文件已损坏,与您在此处显示给我们的文件不同。

根据pastebin输出,mazeDisplay.h文件包含以下文本:

extern void drawMaze(char maze[21][21]);

extern void drawGraph(Node *firstNodeOfGraph);

extern int openDisplayWindow();

extern int closeDisplayWindow();

extern void drawNodeWithColor(int c1, int r1, int color);

extern void drawEdgeWithColor(int c1, int r1, int

(注释等被预处理器消除了)。请注意,该文件如何在drawEdgeWithColor()的函数声明的中间结束。

这就是为什么看到语法错误的原因:因为mazeDisplay.h文件中最后一行的末尾缺少。

我所能建议的是,当您将这些文件复制到虚拟机上时,您不知何故没有复制整个文件,但是错过了最后几个字符。

通常最好使用scp之类的文件来复制文件。但是这里的另一个重要教训是,在寻求帮助时,请确保提供正在使用的实际文件,从正在编译它们的系统中剪切和粘贴的文件,而不是发布您认为相同的其他文件...他们可能不是。如果提供给他们的信息不正确,人们将无法帮助您。

[此外,您的mazes.c文件中还有一些奇怪的地方;似乎您试图将graphSet.h文件的内容直接插入mazes.c文件中;您不需要这样做,并且绝对不应该同时包含两者。

© www.soinside.com 2019 - 2024. All rights reserved.