0

I improved my signal handling function but now when I try to compile my program via gcc ./test2.c -Wall -Wextra, I receive the following;

./test2.c: In function 'end_app':
./test2.c:21: warning: implicit declaration of function 'flock'
./test2.c: In function 'main':
./test2.c:46: warning: implicit declaration of function 'usleep'
./test2.c:47: warning: implicit declaration of function 'close'

This is the source code to test2.c

#include <stdio.h>
#include <stdlib.h>
    #include <signal.h>
    #include <fcntl.h>

    static int end=0;
    static int f;

    static void end_app(int sig){
      printf("Ending from sig#%d",sig);
      struct sigaction si;
      si.sa_handler=SIG_DFL;
      si.sa_flags=0;
      sigaction(SIGCHLD,&si,NULL);
      sigaction(SIGTSTP,&si,NULL);
      sigaction(SIGTTOU,&si,NULL);
      sigaction(SIGTTIN,&si,NULL);
      sigaction(SIGSEGV,&si,NULL);
      sigaction(SIGTERM,&si,NULL);
      sigaction(SIGHUP,&si,NULL);
      flock(f,LOCK_UN); //compiler gives implicit declaration warning
      printf("Ended\n");end=1;
    }

    void newsig(){
      struct sigaction s,o;
      o.sa_handler=SIG_DFL;
      o.sa_flags=0;
      s.sa_handler=&end_app;
      s.sa_flags=SA_RESTART;
      sigaction(SIGCHLD,&s,&o);
      sigaction(SIGTSTP,&s,&o);
      sigaction(SIGTTOU,&s,&o);
      sigaction(SIGTTIN,&s,&o);
      sigaction(SIGSEGV,&s,&o);
      sigaction(SIGTERM,&s,&o);
      sigaction(SIGHUP,&s,&o);
    }

int main(){
      f=open("xx.pid",O_WRONLY|O_CREAT|0x700);
      flock(f,LOCK_EX); //function works here
      printf("Started\n");
      newsig();
      while(1){
        usleep(1000);
        if (end==1){close(f);return 0;}
      }
      return 0;
    }

My question is why would the compiler complain about the use of flock in my signal handler function but it doesn't complain about it in the main function? and what can I do to rectify this? I need flock to work so that I can successfully lock and unlock the file.

2

2 Answers 2

3

As the warning says, the flock function is implicitly declared the first time you use it. Since it was already (implicitly) declared earlier in the file, the compiler doesn't complain about the later usage.

If you comment out the first call, then it should complain about the second.

You should include the header file where the function is declared to get rid of the warning. According to the manpage link in Joachim Pileborg's comment above, the header is <sys/file.h>.

2

You're getting that warning because you haven't added the proper #includes (i.e. you used those functions without an explicit declaration). When a function isn't given an explicit prototype, the compiler assumes each parameter type is int. This can cause undesired behavior when the size of actual function parameter types don't equal the size of int.

GCC only issues this warning once per function so your build output won't be cluttered with pages of the same message.

For flock() you need #include <sys/file.h>.

usleep() and close() require #include <unistd.h>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.