预处理器是否可以检查是否使用了链接器选项?

Is it possible for preprocessor to check if a linker option is used?

我有一个使用 clock_gettime 的定时器程序。这需要 -lpthread 选项才能成功编译,否则会出现 undefined reference to 'clock_gettime' 错误。

到目前为止,我探索了我们可以使用 #warning 生成预处理器警告消息,并通过 #if __has_include("<pthread.h>") 检查 include 是否存在。但是检查那些并没有真正检查是否定义了 clock_gettime 函数。

只是好奇我是否可以制作一条自定义消息,在编译时或预处理时警告那些在编译时没有包含 -lpthread 选项的人。

我在 Windows 上使用 MinGW。

I have a timer program which uses clock_gettime. This requires -lpthread option to compile successfully or else it gets undefined reference to clock_gettime error.

clock_gettime 需要 -lrt,参见 man clock_gettime-lpthread 正好依赖于那个库。

-lpthread linker 选项永远不要使用,因为它是不够的。编译和 link 多线程应用程序的正确方法是使用 -pthread 编译器和 linker 选项。编译器选项定义了所需的宏,linker 选项 link 是正确的库。

Just curious if I can make a custom message to warn people at compile time or preprocessing time who compiled without it to include -lpthread option.

-lrt 是一个 linker 选项。当目标文件与 linking 分开编译时,linker 选项不是编译器命令行的一部分,因此在那个阶段不可用。有 link 时间代码生成,但是,编译器通常不会将 linker 选项导出为正在编译的代码以检查的宏。