Standard include paths and defines in preprocessor 标准的预处理器包含路径和定义

huangapple go评论58阅读模式
英文:

Standard include paths and defines in preprocessor

问题

我要写一个C预处理器。我需要手动定义类似__linux__这样的标准宏,还是可以在系统中找到定义并直接使用?

另外,我已经检查了我的环境变量,但找不到默认的包含路径在哪里。需要手动指定吗?

我已经查看了所有的环境变量和一些头文件,但没有宏或标准包含路径。

英文:

I'm gonna write a C preprocessor. Do I need to define the standard macros like __linux__ manually or are they defined somewhere I can just grab within the system?

Also, I've checked my environment variables but I can't find where the default include paths are. Do they need to be manually specified?

I've looked through all environment variables and some header files but there were no macros or standard include paths.

答案1

得分: 3

以下是您要翻译的内容:

首先,C预处理器只需要预定义一些宏 (C17, 6.10.8):

__DATE__
__FILE__
__LINE__
__STDC__
__STDC_HOSTED__
__STDC_VERSION__
__TIME__

同样,还有一些特定的宏 可能 会被预定义 (C17, 6.10.8.2 和 6.10.8.3)。

除此之外,任何额外的服务都是作为C预处理器的作者可能决定为用户提供的,但当然您需要知道如何定义给定的宏(在编译时硬编码,使用运行时检测机制,由配置文件提供,...)。没有一个标准的方法来执行这个操作。

标准的包含路径也是您需要为您的预处理器定义的内容 - 无论是在配置文件中提供的,还是作为程序输入,或者您将与特定编译器交互(例如 GCC)或其他方式。

实现一个完整的C预处理器不是一项小任务。如果使用现有的实现是一个选项,那可能更好。

英文:

First of all, a C preprocessor is only required to predefine a few macros (C17, 6.10.8):

__DATE__
__FILE__
__LINE__
__STDC__
__STDC_HOSTED__
__STDC_VERSION__
__TIME__

Similarly, there are some specific macros that may be predefined (C17, 6.10.8.2 and 6.10.8.3).

Anything beyond that is an additional service which you as the C preprocessor author may decide to offer your users, but of course you need to know how to define a given macro (hardcoded at compile-time, using a run-time detection mechanism, supplied by a configuration file, ...). There is no canonical way to do that.

Standard include paths is also something that you need to define for your Preprocessor - whether they are provided in a configuration file, as program input, or you will interface with a specific compiler (e.g. GCC) or something else.

Implementing a full C Preprocessor is not a small task. If using an existing implementation is an option, then that is probably to be preferred.

答案2

得分: -1

标准的宏,如__linux__,通常由编译器根据目标平台预定义。您不需要在预处理器中手动定义它们 Standard include paths and defines in preprocessor
标准的预处理器包含路径和定义

英文:

the standard macros like __linux__ are typically predefined by the compiler based on the target platform. You don't need to define them manually in your preprocessor Standard include paths and defines in preprocessor
标准的预处理器包含路径和定义

huangapple
  • 本文由 发表于 2023年5月22日 17:09:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定