• Uncategorized

About c : keep-getting-implicit-declaration-errors

Question Detail

Error

In function ‘main’:
assignment.c:46:14: warning: implicit declaration of function ‘fork’ [-Wimplicit-function-declaration]
   46 |     int pid= fork();
      |              ^~~~
assignment.c:49:9: warning: implicit declaration of function ‘execvp’ [-Wimplicit-function-declaration]
   49 |         execvp(command[0],command);
      |         ^~~~~~
assignment.c:54:9: warning: implicit declaration of function ‘wait’ [-Wimplicit-function-declaration]
   54 |         wait(NULL);
      |         ^~~~
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    
    char line[200];
    char* command[100];
    char progpath[20];
    int argc;

while(1){

printf("Enter command and press ctrl + D or type Exit to terminate program \n ");                   

        if(!fgets(line, 200, stdin)){  
        break;                                
    }
    size_t length = strlen(line);
    if(line[length - 1] == '\n')
    line[length - 1] = '\0';
    if(strcmp(line, "exit")==0){            
        break;
    }

    char *token;                  
    token = strtok(line," ");
    int i=0;
    while(token!=NULL){
        command[i]=token;
        token = strtok(NULL," ");
        i++;
    }
     command[i]=NULL;                    

    argc=i;                          
    for(i=0; i<argc; i++){
        printf("%s\n", command[i]);      
    }
    for(i=0; i<strlen(progpath); i++){    
        if(progpath[i]=='\n'){
            progpath[i]='\0';
        }
    }

    int pid= fork();           

    if(pid==0){              
        execvp(command[0],command);
        fprintf(stderr, "Child process could not do execvp\n");

    }
    else{                  
        wait(NULL);
        printf("Child exited\n");
    }

}
} 

pls help me to solve this problem as soon as possible

Question Answer

You need to add these preprocessor directives:

#include <sys/types.h>
#include <unistd.h>    // fork, execvp
#include <sys/wait.h>  // wait

Also, fork() does not return an int but a pid_t. Use the correct types.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.