2014-02-22 39 views
0

我想使用c语言编程创建一个shell文件。我有教授给出的这个架构,但是我尝试执行我有这个错误enter image description here,我有这个问题未定义的引用..链接器命令失败,退出代码为1

myshell.c文件(和这个文件是我必须修改和执行的文件)

/* 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]); 
    } 
    } 
} 

和shell.l文件

/* 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; 
} 
+0

你不需要'extern'用一个函数声明来完成。 – ajay

回答

1

丢失的功能被包括在shell.l源。这是一个词法分析器的来源。您需要从shell.l构建shell.o目标文件。

这可以通过创建C源代码

flex -o shell.c shell.l 

,然后编译

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

它不工作,因为我得到了另一个错误 铛错误没有这样的文件或目录:shell.c – user3340449

+0

请参阅更新的答案。 –

+0

它不工作要么 弯曲的线2592 – user3340449

相关问题