诡异的_GNU_SOURCE宏

好久没更新博客了,突然来一篇,这...难道是干货?......在看正文之前允许我先说几句话,骂人抓狂发火。好了看下面的内容:


诡异代码之test.c:

#include <aio.h>

int main() 

{

     struct aioinit aa;

     aa.aio_threads = 10;

     return 0;

}

使用glibc的aio需要在链接时,用-lrt:

gcc -o test test.c -lrt

test.c:4: error: storage size of ‘aa’ isn’t known

Why ???

代码修改一下:

#define _GNU_SOURCE

#include <aio.h>

int main() 

{

     struct aioinit aa;

     aa.aio_threads = 10;

     return 0;

}

就OK了。Why?

Defining _GNU_SOURCE has nothing to do with license and everything to do with writing (non-)portable code. If you define _GNU_SOURCE, you will get:
  1. access to lots of nonstandard GNU/Linux extension functions
  2. access to traditional functions which were omitted from the POSIX standard (often for good reason, such as being replaced with better alternatives, or being tied to particular legacy implementations)
  3. access to low-level functions that cannot be portable, but that you sometimes need for implementing system utilities like mountifconfig, etc.
  4. broken behavior for lots of POSIX-specified functions, where the GNU folks disagreed with the standards committee on how the functions should behave and decided to do their own thing.
As long as you're aware of these things, it should not be a problem to define _GNU_SOURCE, but you should avoid defining it and instead define _POSIX_C_SOURCE=200809L or _XOPEN_SOURCE=700when possible to ensure that your programs are portable.
In particular, the things from _GNU_SOURCE that you should never use are #2 and #4 above.

http://stackoverflow.com/questions/5582211/what-does-define-gnu-source-imply

为了这个问题折腾了好久,请问您现在的感受是什么?


              “少侠,你还太年轻!”

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值