ayush1794
ayush1794

Reputation: 101

Restrict library linking in C

If I want gcc compiler to throw an error if I include stdlib.h or a particular header, what flags should I use?. I don't want to use -nostdlib flag, because it doesn't link any standard libraries.

Upvotes: 0

Views: 111

Answers (1)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

For each header file there is an include guard, so I guess you can try to check for it like this

#ifdef _STDLIB_H
#error "You should not include stdlib.h"
#endif

and then you can apply the same with other headers. Of course, this will only work with a particular c standard library in this case I took the include guard from stdlib.h in glibc. And it will not work if you include stdlib.h after checking.

Also, a header file is not a library, it contains definitions and functions prototypes from libraries, if you don't want you program to link to the standard library you have no other option AFAIK than gcc -nostdlib.

Upvotes: 3

Related Questions