【问题标题】:error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token错误:在“{”标记之前应为“=”、“,”、“;”、“asm”或“__attribute__”
【发布时间】:2016-06-18 12:29:51
【问题描述】:

我正在开发一个简单的 shell 项目,并且我的主循环正在运行。我可以输入命令,它会解析字符串。我也有一个 which 命令正在工作。我做了一些小改动,现在我得到了错误:

sh.c: In function ‘which’:
sh.c:15: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
sh.c:85: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
sh.c:92: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
sh.c:97: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
sh.h:6: error: old-style parameter declarations in prototyped function definition
sh.c:100: error: expected ‘{’ at end of input
make: *** [sh.o] Error 1

下面是我的 sh.c 文件。我尝试完全删除我的 which 代码,因为我认为这是问题所在。但它什么也没改变。

#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include "sh.h"

int sh( int argc, char **argv, char **envp )
{
  char *prompt = calloc(PROMPTMAX, sizeof(char));
  char *commandline = calloc(MAX_CANON, sizeof(char));
  char *command, *arg, *commandpath, *p, *pwd, *owd;
  char **args = calloc(MAXARGS, sizeof(char*));
  int uid, i, status, argsct, go = 1;
  struct passwd *password_entry;
  char *homedir;
  struct pathelement *pathlist;

  uid = getuid();
  password_entry = getpwuid(uid);               /* get passwd info */
  homedir = password_entry->pw_dir;     /* Home directory to startout with*/

  if ( (pwd = getcwd(NULL, PATH_MAX+1)) == NULL )
  {
    perror("getcwd");
    exit(2);
  }
  owd = calloc(strlen(pwd) + 1, sizeof(char));
  memcpy(owd, pwd, strlen(pwd));
  prompt[0] = ' '; 
  prompt[1] = '\0';

  /* Put PATH into a linked list */
  pathlist = get_path();

  while ( go )
  {
    /* print your prompt */
    printf(" [%s]>", getcwd(NULL, PATH_MAX+1));
    /* get command line and process */
      fgets(commandline, MAX_CANON, stdin);

    printf("in commandline %s", commandline);

    args[0] = strtok(commandline, " \n");
    int n = 0;
    printf ("in args %s;\n", args[0]);
    while(args[n]!=NULL){
      n++;
      args[n] = strtok(NULL, " \n");
      printf ("in args %s;\n", args[n]);
    }



    /* check for each built in command and implement */
    if(strcmp(args[0], "exit") == 0){
      printf("exiting\n");
      exit(0);
    }

    if(strcmp(args[0], "which") == 0){
      which(args, pathlist);

    }

  }
  return 0;
} /* sh() */

int which(char **args, struct pathelement *pathlist )
{
  return 0;
}



char *where(char *command, struct pathelement *pathlist )
{
  /* similarly loop through finding all locations of command */
} /* where() */

void list ( char *dir )
{
  /* see man page for opendir() and readdir() and print out filenames for
  the directory passed */
} /* list() */

下面是我的 sh.h 文件

#include "get_path.h"

int pid;
int sh( int argc, char **argv, char **envp);
int which(char **args, struct pathelement *pathlist )
char *where(char *command, struct pathelement *pathlist);
void list ( char *dir );
void printenv(char **envp);

#define PROMPTMAX 32
#define MAXARGS 10

我知道这可能是一些愚蠢的错误,例如缺少分号或括号。但是我把所有的代码都翻了好几遍了,好像没找到。

【问题讨论】:

  • int which(char **args, struct pathelement *pathlist )

标签: c shell compiler-errors


【解决方案1】:

如果没有所有代码,很难看到所有错误和对应的行。但是在您的sh.h 文件中,我已经看到您在int which(char **args, struct pathelement *pathlist ) 行之后缺少;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多