There's really no portable way to do this since the header text doesn't even need to exist in a file at all! It's quite acceptable under the standard for the #include <system header file>
to simply modify the environment without reference to a specific actual header file so, in that case, you don't have easy access to the information from a program.
In terms of finding out if the header text is available, this could range from a simple text search (likely to have false positives since there may be a comment about printf
in the math.h
header file) to a full blown C-aware parser (likely to be complicated).
Alternatively, you could just (manually) reference the actual standard since those details are available there, a la:
7.21.4.1 The remove function
Synopsis
#include <stdio.h>
int remove(const char *filename);
This little snippet from C11 means that the remove
function can be found in stdio.h
.
And, if you still want a way to do this within a program, simply gather all that knowledge from the standard into a file or data structure (like an associative array) and write code to look up a given identifier, along the lines of:
#include <string.h>
#include <assert.h>
const char *lookFor (const char *ident) {
// These should have ALL identifiers and their
// equivalent headers.
static const char const *identifier[] = { "remove", "strcpy", ... };
static const char const *header[] = { "stdio.h", "string.h", ... };
assert (sizeof(identifier) == sizeof(header));
for (size_t i = 0; i < sizeof(header) / sizeof(*header); i++)
if (strcmp (ident, identifier[i]) == 0)
return header[i];
return NULL;
}
gc
c you can use -aux-info option to print to file those infos. E.g.gcc test.c -aux-info=infos.txt -o test
– LPsprintf
andexit
, include that, and your program wil compile as usual. – Jongware