Unix/C 命令行参数:每个函数都有相同的错误我做错了什么?

Unix/C command line arguments: Every single function has the same error what am I doing wrong?

我应该从命令行接收参数,以构建一个示例 Unix 风格 'ls' 风格的程序,列出目录内容。

我有预先构建的函数,我必须将代码分成模块化的头文件和每个单独函数的 c 文件,并创建一个 makefile。

makefile 将 运行 并给出这些警告:

-bash-3.2$ make run
gcc -c main.c
main.c: In function ‘main’:
main.c:18: warning: passing argument 1 of ‘do_ls’ from incompatible pointer type

这里是do_ls.h:

'''

#ifndef DO_LS
#define DO_LS
                void do_ls( char*[] );


#endif

'''

错误:

-bash-3.2$ gcc main.c
main.c: In function ‘main’:
main.c:18: warning: passing argument 1 of ‘do_ls’ from incompatible pointer type
main.c:23: warning: passing argument 1 of ‘do_ls’ from incompatible pointer type
/tmp/cc8Q7153.o: In function `main':
main.c:(.text+0x1b): undefined reference to `do_ls'
main.c:(.text+0x47): undefined reference to `do_ls'
collect2: ld returned 1 exit status

主要内容:

#include        <stdio.h>
#include        <sys/types.h>
#include        <dirent.h>
#include        "do_ls.h"


    int main(int ac, char *av[])
    {
     if ( ac == 1 )
            do_ls(".");

     else
            while ( --ac ){
                    printf("%s:\n", *++av );
                    do_ls( *av );
            }
    }

do_ls 函数需要一个 char * 的数组,但是当您调用它时,您只传入一个 char *。这就是最初调用 make 时所抱怨的警告。此参数不匹配调用 undefined behavior.

试着这样称呼它:

if ( ac == 1 ) {
    char *args[] = { ".", NULL };
    do_ls(args);
} else {
    do_ls(av+1);
}