首页 > 解决方案 > 如何尝试捕获诸如“#include”之类的预处理器指令

问题描述

有没有一种方法可以尝试捕获#include 指令,因为#include 是处理器指令,并且try catch 方法在编译期间完成,它不会起作用有什么解决方法吗?我需要检查是否可以包含一个文件,否则包含另一个文件。

标签: c++includetry-catchpreprocessor

解决方案


__has_include 函数以及使用 #if 和 #elif 可以做到这一点。

#if defined __has_include
#  if __has_include (<stdatomic.h>)
#    include <stdatomic.h>
#  endif
#endif

来源:https ://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005finclude.html


推荐阅读