简体   繁体   English

为什么我不能检查我是否在 tdm-gcc 编译器中包含了 stdlib.h?

[英]Why cant i check if i have included stdlib.h on tdm-gcc compiler?

I'm writing a header file in C and need stdlib.h for it to work.我正在 C 中编写 header 文件,并且需要stdlib.h才能工作。 But, when I check if _STDLIB_H is defined, the pre-processor says it's not, even if I include the file.但是,当我检查是否定义了_STDLIB_H时,预处理器说它不是,即使我包含了文件。 I have tried it on multiple compilers, where it works on most but not on tdm-gcc .我已经在多个编译器上尝试过它,它适用于大多数但不适用于tdm-gcc How can I fix this?我怎样才能解决这个问题?

Looking at stdlib.h source code, it seems like the macro to look for in tdm-gcc might be _TR1_STDLIB_H .查看stdlib.h源代码,似乎要在 tdm-gcc 中查找的宏可能是_TR1_STDLIB_H
So you can try something like:因此,您可以尝试以下操作:

#if defined _STDLIB_H || defined _TR1_STDLIB_H

For a safer way to check if stdlib.h is properly included, you should check for a macro that the C standard requires the file to define.为了更安全地检查stdlib.h是否正确包含,您应该检查 C 标准要求文件定义的宏。

I may be missing something, but I don't see any requirement in the C standard for stdlib.h to define _STDLIB_H .我可能遗漏了一些东西,但我在 C 标准中没有看到任何要求stdlib.h定义_STDLIB_H I think this may just be a common way compilers decided to guard against multiple inclusion.我认为这可能只是编译器决定防止多重包含的一种常见方式。

try something like尝试类似的东西

#include <stdlib.h>

#ifndef NULL
#error "stdlib.h not included"
#endif

because the C standard requires that stdlib.h defines NULL因为 C 标准要求stdlib.h定义NULL

But none of this should be technically necessary... I don't know of a preprocessor that won't throw a fatal error if it can't find a file you tried to #include但这在技术上都不是必需的......我不知道如果找不到您尝试#include的文件时不会抛出致命错误的预处理器

EDIT:编辑:

According to the C standard stdio.h also defines NULL , so perhaps it would be better to check for EXIT_SUCCESS or EXIT_FAILURE根据 C 标准stdio.h还定义了NULL ,所以检查EXIT_SUCCESSEXIT_FAILURE可能会更好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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