Home > Blockchain >  Implicit declaration of a function in spite of its inclusion in a header file
Implicit declaration of a function in spite of its inclusion in a header file

Time:12-19

It's probably best to mention that I am running macOS for this and have found no issues on another Linux machine.

My project has a master.h file which includes a lot of headers which are used throughout the project. Here's a snippet:

#ifndef PROGETTOSO_MASTER_H
#define PROGETTOSO_MASTER_H

#define _POSIX_C_SOURCE 199309L
#define _GNU_SOURCE

#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <sys/stat.h>

Now, it seems like it works fine for all places but a particular function call in master.c

printf("Signal: %s\n", strsignal(signum)); generates a warning (and thus an error) that strsignal is implicitly declared and when I try and run a quick fix via CLion on it, it simply places an include of strings.h at the top of the file, before the master.h include. That solves it, but I don't understand why, it's already included in master just like everything else there, why's the compiler having issues with it?

Also interestingly strings.h is not a listed requirement in the man page synopsis, signal.h is and removing the strings include in favour of the signal one still solves the issue. Also works if I use only string.h as well.

So while this last bit is a little bit out of the question relevance, I thought I would still mention it, in any case, the include issue still stands.

I've tried looking at different SO questions (such as this one), but haven't really managed to apply the answers to my codebase, or simply do not understand the situation I am in at the moment.

CodePudding user response:

The problem is your particular choice of feature test macros:

#define _POSIX_C_SOURCE 199309L
#define _GNU_SOURCE

strsignal() dates to the 2008 version of POSIX. Defining _POSIX_C_SOURCE as 199309L specifically requests that strsignal() not be declared. But that applies only to headers included after those macros are declared.

However, including one of the headers that (directly or indirectly) declares that function before the feature-test macros are defined can still get you a declaration of that function. It can also get you inconsistency in which functions are declared and which not.

The correct solution is to use the correct feature test macros for the features you want. In this case, you could try changing the _POSIX_C_SOURCE macro to

#define _POSIX_C_SOURCE 200809L
  • Related