获取退出状态从使用atexit注册的函数中() [英] Getting the exit status from inside an function registered with atexit()

查看:176
本文介绍了获取退出状态从使用atexit注册的函数中()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的的atexit()注册的功能,我想获得的退出状态(无论是参数为退出(3)或什么的main()与返回)。

Inside my atexit() registered function I would like to get the exit status (either the argument to exit(3) or what main() returned with).

是否有这样做的任何可移植的方法?是否有任何GNU库做这件事的具体方式,如环球控股是看重我可以参考?

Is there any portable way of doing this? Is there any GNU libc specific way of doing it such as a global holding that value I can reference?

推荐答案

下面是一个黑客:

// hack.c
int last_exit;

// hack.h
extern int last_exit;
#define exit(x) (exit)(last_exit = (x))

不会为<$​​ C $ C>收益,但是,嘿,它的便携!工作

Won't work for return, but, hey, it's portable!

在一个更维护者友好的音符,你可能要考虑编写某种形式的包装来为你做一些类似的。黑客围绕如何GCC工具退出()像一个维护的噩梦声音。更好地写出了一些辅助功能,该出口适合你,甚至与宏掩盖他们,如果你到那种事。用宏,你甚至可以更换收益通话,如果你总是叫收益使用括号。虽然这听起来像更加维护噩梦。

On a more maintainer-friendly note, you may want to consider writing some form of wrapper to do something similar to this for you. Hacking around how GCC implements exit() sounds like a maintenance nightmare. Better to write a few helper functions that exit for you, and maybe even mask them with macros if you're into that kind of thing. With a macro you might even be able to replace return calls, if you always call return with parenthesis. Though this sounds like even more of a maintenance nightmare.

这篇关于获取退出状态从使用atexit注册的函数中()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆