gpt4 book ai didi

同时使用 c89 和 POSIX

转载 作者:太空宇宙 更新时间:2023-11-04 01:31:30 24 4
gpt4 key购买 nike

是否可以在严格的 std=c89 中使用 POSIX 函数?当我尝试在 Linux 上以严格的 ANSI C 模式编译可执行文件时,gcc 和 clang 对 readlinkrealpath 等函数一无所知,尽管包含了 header 。因为即使在 ANSI C 模式下我也必须使用 POSIX 函数,所以我正在寻找实现它的方法。我考虑过 dlsym,但我不知道我必须打开哪个库。此类调用被#ifdef 包围,因此它们不会在其他系统上引起警报。需要跨平台解决方案。提前致谢!

最佳答案

您正在寻找“功能测试宏”。参见 the Single Unix Specification, Issue 6: System Interfaces Chapter 2.2, "The Compilation Environment"

编辑:

引用该页面:

The _POSIX_C_SOURCE Feature Test Macro

A POSIX-conforming application should ensure that the feature test macro _POSIX_C_SOURCE is defined before inclusion of any header.

GCC 和 clang 目前默认为您定义 _POSIX_C_SOURCE,除非将 c89、c99、c11 或任何行为等效的字符串之一传递给编译器的 -std 选项。

另外:

The _XOPEN_SOURCE Feature Test Macro

An XSI-conforming application should ensure that the feature test macro _XOPEN_SOURCE is defined with the value 600 before inclusion of any header. This is needed to enable the functionality described in The _POSIX_C_SOURCE Feature Test Macro and in addition to enable the XSI extension.

换句话说,为了保证您的程序(又名应用程序)可以使用 POSIX,可以在包含任何 header 之前 #define _POSIX_C_SOURCE 200112L 或传递 -D_POSIX_C_SOURCE=200112L编译器的选项。对于 XSI 功能,您必须将 _XOPEN_SOURCE 定义为值 600。

还有更新版本的单一 Unix 规范 — 第 7 期。Very similar text can be found in Issue 7 .与上述文本的唯一真正区别是 _POSIX_C_SOURCE 和 _XOPEN_SOURCE 的编号已更改。

关于同时使用 c89 和 POSIX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21867897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com