可以在函数名称上使用#IFNDEF

possible to use #ifndef on a function name?

本文关键字:#IFNDEF 函数      更新时间:2023-10-16

我正在与Micropython合作,他们的标题文件使用大量

#ifndef function_name
void function_name(const char *str, size_t len);
#endif

使用在线C和C 编译器,我尝试了以下代码在C

中对此进行测试
#include <iostream>
using namespace std;
void func();    
#ifndef func
int a = 5;
#else
int a = 3;
#endif    
int main()
{
  cout << "a = " << a << endl;
}

和c

#include <stdio.h>
void func();

#ifndef func
int a = 5;
#else
int a = 3;
#endif

int main()
{
    printf("Hello World");
    printf("a = %dn", a);
    return 0;
}

两次,我都得到了5,这意味着在功能名称上使用#ifndef不起作用。

问题

是否有一些特殊的编译器标志可以启用此功能或它们有错误?

#ifndef X检查是否已定义了名称 X宏观。函数名称和宏名称属于不同的名称空间。功能X的存在或不存在不会影响#ifndef X