macro definition containing #include directive

前端 未结 9 963
抹茶落季
抹茶落季 2020-12-08 06:55

Is there a way to define a macro that contains a #include directive in its body?

If I just put the \"#include\", it gives the error

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 07:31

    I also wanted to do this, and here's the reason:

    Some header files (notably mpi.h in OpenMPI) work differently if you are compiling in C or C++. I'm linking to a C MPI code from my C++ program. To include the header, I do the usual:

    extern "C" {
    #include "blah.h"
    }
    

    But this doesn't work because __cplusplus is still defined even in C linkage. That means mpi.h, which is included by blah.h, starts defining templates and the compiler dies saying you can't use templates with C linkage.

    Hence, what I have to do in blah.h is to replace

    #include 
    

    with

    #ifdef __cplusplus
    #undef __cplusplus
    #include 
    #define __cplusplus
    #else
    #include 
    #endif
    

    Remarkably it's not just mpi.h that does this pathological thing. Hence, I want to define a macro INCLUDE_AS_C which does the above for the specified file. But I guess that doesn't work.

    If anyone can figure out another way of accomplishing this, please let me know.

提交回复
热议问题