未定义的功能感测 [英] Undefined function sensing

查看:68
本文介绍了未定义的功能感测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用GCC编译器执行此操作(这可能吗?):

I am trying to do this (is this possible?) with GCC compiler:

指定一个函数,但是如果未实现,则该函数指向NULL.示例:

Specifiy a function but this function if is not implemented point to a NULL. Example:

extern void something(uint some);

如果未实现,则指向NULL值.

And if this is unimplemented point to a NULL value.

因此可以这样检查:

something != NULL ? something(222) : etc.;

我想使用低谷GCC解决方案(这可以通过函数指针解决).

I would like solution with trough GCC (this could be solvable with function pointers).

推荐答案

这绝对不是可移植的,但是在某些平台上,gcc可以使用弱符号来实现此目的.我知道这适用于Linux和* BSD,但不适用于MacOS.

This is definitely not portable, but gcc can do this with weak symbols on some platforms. I know this works on Linux and *BSD, but doesn't work on MacOS.

$ cat weak.c
#include <stdio.h>

extern int foo(void) __attribute__((__weak__));

int
main(int argc, char **argv)
{
    int x = foo ? foo() : 42;

    printf("%d\n", x);
    return 0;
}
$ cat weak2.c
int
foo(void)
{
    return 17;
}
$ cc -o weak weak.c && ./weak
42
$ cc -o weak weak.c weak2.c && ./weak
17
$

这篇关于未定义的功能感测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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