Stop g++ compiler from including specific header files -
as title stated, want compiler fail when include header files; example, <cmath>
is possible compiler flags? or have delete headers?
#include <cmath>
has nothing library, , header file. assuming mean want compilation fail if include particular header file, should able leverage gcc's support specifying include directories through environment variables.
to so, create or edit appropriate environment file. if using gnu bash on debian, example, can create file /etc/profile.d/gcc-include-dirs
. readable in /etc/profile.d sourced when shell launched, apply shells started after point. (to absolutely certain, may want log out , in once, issue env | grep include
confirm.) create such file favorite editor , add following it:
export c_include_path=/usr/local/include/fail:${c_include_path} export cplus_include_path=/usr/local/include/fail:${cplus_include_path}
make sure file readable (chmod 644 /etc/profile/gcc-include-dirs
) , owned root (chown root:root /etc/profile/gcc-include-dirs
).
you can put file elsewhere , source
when needed, if need behavior @ specific times. in case, logging out shell in question , logging in restore gcc's normal behavior (you don't need log out entire session, particular shell instance). starting subshell , sourcing file within subshell work nicely in case; exit
when done.
then create file /usr/local/include/fail/cmath
following content:
#error "failing because included 'cmath'"
make sure file readable , owned root. #error , evil twin #warning emit fatal error , compilation warning, respectively, whenever file gets included, gcc encounter #error preprocessor directive resulting in emission of fatal error causes compilation fail.
if want override behavior single compilation, use gcc's -i
parameter specify path directory real math.h lives. since -i
takes precedence on $c_include_path
, $cplus_include_path
means include c library's version. example, cc -o mathprogram -i/usr/include mathprogram.c
use math.h in /usr/include when #include <math.h>
regardless of might in /usr/local/include/fail, because looks first in /usr/include.
since affects compilation (and compilation started through shell), on system unaffected (unless have weird dependencies 2 environment variables).
for c*
headers, may need create corresponding *.h
header content identical c*
header. because e.g. cmath
might map math.h
(the name of same header in c). make file 1 above, complain math.h
instead. (gcc doesn't care, makes diagnostics easier.) may need put files in other places (i'm not sure include directory structure gcc wants c++ headers); in case, find / -name cmath
(or similar) give idea of structure need replicate under /usr/local/include/fail.
do note not stop people copying relevant parts of header file own source code; there nothing magical header files compiler's point of view. depending on trying protect against, may issue.
Comments
Post a Comment