简体   繁体   English

在编译时检查静态函数是否存在

[英]Checking if a static function exists at compile time

function.功能。 c (I can't edit this file) c (我不能编辑这个文件)

#define FOO    1

#if FOO == 1
    void Foo() {}
#endif

cpp cp

class MyClass
{
#if FOO == 1
    void CppFoo()
    {
        Foo();
    }
#endif
}

I want to do the same thing, but without using the define TEST in the main.cpp file我想做同样的事情,但不使用 main.cpp 文件中的定义TEST

What I want to do:我想做的事:

class MyClass
{
#if (extern "c" Foo() exist)
    void CppFoo()
    {
        Foo();
    }
#endif
}

The CppFoo() method will not have to be declared if the static function Foo() has not been declared.如果尚未声明静态函数Foo()则不必声明CppFoo()方法。

How can I do this?我怎样才能做到这一点?

You can use a weak attribute, for example:您可以使用弱属性,例如:

file ac:文件 ac:

#include <stdio.h>

int foo() __attribute__ ((weak));
int foo() {}

int main(int argc, char** argv)
{
    foo();
}

File bc:文件公元前:

#include <stdio.h>

int foo () {
    printf("Hello Wurld!\n");
}

Now if you do not compile and link bc, then the default (no-op) function foo() is called, otherwise the function foo() from bc现在,如果您不编译和链接 bc,则调用默认(无操作)函数foo() ,否则调用 bc 中的函数foo()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM