cshellminix

undefined reference.. linker command failed with exit code 1


I want to create a shell file using the c language programming. I have this scheleton given already by the professor, but wen I try to executed I have this errorenter image description here and I have this problem

the myshell.c file (and this file is the one that I have to modify and execute)

/* RCS information: $Id: myshell.c,v 1.2 2006/04/05 22:46:33 elm Exp $ */

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>

extern char **getcmdstring(char *cmd);

int main(void) {
  int i;
  char **args; 
  char cmd[4096];

  while(1) {
    printf("$ ");
    fgets(cmd,4096,stdin);
    args = getcmdstring(cmd);
    for(i = 0; args[i] != NULL; i++) {
      printf("Argument %d: %s\n", i, args[i]);
    }
  }
}

and the shell.l file

/* RCS information: $Id: shell.l,v 1.1 2006/04/05 22:46:33 elm Exp $ */

#include <stdio.h>
#include <strings.h>
#include <string.h>

int _numargs = 200;
char *_args[200];
int _argcount = 0;
char *strdup(const char *);
%}

WORD [a-zA-Z0-9\/\.-]+
SPECIAL [()><|&;]

%%
  _argcount = 0; _args[0] = NULL;

{WORD}|{SPECIAL} {
  if(_argcount < _numargs-1) {
    _args[_argcount++] = (char *)strdup(yytext);
    _args[_argcount] = NULL;
  }
}

\n return (int)_args;

[ \t]+

.

%%

char **getcmdstring(char *cmd) {
    char **ret;
    yy_scan_string(cmd);
    ret = (char **)yylex();
    yy_delete_buffer(YY_CURRENT_BUFFER);
    return ret;
}

Solution

  • The missing function is included in the shell.l source. This is a source for a lexical analyzer. You need to build the shell.o object file from shell.l.

    This can be done by creating the C source

    flex -o shell.c shell.l
    

    and then compiling

    clang shell.c myshell.c -o myshell -ll