简体   繁体   中英

Implicit declaration of function in recursive call C

I'm trying to write a recursive function to list out all the directories until a regular file is inputted but I get the following error:

1.c:25:21: warning: implicit declaration of function 'changeDirectoryAndGetFileName' is invalid in C99 [-Wimplicit-function-declaration] if(!isFile(sf)) changeDirectoryAndGetFileName(); ^

1 warning generated. Undefined symbols for architecture x86_64: "_changeDirectoryAndGetFileName", referenced from: _changeDirectoryAndGetFilename in 1-b3c344.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here is my code:

#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>

void listCurrentDirectory(){
    DIR *d;
    struct dirent *entry;
    d = opendir(".");
    while((entry=readdir(d)) != NULL)printf("%s\n", (*entry).d_name);
    closedir(d);
}

int isFile(char *fname){
    struct stat pstat;
    stat(fname, &pstat);
    return S_ISREG(pstat.st_mode);
}

char *changeDirectoryAndGetFilename(){
    listCurrentDirectory();
    char sf[] = "Placeholder";
    scanf("%s", sf);
    printf("File selected: %s\n", sf);
    if(!isFile(sf)) changeDirectoryAndGetFileName();

    return "d";
}

int main(int argc, char *argv[]){

    if(argc != 2){
        printf("code(-c) or decode(-d)\n");
        return 1;
    }
    changeDirectoryAndGetFilename();
    return 0;
}

Undefined symbols for architecture x86_64: "_changeDirectoryAndGetFile N ame", referenced from: _changeDirectoryAndGetFile n ame

Notice the capitalization of the n in the name of the called function compared to the calling function. In other words: changeDirectoryAndGetFile n ame is indeed defined, but changeDirectoryAndGetFile N ame (with a capital N) is not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM