简体   繁体   中英

Make the C preprocessor ignore certain #include directives

I use a parser generator here, that unfortunately insists on putting a

#include <some/file.h>

at the top of every generated source file. The header has since long been renamed. While it is no problem forcing the compiler (gcc) to use the new header with -include new/header.h , removing the above directive from every generated file complicates the build-process.

Is there a way to tell gcc to simply ignore some/file.h ?

用一个空文件替换some/file.h

No. You can post-process your generated file - I say: NO!!!

Or you can just add '.' to your system include directories (or whatever your local include path is - make sure it's also a <> system include path).

Then make a 'some' directory and stick your own permanent 'file.h' in there that has 1 line for #include and get rid of your -include.

I'm guess there's some reason that might not work - cause it seems like the more straight forward and understandable thing to do before using -include. Especially since you can comment the pass-through file to explain what's going on.

为什么不建立从some / file.h到new / header.h的符号链接,并删除-include指令?

#include <some/file.h>

may start as something like

#ifndef _FILE_H_
#define _FILE_H_

If so, just add #define _FILE_H_ before the #include command and it should ignore it. I'm not sure whether this is the best solution, though.

Try using preprocessor directives like #if and #ifdef and gcc -DSYMBOL=value command line flag.

In example, if you compile using gcc -DREQUIRE_STDC=1 -o myfile.o myfile.c , and your .c file contains:



#if defined(REQUIRE_STDC) && defined(__STDC__)
#include "some/file.h"
#else
#include "another/file.h"
#endif /* defined(REQUIRE_STDC) && defined(__STDC__) */

It will compile using "some/file.h" if have both STDC and REQUIRE_STDC symbols defined. Also your header may include the proper directive to avoid multiple inclusions of the same file:



#ifndef MY_HEADER_FILE
#define MY_HEADER_FILE 1

/* your C declarations here */

#endif /* MY_HEADER_FILE */

Also, you could the gcc preprocessor manual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM